simple way to make h.264 mp4 web and iOS/mobile playable video mp4 files for linux and macosx using ffmpeg

Greetings video geeks! 😎

At my job, I’ve updated the process and way we create our .mp4 files that are shown on video pages on

It’s a much cleaner/clearer process, namely:

  • I opted to ditch ffpreset files in favor of command-line argument 100% equivalents. It seems a bit easier for someone reading the task log of their item, trying to see what we did.
  • I no longer need qt-faststart step and dropped it. I use the cmd-line modern ffmpeg “-movflags faststart”

Entire processing is now done 100% with ffmpeg, in the standard “2-pass” mode
As before, this output .mp4:

  • plays in modern html5 video tag compatible browsers
  • plays in flash plugin within browsers
  • works on all iOS devices
  • makes sure the “moov atom” is at the front of the file, so browsers can playback before downloading the entire file, etc.

Here is an example (you would tailor especially the “scale=640:480” depending on source aspect ratio and desired output size; change or drop altogether the “-r 20” option (the source was 20 fps, so we make the dest 20 fps); tailor the bitrate args to taste):

  • ffmpeg -y -i stairs.avi -vcodec libx264 -pix_fmt yuv420p -vf yadif,scale=640:480 -profile:v baseline -x264opts cabac=0:bframes=0:ref=1:weightp=0:level=30:bitrate=700:vbv_maxrate=768:vbv_bufsize=1400 -movflags faststart -ac 2 -b:a 128k -ar 44100 -r 20 -pass 1 -acodec aac -strict experimental stairs.mp4;
  • ffmpeg -y -i stairs.avi -vcodec libx264 -pix_fmt yuv420p -vf yadif,scale=640:480 -profile:v baseline -x264opts cabac=0:bframes=0:ref=1:weightp=0:level=30:bitrate=700:vbv_maxrate=768:vbv_bufsize=1400 -movflags faststart -ac 2 -b:a 128k -ar 44100 -r 20 -pass 2 -acodec aac -strict experimental -metadata title='”Stairs where i work” – lame test item, bear with us –’ -metadata year=’2004′ -metadata comment=license:’’ stairs.mp4;

Happy hacking and creating!

PS: here is the way we compile ffmpeg (we use ubuntu linux at work, but this script works on macosx, too).

convert yuvj420p to yuv420p (chrome playable!) mp4 video (eg: canon/nikon video)


found a nice (video lossless, best i can tell) way to convert the video from cameras like my Canon ELPH SD1400,
which is *already* h.264 video + PCM mono audio

to a new mp4 container with aac audio. that part’s easy/cake w/ ffmpeg — but the trick to get the h.264
video part to play in chrome browser and/or with a flash plugin is to get the flagged “yuvj420p” colorspace pixels
to be considered “yuv420p”. it seems like the former is >= 8 bits-per-pixel and has a range wider than the 256 values;
while the later is 8 bit per pixel. ( brief info/notes/background )

at any rate, finally found this nice post:

which refers to this modified MP4Box/gpac tree, to switch the “fullrange” color-related flag off:

and then, voila! i have a nice little script where i can convert my canon ELPH videos to a html5 video tag and flash plugin compatible mp4:

#!/bin/bash -ex

IN=${1:?"Usage: [input video] [output video]"};
OUT=${2:?"Usage: [input video] [output video]"};

# make a hacked version of "mp4box" that can toggle a colorspace-related flag in our video!
if [ ! -e $HOME/scripts/mp4box ]; then
(git clone && cd gpac) || \
( cd gpac && git reset --hard && git clean -f && git pull && git status );

./configure --enable-pic --static-mp4box --enable-static-bin
make -j4;

# bonus points: make a *static* binary so if we change linux/OS versions, dont hafta worry!
cd applications/mp4box;
gcc -o $HOME/scripts/mp4box -static main.o filedump.o fileimport.o live.o -L../../bin/gcc -lgpac_static -lm -lpthread -ldl -lz

# demux
ffmpeg -y -i "$IN" -an -vcodec copy video.mp4;
ffmpeg -y -i "$IN" -vn -acodec copy audio.wav;

# convert yuvj420p to yuv420p the cheater way (for chrome and flash plugin playback!)
rm -fv tmp.mp4;
$HOME/scripts/mp4box -add video.mp4#:fullrange=off tmp.mp4

# convert wav audio to aac
ffmpeg -y -i tmp.mp4 -i audio.wav -acodec libfaac -ac 1 -ab 256k -vcodec copy t2.mp4;
qt-faststart t2.mp4 "$OUT";
rm -fv video.mp4 audio.wav t2.mp4 tmp.mp4;

natively compiling ffmpeg, mplayer, mencoder on MacOS Lion (with x264!)

OK, I’ve revamped my script to compile these tools:

on MacOS Lion, using the heads of the trees, with direct encoding support for:
vpx / WebM

A nice recent update to ffmpeg is the ability to decode/read Apple ProRes, too!

natively compiling ffmpeg, x264, mplayer on mac (with builtin x264 and webm encoding)

