english >> deutsch Home | UdS | MPI | SB

How to encode Quicktime compatible H.264/MPEG-4 AVC within mencoder



0. Introduction

In this Article I want to show you the proper command options for mencoder to encode high quality H.264 videos which are also playable by the Quicktime video player. I am using it to encode videos in Linux however the commands could be also ported to windows.

I have already posted a blog about encoding image sequence into the new H.264 Codec within ffmpeg, mencoder and some other tools. The previous post was in germain language. If you want to read it then click here. However the command lines posted before produce video files which can not be played by Quicktime.

1. Encoding audio

First you have to encode the sound track which will be muxed later with raw video data. For better support throughout all available video players you have to encode your audio track in some format suitable for H.264 conformed video streams. My choose is AAC codec because it is supported by most of the players and it seems to be one of the prefered audio codec for the H.264/MPEG4-AVC videos.

Grab your current soundtrack into a PCM wave format. This would create a file audiodump.wav in the current directory

mplayer -ao pcm -vc null -vo null INPUT.avi

Now perfrom audio conversion within faac-command. Faac is an AAC encoder which can be found on Faac-Sourceforge

faac -q 100 -c 44100 -b 192 --mpeg-vers 4 audiodump.wav

This command will encode audiodump.wav to a MPEG4-AAC confirm file audiodump.aac. With the options -b you define the bitrate in this case 192kb

2. Encoding video

The command below is used to encode a png image sequence into a raw h264 video stream which is compatible to Quicktime 7. To encode other input files like videos in other format you have to replace "mf://PATH/*.png -mf fps=FPS" by "input.avi" for example.

Please replace PATH, FPS and BITRATE with your values
mencoder mf://PATH/*.png -mf fps=FPS -vf scale=720x576,harddup -ovc x264 -x264encopts bitrate=BITRATE:threads=auto:frameref=5:bframes=0:nob_pyramid:nob_adapt:direct_pred=auto:subq=6:mixed_refs:nodct_decimate:no_psnr -nosound -ofps FPS -noskip -of rawvideo -o output.264
For 2-Pass encoding do following:
mencoder mf://PATH/*.png -mf fps=FPS -vf scale=720x576,harddup -ovc x264 -x264encopts pass=1:bitrate=BITRATE:threads=auto:frameref=5:bframes=0:nob_pyramid:nob_adapt:direct_pred=auto:subq=6:mixed_refs:nodct_decimate:no_psnr -nosound -ofps FPS -noskip -of rawvideo -o /dev/null
mencoder mf://PATH/*.png -mf fps=FPS -vf scale=720x576,harddup -ovc x264 -x264encopts pass=2:bitrate=BITRATE:threads=auto:frameref=5:bframes=0:nob_pyramid:nob_adapt:direct_pred=auto:subq=6:mixed_refs:nodct_decimate:no_psnr -nosound -ofps FPS -noskip -of rawvideo -o output.264

Mencoder will create a file output.264, which contains a raw h264 video stream. Perform step 3 to mux it with the audio track.

3. Muxing audio + video to mp4

To combine audio and video tracks together I am using MP4BOX, which is a part of gpac-package.
Replace FPS with the frame per second as already defined above.

MP4Box -fps FPS -add output.264 -add audiodump.aac output.mp4

After mp4box finishes his work there will be a file output.mp4 containing a video track encoded with H.264/MPEG-4 AVC and an audio track encoded with AAC. You can now delete all temporary files (audiodump.wav, audiodump.aac, output.264)

4. Compatibility and Quality

I have encoded an image sequence of about 7500 png files. The quality is better then previous MPEG4 or MPEG2 encodings by even smaller bitrate. The video files are playable in MPlayer, VLC and Xine. Windows Media Player can also play them but require codec installation first. And finally Quicktime 7 and above can also play such video files. I am not sure if a Quicktime player on Mac is able to decode them.








Art Tevs,    11. May 2007 22:05:29.
The article was viewed 40759 times.


