Linux.com

Everything Linux and Open Source

How to convert YouTube videos to DivX or XviD

August 30, 2006 (8:00:00 AM)  -  3 years, 2 months ago

By: Manolis Tzanidakis

YouTube clips are everywhere these days, and I must admit I'm addicted to the site. Nevertheless, I've had a couple of small annoyances with YouTube videos. I wanted to be able to play my favorite ones on standalone DVD/DivX players. I also found I couldn't go backwards or forwards when playing the site's Flash videos with MPlayer. For those reasons, I decided to convert my favorite YouTube videos to DivX or XviD formats. Here's how you can do the same.

Before starting the conversion you need to download the videos to your computer. Youtube-dl is the perfect tool for the job; check Joe Barr's article for more information. If you use Firefox you can also try the VideoDownloader extension. You will need a recent version of MPlayer with Mencoder built with support for Flash Video; if you can play the .flv videos with MPlayer you're good to go.

Now save the following lines as flv2avi.sh. Make the file executable with chmod 755 flv2avi.sh and copy it in a directory in your PATH (/usr/local/bin is a good candidate):

#!/bin/sh

if [ -z "$1" ]; then
  echo "Usage: $0 {-divx|-xvid} list_of_flv_files"
  exit 1
fi

# video encoding bit rate
V_BITRATE=1000

while [ "$1" ]; do
  case "$1" in
    -divx)
      MENC_OPTS="-ovc lavc -lavcopts \
        vcodec=mpeg4:vbitrate=$V_BITRATE:mbd=2:v4mv:autoaspect"
      ;;
    -xvid)
      MENC_OPTS="-ovc xvid -xvidencopts bitrate=$V_BITRATE:autoaspect"
      ;;
    *)
      if file "$1" | grep -q "Macromedia Flash Video"; then
        mencoder "$1" $MENC_OPTS -vf pp=lb -oac mp3lame \
          -lameopts fast:preset=standard -o \
          "`basename $1 .flv`.avi"
      else
        echo "$1 is not Flash Video. Skipping"
      fi
      ;;
  esac
  shift
done

This script converts a list of .flv files to either DivX or XviD, depending on whether you give the -divx or -xvid option, and saves them with their original file name followed by the .avi extension. Usage is very simple: flv2avi.sh -divx first_vid.flv second_vid.flv etc.flv. For more information about the options used on the mencoder commands you can read its man page.

You might also want to create a video DVD with your favorite YouTube videos; check my previous article on the subject.

Read in the original layout at: http://www.linux.com/archive/feature/56642