continue-revolution / sd-webui-animatediff

AnimateDiff for AUTOMATIC1111 Stable Diffusion WebUI
Other
3.11k stars 258 forks source link

[Feature]: mp4 export bit rate setting #343

Closed WebCrusader closed 11 months ago

WebCrusader commented 11 months ago

Expected behavior

Can you add setting for the mp4 file bit rate, seems the default is 1000k but it will be good to be able to modify it for better quality

example setup of the bit rate here: https://g.co/bard/share/4910d5d8e357

zappityzap commented 11 months ago

Bard is hallucinating, there is no set_bit_rate function in PyAV.

I have a PR in the works that allows setting CRF, preset and tune: https://trac.ffmpeg.org/wiki/Encode/H.264

WebCrusader commented 11 months ago

Bard is not having good time with the pyav documentation

So after reading these sources: https://github.com/PyAV-Org/PyAV/issues/726 https://trac.ffmpeg.org/wiki/Encode/H.264

managed to create the following solution:

import av

# replace the current mp4 file write function to use pyav directly

            crf = shared.opts.data.get("animatediff_mp4_crf", 23)
            logger.info(f"Saving {video_path_mp4} with crf={crf}")
            output = av.open(video_path_mp4, "w")
            stream = output.add_stream('h264', params.fps, options={'preset':'veryslow','crf':f'{crf}'})
            stream.width = frame_list[0].width
            stream.height = frame_list[0].height
            for img in video_array:
                frame = av.VideoFrame.from_ndarray(img)
                packet = stream.encode(frame)
                output.mux(packet)
            packet = stream.encode(None)
            output.mux(packet)
            output.close()
            # with imageio.imopen(video_path_mp4, 'w', plugin='pyav') as file:
            #     if use_infotext:
            #         file.container_metadata["Comment"] = infotext
            #     logger.info(f"Saving {video_path_mp4}")
            #     file.write(video_array, codec='h264', fps=params.fps, filter_graph={'quality':'10'})

for scripts\animatediff_output.py

    shared.opts.add_option(
        "animatediff_mp4_crf",
        shared.OptionInfo(
            23,
            "MP4 file CRF Quality (small value is better quality) (Default: 23)",
            gr.Slider,
            {
                "minimum": 17,
                "maximum": 28,
                "step": 1},
            section=section
        )
    )

in scripts\animatediff.py

zappityzap commented 11 months ago

Thanks for sharing that code. I've been working on imageio[ffmpeg] to save the video with quality settings, and then remuxing with pyav to add the metadata. Using only pyav might be simpler.

WebCrusader commented 11 months ago

seems like imageio just restricts you how can you use pyav and don't expose the stream options in any way

https://github.com/search?q=repo%3Aimageio%2Fimageio%20add_stream&type=code

so using directly pyav is the only option