Sunday, April 15, 2007

 

Informations / Tutorial about encoding a x264 / h264 video with ffmpeg

Hi all !

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)



UUID stuff:



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



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




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


Thursday, April 12, 2007

 

Test of theora alpha7 video codec

Hi all !

Today I spent quite 2 hours to test and compare the theora alpha7 video codec. So I used ffmpeg2theora. I want to know if the theora quality is better than x264 at the same bitrate. Today, at around 480kb/s the answer is no.


Here are some results. The original video is a spiderman 3 trailer from AlloCine.fr. The main caracteristics are :

- Duration: 00:02:37.6, start: 0.000000,

- bitrate: 1029 kb/s,

- Stream #0.0: Video: mpeg4, yuv420p, 480x272, 25.00 fps(r),

- Stream #0.1: Audio: mp3, 44100 Hz, stereo, 192 kb/s.


I used this command line to convert the video stream :

ffmpeg2theora -v QUALITY -S 0 --nosound -o trailer.out Spider_Man_3_18726051_fa2_pod_xvid480.avi

with QUALITY the quality from 1 to 10.

I used this one too :

ffmpeg2theora -V BITRATE -S 0 --nosound -o trailer.out Spider_Man_3_18726051_fa2_pod_xvid480.avi

with BITRATE the bitrate in kb/s.

I try the --optimize option, which save 5% of the final size, but you need something like 2,2 times than the traditional methode to save only 5%!

Here are the results on a celeron 1GHz, 512 Mo Ram :










































































Comparison of various theora v1.0 alpha 7 codec
Test 1 Test 2 Test 3 Test 4 Test 5 Test 6 Test 7
Sharpness S0 S0 S0 S0 S0 S0 S0
Sound? nosound nosound nosound nosound nosound nosound nosound
Birate (kb/s)/Quality Birate 480 Birate 480 Quality 3 (~430kb/s) Quality 4 (~500kb/s) Quality 3.5 Quality 3.5 Quality 3.3 (~480kb/s)
--optimize? yes no no no no yes no
Final Size 8,9 Mo 8,9 Mo 8,4 Mo 11 Mo 9,5 Mo 9,3 Mo 9,2 Mo
Time to encode 4min31 1min39 1min38 1min40 1min39 4min10 1min37



Ok, on this test there are no picture, neither video to watch. But trust me, x264 at same resolution, 2-pass encoding and an average bitrate of 480kb/s the quality is really better. It may be because there are really much options to choose.


Sunday, April 01, 2007

 

Update for low bitrates movies

Yesterday, I did few tests between ogg vorbis and faac audio codec at low bitrates. So, the main thing to remember is :

1. ogg vorbis CBR encoding is really slow !

2. ogg vorbis VBR encoding is twice faster than the CBR methode

3. faac encoding is twice faster than vorbis VBR methode

4. under 80kb/s, audio is not really good. So, for low bitrates movies, you should use audio at 80kb/ at least.

5. faac add some strange audio artefact, which are away from ogg vorbis. So, you should wait for best result with FAAC codec and use OGG VORBIS today for your low rates movies.




About the container, the only one which works with a X264 video track and a VORBIS audio track is matroska container (MKV). It is pretty long to do such files. The main opertions are (under linux) :

1. ffmpeg -i movie_source.avi -acodec pcm_s16le -vn audio.wav # -vn means no video, and use pcm_s24le if your audio source is 48kHZ

2. ffmpeg -i movie_source.avi -an -vcodec copy video.avi # -an means no audio

3. compress the audio track with oggenc (ogmtools package)

oggenc \

--resample 44100 \ #optionnal

-q2 \ #around 80kb/s, 10min for 40min audio track on a 1GHz CPU

-o audioq2.ogg

audio_src.wav

4. compress the video track with avidemux

4.1 x264 codec

4.2 2 pass

4.3 450kb/s average

4.4 minq=25 / maxQ=51

4.5 resolution mplayer filter/ width=480/ lanczos #this resolution is a good compromise

4.6 MP4 container

5. mkvmerge -o movie_out.mkv -A video.mp4 audioq2.ogg #mkvtoolnix package, you can add other audio tracks too.



Finally, for a 40min TV show :

1. the audio filesize : ~26Mo,

2. the video filesize : ~66Mo,

3. the final movie filesize : ~92Mo.



Enjoy ;)

This page is powered by Blogger. Isn't yours?