Why I need this?

I have generated many avi files from scientific programs during my daily research work. They are very simple, but huge. 300MB for a 15-second movie. Based on my understanding, these avi files are encoded by jpeg format ... If I could use H.264 video code to encode them, how much will be? Less than 3MB! Yes, 100 to 1 compression, and no visual compromise! Since I have already finished the analysis, and these movies are purely for demonstration, it would save a lot of space and save a lot of time during transferring. Of course, I want to do the file conversion automatically.
Test environment
Windows 7 + FFmpeg (there are other places you can download ffmpeg win32 builds; as long as they have included libx264 and ffpresent files, they should work as well)
- Besides win7, the following code should work on windows xp and vista.
- It should be very easy adapted to mac/linux using shell scripts.
- I chose win7 as my working platform, because the most powerful computer I can access right now is running win7.
How to do it?
1. make sure the ffmpeg is running appropriately - after unzip the win32 ffmpeg build, you should see a file called ffmpeg.exe; open a windows command line (win+r ; cmd enter); go to the ffmpeg folder and type in ffmpeg (you should see screen rolling and please make sure libx264 is enabled)
2. test tun, assuming your avi file named input.avi; run the following command
ffmpeg -i input.avi -vcodec libx264 -vpre "C:\Users\ffmpeg\ffpresets\libx264-max.ffpreset" -b 1600k output.mp4
Please do change "C:\Users\ffmpeg\ffpresets\libx264-max.ffpreset" into whatever your file's absolute address.
Here is the output
[video vid_image=http://en.dogeno.us/wp-content/uploads/2010/02/2010-02-12_000012-452x429.png filename=http://en.dogeno.us/wp-content/uploads/2010/02/output-100k-max.mp4 /]
The flash player crops the movie, check the full version by downloading output 1600kMax.
I used single-pass max-quality (bit rate at 1600k) compression. However, after some reading, I noticed that using two-pass hq-quality (bit rate at 2000k) setting could significantly speed the compression process, which can be done by the following batch file
2pass.bat (open notepad, copy&paste, save it as 2pass.bat, double click it to run)
ffmpeg -y -i input.avi -b 2M -bt 4M -vcodec libx264 -pass 1 -vpre "C:\Users\ffmpeg\ffpresets\libx264-fastfirstpass.ffpreset" output.mp4
ffmpeg -y -i input.avi -b 2M -bt 4M -vcodec libx264 -pass 2 -vpre "C:\Users\ffmpeg\ffpresets\libx264-hq.ffpreset" output.mp4
Please do change "C:\Users\ffmpeg\ffpresets\libx264-fastfirstpass.ffpreset" "C:\Users\ffmpeg\ffpresets\libx264-hq.ffpreset" into whatever your files' absolute address.
Here is the two-pass compressed video
[video vid_image=http://en.dogeno.us/wp-content/uploads/2010/02/2010-02-12_000012-452x429.png filename=http://en.dogeno.us/wp-content/uploads/2010/02/output-2pass.mp4 /]
The flash player crops the movie, check the full version by downloading output2pass.
I couldn't see any difference! The file size is about the same.
3. use windows batch file to automatically compress all AVI files in the current folder
batch.bat (open notepad, copy&paste, save it as batch.bat, double click it to run)
@ ECHO OFF
REM The following will convert all avi files in the current folder into mp4 files.
IF EXIST *.avi FOR %%A IN (*.avi) DO CALL :twopass "%%A"
:twopass
ffmpeg.exe -y -i %* -b 2M -bt 4M -vcodec libx264 -pass 1 -vpre "C:\Users\ffmpeg\ffpresets\libx264-fastfirstpass.ffpreset" output.mp4
ffmpeg.exe -y -i %* -b 2M -bt 4M -vcodec libx264 -pass 2 -vpre "C:\Users\ffmpeg\ffpresets\libx264-hq.ffpreset" output.mp4
RENAME output.mp4 %*.mp4
ECHO.
GOTO :EOF
Enjoy!
FFmpeg commands on the Internet
ffmpeg -y -i test.mov -acodec libfaac -ar 44100 -ab 96k -coder ac -me full -me_range 16 -subq 5 -sc_threshold 40 -vcodec libx264 -s 1280x544" -b 1600k -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -i_qfactor 0.71 -keyint_min 25 -b_strategy 1 -g 250 -r 20 87.mp4;
-y :: overwrite the output file if it already exists
-i trailerTest.mov :: the input video file
-acodec libfaac :: using the aac codec
-ar 44100 :: the audio sampling rate
-ab 96k :: the audio bitrate
-vcodec libx264 :: use the x264 codec
-s 1280x540 :: the size of the output video
-b 1600k :: the bitrate of the output video
-g 250 :: frequency of keyframes
-r 20 :: the frame rate
87.mp4 :: the output file
quicktime .mov conversion
ffmpeg -y -i input.mov -acodec libfaac -ar 44100 -ab 96k -vcodec libx264 -level 41 -crf 20 -bufsize 20000k -maxrate 25000k -g 250 -r 20 -s 1280x544 -coder 1 -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -flags2 +brdo+dct8x8+bpyramid -me umh -subq 7 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -rc_eq 'blurCplx^(1-qComp)' -bf 16 -b_strategy 1 -bidir_refine 1 -refs 6 -deblockalpha 0 -deblockbeta 0 output.mp4
720p compress
ffmpeg.exe -y -i "input.avi" -f mp4 -vcodec libx264 -s 1280x720 -r 24000/1001 -b 4096k -bt 4096k -acodec libfaac -ac 2 -ab 128k "720-24p.mp4“
High quality 2 Pass
ffmpeg -y -i INPUT -r 30000/1001 -b 2M -bt 4M -vcodec libx264 -pass 1 -vpre fastfirstpass -an output.mp4
ffmpeg -y -i INPUT -r 30000/1001 -b 2M -bt 4M -vcodec libx264 -pass 2 -vpre hq -acodec libfaac -ac 2 -ar 48000 -ab 192k output.mp4
iPod-iPhone 320 width
ffmpeg -i INPUT -s 320x240 -r 30000/1001 -b 200k -bt 240k -vcodec libx264 -vpre ipod320 -acodec libfaac -ac 2 -ar 48000 -ab 192k output.mp4
iPod-iPhone 640 width
ffmpeg -i INPUT -s 640x480 -r 30000/1001 -b 200k -bt 240k -vcodec libx264 -vpre ipod640 -acodec libfaac -ac 2 -ar 48000 -ab 192k output.mp4
Hight quality, 2 pass without presset
ffmpeg -y -i input -r 24000/1001 -b 6144k -bt 8192k -vcodec libx264 -pass 1 -flags +loop -me_method dia -g 250 -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -bf 16 -b_strategy 1 -i_qfactor 0.71 -cmp +chroma -subq 1 -me_range 16 -coder 1 -sc_threshold 40 -flags2 -bpyramid-wpred-mixed_refs-dct8x8+fastpskip -keyint_min 25 -refs 1 -trellis 0 -directpred 1 -partitions -parti8x8-parti4x4-partp8x8-partp4x4-partb8x8-an output.mp4
ffmpeg -y -i input -r 24000/1001 -b 6144k -bt 8192k -vcodec libx264 -pass 2 -flags +loop -me_method umh -g 250 -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -bf 16 -b_strategy 1 -i_qfactor 0.71 -cmp +chroma -subq 8 -me_range 16 -coder 1 -sc_threshold 40 -flags2 +bpyramid+wpred+mixed_refs+dct8x8+fastpskip -keyint_min 25 -refs 4 -trellis 1 -directpred 3 -partitions +parti8x8+parti4x4+partp8x8+partb8x8-acodec libfaac -ac 2 -ar 44100 -ab 128k output.mp4
iPod-iPhone 320 width, without presset
ffmpeg -i INPUT -s 320x240 -r 30000/1001 -b 200k -bt 240k -vcodec libx264 -coder 0 -bf 0 -flags2 -wpred-dct8x8 -level 13 -maxrate 768k -bufsize 3M-acodec libfaac -ac 2 -ar 48000 -ab 192k output.mp4
iPod-iPhone 640 width, without presset
ffmpeg -i INPUT -s 320x240 -r 30000/1001 -b 200k -bt 240k -vcodec libx264 coder 0 -bf 0 -refs 1 -flags2 -wpred-dct8x8 -level 30 -maxrate 10M -bufsize 10M-acodec libfaac -ac 2 -ar 48000 -ab 192k output.mp4
iPod-iPhone, 2 pass custom
ffmpeg -y -i input -r 30000/1001 -s 480x272 -aspect 480:272 -vcodec libx264 -b 512k -bt 1024k -maxrate 4M -flags +loop -cmp +chroma -me_range 16 -g 300 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -rc_eq "blurCplx^(1-qComp)" -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -coder 0 -refs 1 -bufsize 4M -level 21 -partitions parti4x4+partp8x8+partb8x8 -subq 5 -f mp4 -pass 1 -an -title "Title" output.mp4
ffmpeg -y -i input -r 30000/1001 -s 480x272 -aspect 480:272 -vcodec libx264 -b 512k -bt 1024k -maxrate 4M -flags +loop -cmp +chroma -me_range 16 -g 300 keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -rc_eq "blurCplx^(1-qComp)" -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -coder 0 -refs 1 -bufsize 4M -level 21 -partitions parti4x4+partp8x8+partb8x8 -subq 5 -f mp4 -pass 2 -acodec libfaac -ac 2 -ar 44100 -ab 128k -title "Title" output.mp4
Great tut dude!
Very good tutorial. From 115,2 MB to 1,8 MB.
Best regards and good luck with your research.
Is there a difference to use ffmpeg or mencoder? Are the mencoder way too faster then ffmpeg?
I don't know. mencoder is from mplayer? never test, interest to know...