I'm trying to encode a video with ffmpeg into H.264 (via the libx264 library) with a constant bit rate. I know, I know, VBR is often preferred, but for this specific job I'm required to use CBR (just as long as it's so many kilobytes per second; it doesn't have to be an exact kilobytes per frame, afaik). My sample video I'm using to test is from here: http://a1408.g.akamai.net/5/1408/1388/2005110403/1a1a1ad948be278cff2d96046ad90768d848b41947aa1986/sample_iTunes.mov.zip (it comes from http://support.apple.com/kb/HT1425)
I can get a constant bit rate when encoding the video with MPEG-4 Video (using the commands ffmpeg -i sample_iTunes.mov -b 819968 -minrate 819968 -maxrate 819968 out.mov
), and the bit rate is as expected. Reading the video's specs via the QuickTime Inspector, it's got a data rate of 844.94 kbit/s. Cool.
However, when I change the codec to libx264, it seems to completely ignore my bitrate requests! The command I'm trying is "ffmpeg -i sample_iTunes.mov -vcodec libx264 -vpre medium -b 819968 -vb 819968 -minrate 819968 -maxrate 819968 -bufsize 400000 test.mov
". But when I check the video's specs via the QuickTime Inspector, it's got a data rate of 254.74 kbit/s. WTF? That's not even close!
I've tried changing so many parameters and adding tons of different things, and I've spent 2 days googling this, but I can't seem to get it to work. If I encode the video with the MainConcept H.264 encoder, I can get a constant bitrate, but I need this to work with ffmpeg.
If someone can help me figure out how to do CBR H.264 encoding with FFmpeg, I will love you forever!
You can select the codecs needed by using the -c flag. This will make a Matroska container with a VP9 video stream and a Vorbis audio stream, essentially the same as the WebM we made earlier. The command ffmpeg -codecs will print every codec FFmpeg knows about.
264. For 4K @ 30fps h. 264, the recommended target average bitrate is ~45 Mbps, and for HEVC, it is ~22.5 Mbps. For 4K @ 60fps HEVC, the recommended target average bitrate is ~48-54 Mbps, depending on the device.
H.264 is typically used for lossy compression, although it is also possible to create truly lossless-coded regions within lossy-coded pictures or to support rare use cases for which the entire encoding is lossless.
H. 264 is the most popular compression format for a good reason. It allows your file size to shrink substantially lower without harsh impacts on your video quality.
I too have been working on trying to get CBR out of x264. I found this blog post by Dark Shakari quite interesting.
Here is what I have for low-latency CBR video to an MPEG tranport stream:
ffmpeg -i sintel_trailer-720p.mp4 -an -tune zerolatency \
-x264opts bitrate=4000:vbv-maxrate=4000:vbv-bufsize=166 \
-vcodec libx264 -f mpegts -muxrate 4000K -y trailer.ts
According the x264 developer's blog you set:
Finally, set the ffmpeg switch for x264 of -tune zerolatency.
Hope that's helpful. And, if anyone has improvements to this please do let me know!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With