1. Rob @ 30. December 2009 17:32:07
Actually, I spoke too soon. The command given in 2 which I tried, sans nob_pyramid for reasons already given, did not produce a PNG => H.264 which could be played on (OSx) DivX, Mplayer, VLC, QuickTime. VLC says (among a long debug) the file is 22.72ms. At 96 images and 7 frames per second, I'd have assumed it should be approximately 96/7 = 14 seconds in length.
2. Rob @ 30. December 2009 15:41:38
Thank you for this guide. The myriad of mencoder options is stupefying. I\'m trying to encode a sequence of PNG\'s using mencoder (SVN-r29934-snapshot-4.1.2) on RedHat(5) to an H.264 that is easily playable by Quicktime for Mac and Windows. Even considering your disclaimer in 4., would you consider helping me out?

The PNG sequence I\'m trying to movie-ise is located at the http reference given. I tried your command in 2. but my mencoder does not support nob_pyramid. With that option removed, the encoding appears to complete and on my Mac (Leopard), I can view the movie (see output.mov) in VNC but not in Quicktime (10.0).

Specifically, I tried this (see doit.sh at URL given):
output=output.mov
FPS=7

mencoder mf://*.PNG -mf fps=$FPS -vf scale=925:636,harddup -ovc x264 -x264encopts bitrate=900:threads=auto:frameref=5:bframes=0:nob_adapt:direct_pred=auto:subq=6:mixed_refs:nodct_decimate:no_psnr -nosound -ofps $FPS -noskip -of rawvideo -o $output

Any help will be appreciated!
3. David @ 17. March 2009 16:27:03
Thank you James!
Your solution is what I've been looking for for quite a while now!
Encoded films works very well with the Quicktime player.
Adding keyint=25 makes the movie more responsive to search and timeline scrubbing which is a bit sluggish without this option.
Also you can easily enough change the format and extension to mov to get it a a more familiar file for all your computer illiterate clients.
4. David @ 17. March 2009 16:26:52
Thank you James!
Your solution is what I've been looking for for quite a while now!
Encoded films works very well with the Quicktime player.
Adding keyint=25 makes the movie more responsive to search and timeline scrubbing which is a bit sluggish without this option.
Also you can easily enough change the format and extension to mov to get it a a more familiar file for all your computer illiterate clients.
5. Codecs @ 29. August 2008 05:28:48
Thanks! its really helped me a lot.! i really needed that.
6. stevpan @ 17. July 2008 02:59:19
Hi! Art. I found ffmpeg can do it in one line of command. However, ffmpeg has some restrictions on filenames.

e.g.
ffmpeg -r 25 -b 1250k -i %03d.bmp -i test.mp3 -pass 1 -acodec libfaac -ar 48000 -ac 2 -ab 128k output.mp4

See details on my google sites

https://sites.google.com/site/stevpan1/Home/frequently-used-scripts-for-audiovideo-editing
7. Art @ 31. January 2008 21:20:07
Glad to hear, Scott!

You are welcome!
8. Scott @ 31. January 2008 18:53:31
Thank you so much. I tried so many combination and it\'s just not entirely clear how to get exactly what the iPod wants. You saved me many more variations I\'m sure. FYI - the newest version of iTunes does not seem to require the nicmp4box.
9. James Hastings-Trew @ 20. September 2007 04:10:08
There is no reason to encode the audio and video separately - mencoder is quite capable of creating a valid mp4 stream on its own:

mencoder INPUT -sws 9 -vf harddup /
-oac faac -faacopts br=128:mpeg=4:object=2:raw /
-ovc x264 -x264encopts bitrate=900:vbv_maxrate=1500:vbv_bufsize=2000:nocabac:/
level_idc=13:global_header /
-of lavf -lavfopts format=mp4 -o OUTPUT

If you want multipass stick the :pass=1 or :pass=2 commands into the -x264encopts as needed, and don't forget the -ofps 24000/1001 or 30000/1001 option to tell mencoder what framerate your output file is. If you want iTunes to be able to import the result into your ipod, you'll need to pass the result through NicMP4Box:

nicmp4box -add OUTPUT FINALFILE

This adds the "ipod atom" to the file.
10. Art @ 07. June 2007 10:07:13
You are welcome!
I like to hear, that the article was helpful to somebody else.
11. futbol4 @ 06. June 2007 20:35:32
Thanks for this! I have been trying to figure out how to do this in windows via command line. I was able to use mencoder and MP4Box for windows and run them command line!

Write a comment


Name*:
http://
Comment*:
Type the characters from the image here.