Video conversion: ogv to mp4
31 Aug 2011
I sometimes record screencasts for clients (or just for fun) using recordmydesktop, a great, simple screen-and-voice recorder on Ubuntu.
It captures the screen and audio for a selected area and outputs it to an .ogv file. This file views fine on my own computer, but Windows users and others often have trouble with it, and it's also quite a large file size by default.
So I often convert my screencasts to .mp4 format and then put them on screencast.com. Screencast.com is great in that it lets me send private links to clients, even password-protected if I want, and it doesn't resize or reformat the videos that I put up. Screencast.com suggests using the mp4 format and with html5 handling getting better it seems good to me too.
I've always been a fan of mencoder for converting video. I used to convert the recordmydesktop .ogv files to .avi files using this script:
nice mencoder inputfile.ogv -ovc xvid -vf harddup -xvidencopts bitrate=800 -oac mp3lame -lameopts abr:br=92 -channels 1 -o outputfile.avi
or, using a negative bitrate creates an output file of that size using whatever bitrate is needed:
nice mencoder inputfile.ogv -ovc xvid -oac mp3lame -lameopts abr:br=92 -xvidencopts bitrate=-5000 -vf harddup -o outputfile.avi
I would put these scripts into a shell file and then run them as needed from the command line.
However, recently mencoder has had problems for me. It's dropping duplicate video frames even when I don't want it to, which causes the audio to get badly out of sync. It also doesn't seem to handle mp4 output formats well.
So I've been looking around for an alternative.
Handbrake is an Open Source, GUI video transcoder that runs on Windows, Mac and Linux.
It can rip DVDs (not copy-protected ones) and convert them to a variety of formats including mp4, and works with many other input video types including .ogv - so recordmydesktop screencasts are (theoretically) easy to handle.
To get it on Ubuntu you'll need to add the PPA.
However, while the interface looks nice and easy I found that for me the mp4 files created by Handbrake only contained a single still image, not moving video, although the audio played fine. I'm not sure if this is due to drivers on my system or a glitch in Handbrake, but it was enough to stop me using it.
Ffmpeg is another command-line tool.
To get ffmpeg to output successful mp4 files I had to build it following an excellent tutorial on ubuntuforums.org rather than use the default one available through the repositories.
Once I installed ffmpeg according to that tute I could use the following shell script to get a beautiful mp4 output file:
# ogv to mp4
# 1-pass CRF encoding using ffmpeg
# Adjust -crf to change the quality. Lower numbers mean higher quality and a larger output file size. A sane range is 18 to 28.
ffmpeg -i "$1" -acodec libfaac -aq 80 -vcodec libx264 -preset slow -crf 25 -threads 0 "tmp-$2"
# qt-faststart makes playback work better on the web
qt-faststart "tmp-$2" "$2"
If you copy and paste that into 'ogv2mp4.sh' you can run it with
./bin/ogv2mp4.sh inputfile.ogv outputfile.mp4
As a bonus this seems about 4x faster than the original mencoder solution. Nice.