How to 2-pass custom-encode video files for portable MP4 video players using FFMPEG on Linux

I regularly download videos from YouTube using “Download Youtube video as MP4/FLV” Firefox plugin (, as I don’t have time to sit through them. I can watch these videos later when I am traveling. However, my portable media player is the type that cannot play just about any format or any screen size. It requires video files to be of QVGA (320×240) resolution and MP4/WMV format. Video bit rates cannot be more than 1000 kbps. Max audio bit rates are I guess 384 kbps. Max frame rate is 30 per second.

On my computer, I have a full-blown FFMPEG installation that can decode and encode anything to anything. ( I even have an extra FFMPEG installation that can convert videos to AMV format for those cheap Chinese players. (

For converting to the portable video player, here were my requirements:

  1. do 2-pass conversion
  2. prompt for video/audio bit rate so that it can match the download file or meet the max supported resolution of the device
  3. prompt for video size
  4. prompt for video frame rate
  5. copy the output file to desktop
  6. wait after conversion attempt so that any errors can be studied
ffmpeg -i "$1"
read -p "Enter video bitrate " iBitRate
read -p "Enter video framerate " iFrameRate
read -p "Enter video size " iVideoSize
read -p "Enter audio bitrate " iAudioBitrate

sOutputFileName=$(basename "$1")
sLogFileName=ffmpeg-`date +%G-%m-%g-%k-%M-%S`
echo $sLogFileName

echo "First pass"
ffmpeg -i "$1" -y -s $iVideoSize -f mp4 -pass 1 -passlogfile "$sLogFileName" -c:v mpeg4 -b:v `echo $iBitRate`k -r:v $iFrameRate -an /dev/null

echo "Second pass...."

ffmpeg -i "$1" -y -s $iVideoSize -f mp4 -pass 2 -passlogfile "$sLogFileName" -c:v mpeg4 -b:v `echo $iBitRate`K -r:v $iFrameRate  -b:a `echo $iAudioBitrate`K  "$sOutputFileName"

rm ~/ffmpeg*.log
read -p "Press Enter to quit" oNothing

The above script resides in a file. I call it via a Nautilus Actions Configuration command. This allows me to right click a downloaded video file and convert it from the context menu. The command opens a Terminal window where I can study the input file’s codecs, frame rate, bit rates, and size.

gnome-terminal -x sh -c "bash ~/c4w.txt ""$1"""

IMPORTANT: Youtube videos have all kinds of characters in their names. Many of these characters are illegal under FAT32 file system used by the video players. Rename the files to avoid problems.

Make a comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s