Sunday, April 15, 2007
Informations / Tutorial about encoding a x264 / h264 video with ffmpeg
I have just found that you can use ffmpeg to encode / transcode a h264 video. First, you have to compile it with right options :
./configure \
--prefix=/usr \
--enable-gpl \
--enable-mp3lame \
--enable-libogg \
--enable-libvorbis \
--enable-libtheora \
--enable-x264 \
--enable-pp \
--enable-libfaac \
--enable-libfaad \
--enable-liba52 \
--enable-xvid \
--enable-dts \
--enable-swscaler \
--enable-pp \
--enable-static \
--enable-pthreads \
I included theora codec to.
Now, you can read these pages :
- ffmpeg x264 encoding-guide,
- ipod video guide,
- x264 qualityspeed guide.
If you're interesting in adding subtitles in a video, you should read this french blog.
The following lines are a copy of this blog.
Encoding h.264 in FFmpeg
The defaults for x264 in FFmpeg are currently significantly detrimental to the quality of the encoded video. This is due to the internal behaviour of FFmpeg with respect to codec defaults. Depending on the rate control method used, a number of different switches need applying to bring the settings into line with the x264 command line interface defaults. These are generally OK settings as they have been suggested by the author(s). The necessary switches for FFmpeg to ‘correct’ the defaults until a proper solution is completed are detailed below, however these may be altered as required.
Apply these settings for all RC methods:
-coder 1 -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -me hex -subq 5 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71
Apply these settings for constant QP:
-cqp 26
Apply these settings for variable bit rate:
-bt BIT_RATE -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4
So if you wanted to encode using two-pass VBR, the command line would be something like:
ffmpeg -i INPUT -an -pass 1 -vcodec h264 -b BIT_RATE -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -flags2 -8x8dct-wpred -me epzs -subq 1 -trellis 0 -refs 1 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt BIT_RATE -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 OUTPUT.mp4
ffmpeg -i INPUT -acodec aac -ab 128k -pass 2 -vcodec h264 -b BIT_RATE -flags +loop -cmp +chroma -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 -flags2 +bpyramid+wpred+mixed_refs+8x8dct+brdo -me umh -subq 7 -trellis 2 -bidir_refine 1 -refs 5 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt BIT_RATE -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 OUTPUT.mp4
Note: I’ve altered the settings from the x264 CLI defaults. The quality settings can be considerably reduced for the first pass of two as it has little impact on the output of the second (or nth) pass.
Some general notes for x264 options in FFmpeg that are not self-explanatory:
* ME methods {name (n) for -me name ; ignore n}: dia (5/epzs), hex (7), umh (8), esa (2/full)
* Entropy coders {name (n for -coder n)}: CAVLC (0), CABAC (1)
* i_qfactor = i_frame_qp/p_frame_qp = 1/ipratio (note that this is not the same as ‘ipratio’ from the x264 CLI)
* Bit rate tolerance: bt = BIT_RATE*ratetol (’ratetol’ is the related parameter from the x264 CLI)
Note: If you think of any others, please contact me.
Those of you looking for the iPod/PSP information, I felt it was a little out of place here and have subsequently moved them to their own pages. iPod guide PSP guide
x264 quality/speed guide
The main settings you may want to alter are show below. You will want to reduce the options at the top first:
* 8×8dct and parti8×8
* brdo
* mixed_refs
* partitions
* max_b_frames
* me
* refs
* trellis
* subq
iPod video guide
Encoding videos suitable for iPods
Thanks to “cartman” on the FreeNode IRC network for testing and providing information for this guide.
The “new” higher resolution capability of the iPod video uses something Apple have dubbed a “Low Complexity” version of the Baseline Profile of H.264. Thankfully someone discovered what this meant. (See here for details - credit goes to Tyler Loch for this information.)
At first I wondered what the use of 640×480 video on a 320×240 screen would be but I was informed that these devices have a TV out. Fun. Anyway, on to the good stuff.
1.5Mbit/s 640×480 H.264 videos (for improved quality using TV out)
- BIT_RATE <= 1500 kbps
- 640×480
- Up to 30 fps
- “Low-Complexity” H.264 Baseline Profile
- 1 reference frame
- Up to H.264 level 3
- 640 pixels maximum frame width
- Sample Aspect Ratio (SAR) must be 1:1
- UUID atom must exist containing the following hex data: 6B 68 40 F2 5F 24 4F C5 BA 39 A5 1B CF 03 23 F3
UUID stuff:
- FFmpeg’s libavformat does not yet write this atom but I will request that it be added.
- MP4Box from the GPAC project should write the atom but the size of the atom should be 28 bytes and MP4Box files have the UUID atom as 24 bytes. I don’t know what makes up the 28 bytes but I’ve contacted Jean Le Feuvre (the GPAC guy) about this so hopefully it will be fixed in the development code shortly. In the meantime feel free to use NicMP4Box or figure out the hacked code and readd it into MP4Box. I’ve had a look at Nick’s hacks myself and they are fairly minimal.
- AtomicParsley works! But only the development code which can be checked out from their subversion repository. See below for usage.
For one pass:
ffmpeg -i INPUT -acodec aac -ab 160k -s WIDTHxHEIGHT -vcodec h264 -b BIT_RATE -flags +loop -cmp +chroma -partitions +parti4x4+partp4x4+partp8x8+partb8x8 -me umh -subq 7 -trellis 2 -refs 1 -coder 0 -me_range 16 -g 300 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt BIT_RATE -maxrate 10M -bufsize 10M -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect WIDTH:HEIGHT OUTPUT.mp4
AtomicParsley OUTPUT.mp4 --DeepScan --iPod-uuid 1200 --overWrite
For two pass:
ffmpeg -i INPUT -an -pass 1 -s WIDTHxHEIGHT -vcodec h264 -b BIT_RATE -flags +loop -cmp +chroma -partitions 0 -me epzs -subq 1 -trellis 0 -refs 1 -coder 0 -me_range 16 -g 300 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt BIT_RATE -maxrate 10M -bufsize 10M -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect WIDTH:HEIGHT OUTPUT.mp4
ffmpeg -i INPUT -acodec aac -ab 160k -pass 2 -s WIDTHxHEIGHT -vcodec h264 -b BIT_RATE -flags +loop -cmp +chroma -partitions +parti4x4+partp4x4+partp8x8+partb8x8 -me umh -subq 7 -trellis 2 -refs 1 -coder 0 -me_range 16 -g 300 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt BIT_RATE -maxrate 10M -bufsize 10M -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect WIDTH:HEIGHT OUTPUT.mp4
AtomicParsley OUTPUT.mp4 --DeepScan --iPod-uuid 1200 --overWrite
768kbit/s 320×240 H.264 videos
- BIT_RATE <= 768 kbps
- 320×240
- Up to 30 fps
- H.264 Baseline Profile up to level 1.3
For one pass:
ffmpeg -i INPUT -acodec aac -ab 160k -s WIDTHxHEIGHT -vcodec h264 -b BIT_RATE -flags +loop -cmp +chroma -partitions +parti4x4+partp4x4+partp8x8+partb8x8 -flags2 +mixed_refs -me umh -subq 7 -trellis 2 -refs 5 -coder 0 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt BIT_RATE -maxrate 768k -bufsize 2M -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 13 -title SOME_TITLE OUTPUT.mp4
For two pass:
ffmpeg -i INPUT -an -pass 1 -s WIDTHxHEIGHT -vcodec h264 -b BIT_RATE -flags +loop -cmp +chroma -partitions 0 -me epzs -subq 1 -trellis 0 -refs 1 -coder 0 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt BIT_RATE -maxrate 768k -bufsize 2M -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 13 -title SOME_TITLE OUTPUT.mp4
ffmpeg -i INPUT -acodec aac -ab 160k -pass 2 -s WIDTHxHEIGHT -vcodec h264 -b BIT_RATE -flags +loop -cmp +chroma -partitions +parti4x4+partp4x4+partp8x8+partb8x8 -flags2 +mixed_refs -me umh -subq 7 -trellis 2 -refs 5 -coder 0 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt BIT_RATE -maxrate 768k -bufsize 2M -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 13 -title SOME_TITLE OUTPUT.mp4
IF ANYONE HAS FEEDBACK ON THE SETTINGS BELOW IT WOULD BE APPRECIATED!
2.5Mbit/s 640×480 MPEG-4 SP videos
- BIT_RATE <= 2500 kbps
- 640×480
- Up to 30 fps
- Simple Profile
For one pass:
ffmpeg -i INPUT -acodec aac -ab 160k -s WIDTHxHEIGHT -vcodec mpeg4 -b BIT_RATE -flags +aic+mv4+trell -mbd 2 -cmp 2 -subcmp 2 -g 250 -maxrate 768k -bufsize 2M -title SOME_TITLE OUTPUT.mp4
For two pass:
ffmpeg -i INPUT -an -pass 1 -s WIDTHxHEIGHT -vcodec mpeg4 -b BIT_RATE -flags +aic+mv4+trell -mbd 2 -cmp 2 -subcmp 2 -g 250 -maxrate 768k -bufsize 2M -title SOME_TITLE OUTPUT.mp4
ffmpeg -i INPUT -acodec aac -ab 160k -pass 2 -s WIDTHxHEIGHT -vcodec mpeg4 -b BIT_RATE -flags +aic+mv4+trell -mbd 2 -cmp 2 -subcmp 2 -g 250 -maxrate 768k -bufsize 2M -title SOME_TITLE OUTPUT.mp4
<< Home