i’m very pleased that after years of hacks here and there, all three heads of the current codebases pretty well build natively on mac (snow leopard) for

  • x264
  • ffmpeg
  • mplayer

here, i’m making sure that i compile in static version of libvpx (Webm) and libx264 (h.264) video packages so that the ffmpeg can easily make hiqh quality h.246 and webm transcoded videos.


So we can make nice (2 pass video) ~768 kb/sec 640×480 derivatives like so (alter “-r ” as appropriate):

# make WebM
ffmpeg -deinterlace -y -i 'camels.avi' -vcodec webm -fpre libvpx-360p.ffpreset -vf scale=640:480 -r 20 -threads 2 -map_meta_data -1:0 -pass 1 -an tmp.webm

ffmpeg -deinterlace -y -i 'camels.avi' -vcodec webm -fpre libvpx-360p.ffpreset -vf scale=640:480 -r 20 -threads 2 -map_meta_data -1:0 -pass 2 -acodec libvorbis -ab 128k -ac 2 -ar 44100 tmp.webm

mv tmp.webm 'camels.webm'


# make h.264

ffmpeg -deinterlace -y -i 'camels.avi' -vcodec libx264 -fpre libx264-IA.ffpreset -vf scale=640:480 -r 20 -threads 2 -map_meta_data -1:0 -pass 1 -an tmp.mp4

ffmpeg -deinterlace -y -i 'camels.avi' -vcodec libx264 -fpre libx264-IA.ffpreset -vf scale=640:480 -r 20 -threads 2 -map_meta_data -1:0 -pass 2 -acodec aac -strict experimental -ab 128k -ac 2 -ar 44100 tmp.mp4

qt-faststart tmp.mp4 'camels.mp4'

our preset files:

Nice things about the h.264 derivative:

  • plays on all iphones and ipads
  • browser video tag and/or flash plugin compatible
  • starts immediately, seeks immediately even before entire video is downloaded

hope this may be useful!

Star Wars re-remastered to high quality 1977 version!

So I wanted to sit down in my updated home theatre and watch one of my favorite films, “Star Wars”. Problem is, I have two versions on DVD, and neither are ideal. The 2004 DVD version has remastered audio and video, but also added scenes and changes I really don’t like. The 1977 DVD version is a poor quality transfer and encoding.

So I combined the 1977 and 2004 DVDs into the highest quality 1977 version of the film! I show all my scripts and techniques here, too, so you can make the same version from your two DVDs, too!

star wars comparison of two films
star wars comparison of two films

ffmpeg for time-lapse, sets of images, and even *archiving*!

I now use the FFMPEG package compiled locally on my Mac Leopard laptop.

If you want to take a bunch of JPEG images, you can turn them into a “motion JPEG” AVI video file (which is ideal for time-lapse). What’s neat about ffmpeg, is you can turn a directory of JPEGs into an AVI and later recreate the JPEGs from the AVI.

Thus, I can take 100-1000 JPEG images from a “shoot” and create a video time-lapse of all the JPEG images in a given subdir in a command-line shell (terminal):
ffmpeg -r 6 -i "%04d.jpg" -an -vcodec copy out.avi

Even cooler, you can go directly back to *the same JPEG* images like so (this is where you can see it is a lossless conversion from JPEGs to Motion JPEG and back):
ffmpeg -i out.avi -vcodec copy "%04d.jpg"

“-r 6” is the framerate, ie: “make the video play 6 JPEG images per second”.
It doesn’t get much nicer than that! Not to mention, the encoding is FAST! I recently moved from mencoder/mplayer to ffmpeg since you can specify a complete copy of the input JPEGs (lossless from the source) as well as compress them down a bit (as you like).

For the lossless conversion, the (not entirely obvious) *key* to success is to use the “-vcodec copy” *after* the source file(s).

You can get this running on Windows with Cygwin, Mac with terminal, and any linux distribution like Ubuntu.

ffmpeg building on mac intel/x386

ffmpeg v0.5 just came out. it’s the bomb. it’s got tons of fixes and massive amounts of new codecs that it can read. for example, it can now decode my professional filmmaker brother’s “DVC ProHD” highly proprietary (and massive bitrate!) format! it can also decode flac and 24-bit flac. (encoding flac is disappointing though).

at any rate! macports is a great way to get it installed on your mac.
the current way of setting up macports and then doing
sudo port install ffmpeg
works fine on my PPC at work (oddly — pretty old computer now) but not my Air (intel x386)

So I set out to find the fixes needed to make it work. Here they are:

step 1: install macports — see

sudo port install x264 +noasm # for i386 (not needed for PPC)
sudo port fetch ffmpeg
sudo port checksum ffmpeg
sudo port extract ffmpeg
sudo port patch ffmpeg
remove “–enable-shared” from /opt/local/var/macports/sources/
sudo port install ffmpeg

[May update] I think the packages are improving so some of the above steps may not be necessary, depending on your Mac model, xcode version, and such. So I’d suggest just trying “sudo port install ffmpeg” first to see if it works and try some tweaks above if that doesn’t work