Converting Cell Phone Videos

Background

Recently, I started having problems with the A/V sync on my webcam. I debated and shopped around for a video camera, but ultimately decided that I could use the money on something else. I realized that I can use my cellphone as a video camera instead.

This worked for a while until I started experimenting with other video players on my PC. The sound on my files was nothing but loud static in most of them. The problem is that my phone uses a codec that isn't extremely common yet: 3GPP. I decided to convert my videos to a more common format.

After seeing which players worked and didn't work, I determined that the problem was with gstreamer. However, the ffmpeg back end for players such as mplayer worked well. That's really fortunate. ffmpeg has not only the ability to convert media files, but its syntax is simple as well. (For the record, avidemux did not work.)

I don't have a tremendous number of video files, but I didn't want to sit there converting them all one-by-one. Shell scripting to the rescue!

#!/bin/sh

while [ "$1" ]
do
    ffmpeg -i "$1" -f avi -vcodec libxvid -acodec libmp3lame "$1.avi"
    shift
done

This will convert the media to use the xvid video codec with the mp3 audio codec in an avi container. All three options are very portable. I named it "converttoxvid". Remember to run

chmod u+x convert_to_xvid

to make the script executable. Note that this script will work for any video file mplayer/ffmpeg can play, not just 3GPP files. And mplayer can pretty much play anything you may have.

How it works

This is a very basic shell script. The "while" statement checks to make sure that a file name was given at the command line. "$1" is the first parameter after the command name i.e. the first parameter. If there is a file name, it enters the do loop.

The "ffmpeg" statement converts the video. The output file name is the same as the input file name with ".avi" appended.

The "shift" statement drops the first parameter from the parameter list, shifting all of the others. That means that "$1" is now the second parameter in the list.

The script loops for each file name passed until it is done and exits.

Using the script

Because I put the script in my local "bin" folder which is in my PATH, I don't need to worry about which directory I'm in when I execute this. To convert a cell phone video, I go to the directory where I have the video and run

convert_to_xvid video1.3gp

The script loops, so I can convert more than one file at a time.

convert_to_xvid video1.3gp video2.3gp

The reason I used quotes around the $1 variable in the script is to deal with videos with spaces in their names. So this works:

convert_to_xvid "Video with spaces.3gp" Another\ Video.3gp

The shell expands wildcards before passing parameters to the script, so I can use it this way:

convert_to_xvid *.3gp

That's the way I use it.

Finally, if a few video files are selected in KDE, GNOME, etc. and dragged onto the script file, the script is called with the dragged files as parameters. This means you don't need the command line at all.

The file will appear in the same directory as the original file. For my files, the output file was typically 50% to 80% smaller than the input file while still having very clear video and audio.

Why I chose these codecs

I really like xvid, which stated as an open-source alternative to DivX, hence the name. It gives a high compression rate at very good quality; I think it's even better than DivX, although I haven't run any tests myself to back that up. I saw no noticeable difference in the videos that I converted.

I like mp3 audio. It is high compression at good quality. aac audio is supposedly better, but the file sizes are larger and the encoder doesn't come with every distro of Linux by default.

The avi container is almost ancient. I remember using avi files on Windows 3.1. There are minor limitations to it due to its age, such as a 4 GiB file size limit, but these restrictions don't really apply to anything I due. I like the mkv container more, but most of the Windows machines I know can't use it by default. I wanted something that works instantly for everyone. I didn't use the mp4 container because mp3 audio is not supported.

Also, the H.263 and H.264 standards are nice. A lot of video files are encoded with them now. The problem is the horsepower required to play them and their file sizes. My computer is 8 years old. I drop frames when I play a video that uses these standards if the video resolution is too high. It may be a better option for some people, but not in my particular case.