Closed WebCrusader closed 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
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
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.
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
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