Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding H.264 CBR videos with FFmpeg

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!

like image 502
Cornstalks Avatar asked Aug 19 '11 17:08

Cornstalks


People also ask

How do I specify codec in FFmpeg?

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.

What is the best bitrate for H 264?

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.

Is H 264 lossless?

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.

Does H 264 reduce quality?

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.


1 Answers

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:

  • vbv-maxrate = bitrate = B = target bitrate
  • vbv-bufsize = B / fps (in this video's case that's 24 fps)

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!

like image 54
mevatron Avatar answered Sep 27 '22 23:09

mevatron