May 26

Converting Videos for iPod Using FFmpeg


The iPod is an awesome little media player that a lot of people use to play music, but many others also use it to play videos. Unfortunately, though, it is fairly fussy about which video formats it accepts. Here’s how to convert videos to allow them to be played on an iPod (and presumably iPad and iPhone, but I don’t have either of those to test with).

The Tools

The program I’m going to use is called FFmpeg, it’s fairly popular, is often found in the standard repositories on many Linux distributions and can also be used on Windows and Mac OSX.

FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video.

FFmpeg website (http://ffmpeg.org/)

To download FFmpeg, either download it from http://ffmpeg.org/download.html if you’re on Windows, checkout the code from git://source.ffmpeg.org/ffmpeg.git if you want to build it yourself, or install from your repository if you’re on Linux and your distribution has it in the repo. On Fedora use su -c "yum install ffmpeg" on Ubuntu use sudo apt-get install ffmpeg

Convert a Single Video

Converting a single video is pretty simple, open up a terminal and browse to the directory where the video is and run the following command.

ffmpeg -i input.avi -acodec aac -vcodec mpeg4 -s 320x180 -strict experimental output.mp4

After waiting a while, your video should be done.

Converting Multiple Videos

Converting one video is easy enough, but chances are you have more than one video to convert. You could, if you want, just run the command, wait, run it again for the next file, etc.
But, that’s the boring approach. Instead, I wrote a script to automatically iterate through all videos in the directory (.avi in my case, but it can be changed very easily) and convert them one after another.

Here’s the script:

#! /bin/bash
echo "Starting script..."
for f in `ls | grep .avi`; do
	echo "Converting $f"
	ffmpeg -i $f -acodec aac -vcodec mpeg4 -s 320x180 -strict experimental output/${f:0:${#f}-3}mp4
echo "Finished"

Simply save that entire script in a file called convert.sh in the same directory as the videos and change the permissions to allow it to be executed by running chmod a+x convert.sh.
Then, when you’re ready to run the script just type ./convert.sh in the same directory, and wait until they’re all done.

That’s it, have fun converting all your videos!



Skip to comment form

  1. Dragnucs

    Is the -strict option obligatory?

    1. Joe

      When using the aac audio codec, it is yes, because “encoder ‘aac’ is experimental and might produce bad results.”

      If you omit the option, FFmpeg will give you a little message saying this, with the following suggestion: Add ‘-strict experimental’ if you want to use it.

  2. Gigi

    With the latest generation ipod/iphones with their high resolution “retina” display, use -s 640×360 as the video ouput size for better resolution (assuming the source is equal if not larger).

    There are (optional) presets for ipod video conversion that come with ffmpeg. Look under /usr/share/ffmpeg/ and if found, you can add -vpre=

    1. Joe

      Thanks for the tip!

  3. Allen Halsey

    Thanks for the tip.

    Suggestion for your script: use filename expansion put double quotes around the variable expansion:

    for f in *.avi; do
    	echo "Converting $f"
    	ffmpeg -i "$f" -acodec aac -vcodec mpeg4 -s 320x180 -strict experimental "output/${f:0:${#f}-3}mp4"

    See: Bash Pitfalls #1: for i in $(ls *.mp3)

    1. Joe

      Thanks, I’ll give that a read.

      Admittedly I’m not very experienced using bash, I just throw together whatever seems to work!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>