ffmpeg video converterIn the few previous posts we have been downloading YouTube videos with Awk and Perl. What we ended up with were .flv (Flash Media Video) files which do not play with the usual video players like Windows Media Player which comes with Windows out-of-the-box.

To play these .flv's we either need a special video player which supports this video file format such as VLC Media Player or FLV Player, or get a good video codec pack (which I do not recommend since it eventually messes the whole multimedia subsystem up, not gonna even link to it), or get a ffdshow all-in-one video codec (I recommend it).

The ffdshow solution is pretty good and that is what I use myself together with the excellent Media Player Classic video player which replaces the horrific Windows Media Player once and forever.

Now suppose you wanted to send the video file to your friend who didn't know about all these video codecs and players and didn't want to have them installed. What would you do?

Or suppose you wanted to put the video on your brand new iPhone or video iPod, or your cellphone.

Let's learn how to convert the video to a better format such as windows media video (.wmv), .avi, mpeg or divx, or mobile phone's 3gp format (or even others).

The tool we are going to use is called ffmpeg. ffmpeg is a command line tool to convert one video and audio file format to another.

The official homepage of the tool is at ffmpeg.mplayerhq.hu which is more often down than not. The official download page is here, alternative download here.

I went to the alternative site, downloaded the latest version (ffmpeg.rev9767.7z) and extracted it with WinRar.

The other cool thing about ffmpeg is that it is a command line tool and it's cross platfrom, so you can run it anywhere and it does not need the GUI.

Once you have it downloaded, unpack it and run the tool without any command line options:

usage: ffmpeg [[infile options] -i infile]... {[outfile options] outfile}...
Hyper fast Audio and Video encoder

Main options:
-L                  show license
-h                  show help
-version            show version
-formats            show available formats, codecs, protocols, ...
-f fmt              force format
...

Okay, from the first usage line we now know that to convert a video we will run the tool as following:

> ffmpeg -i our_flash_video.flv ... options ... our_desired_video.avi (or .mpg, or whatever)

Besides this valuable information we also get hundreds of various options which do not yet make much sense to us.
We need documentation of the tool to understand it. Quickly typing 'ffmpeg documentation' into Google we find this ffmpeg documentation page.

Before going into documentation let's first just try converting the video:

> ffmpeg.exe -i youtube_flash_video.flv out.avi
FFmpeg version SVN-r9767, Copyright (c) 2000-2007 Fabrice Bellard, et al.
  configuration: --enable-gpl --enable-pp --enable-swscaler --enable-pthreads --
enable-liba52 --enable-avisynth --enable-libamr-nb --enable-libamr-wb --enable-l
ibfaac --enable-libfaad --enable-libgsm --enable-libmp3lame --enable-libnut --en
able-libogg --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libx
vid --cpu=i686 --enable-memalign-hack --extra-ldflags=-static
  libavutil version: 49.4.1
  libavcodec version: 51.40.4
  libavformat version: 51.12.1
  built on Jul 20 2007 18:03:34, gcc: 4.2.0

Seems stream 0 codec frame rate differs from container frame rate: 1000.00 (1000
/1) -> 15.00 (15/1)
Input #0, flv, from 'youtube_flash_video.flv':
  Duration: 00:01:23.9, start: 0.000000, bitrate: 64 kb/s
  Stream #0.0: Video: flv, yuv420p, 320x240, 15.00 fps(r)
  Stream #0.1: Audio: mp3, 22050 Hz, mono, 64 kb/s
Output #0, avi, to 'out.avi':
  Stream #0.0: Video: mpeg4, yuv420p, 320x240, q=2-31, 200 kb/s, 15.00 fps(c)
  Stream #0.1: Audio: mp2, 22050 Hz, mono, 64 kb/s
Stream mapping:
  Stream #0.0 -> #0.0
  Stream #0.1 -> #0.1
Press [q] to stop encoding
frame= 1259 fps=219 q=2.0 Lsize=    2780kB time=83.4 bitrate= 272.9kbits/s
video:2050kB audio:652kB global headers:0kB muxing overhead 2.860901%

Hey, wow! It worked, we just converted our .flv to an mpeg4 video! Also the sound got converted from mp3 to mp2.

We would like to have more control over these parameters, right?

Let's go through documentation and find how to change audio and video bitrate, video framerate, video and codecs and output video size.
For various video options there is a section in documentation called 'Video Options', there we find that '-b bitrate' option changes video bitrate, '-s wxh' changes video width and height, '-vcodec codec' changes the video codec and '-r framerate' changes the video frame rate.

The audio options are listed here and we find that to change the audio bitrate we need to specify '-ab bitrate' and '-acodec codec' to change the audio codec.

The other interesting option is '-formats' which lists the supported codecs and protocols.

The videos we get from YouTube are usually 320x240 in size and at 15 fps with 64kbit/s mp3 sound.

If we did not specify the vid size, framerate and other parameters the input video's ones would be used for output video.

Just to illustrate how to use those command options, let's convert a YouTube video to 3gp format for viewing on a mobile phone. What is this 3gp format? Looking in wikipedia for 3gp we find that this format stores video streams as MPEG-4 Part 2 or H.263 and audio streams as AMR-NB, AMR-WB, AMR-WB+ or AAC-LC.
Also it turned out that H.263 can only take a number of video sizes which are listed on this page.
All we have to do is just to specify the right command line flags - and voila!

> ffmpeg.exe -i youtube_flash_video.flv -r 15 -b 200kbit/s -s 176x144 -vcodec h263 -ar 8000Hz -ab 10.2k -acodec libamr_nb for_mobile.3gp

Yeap, it worked, I uploaded the file to my Nokia N73 and it played perfectly!

To convert it to DivX and leave all the other parameters as is, use the following simple command line:

> ffmpeg.exe -i youtube_flash_video.flv -vcodec mpeg4 divx_youtube_movie.avi

To convert to other formats, just look at the supported encoding formats via '-formats' command line option. For example, to convert a video to .wmv, you would specify '-vcodec wmv2' or '-vocdec wmv1', etc.

Now we know how to download youtube videos and how to convert them. It seems that a perfect time has come to create a free, quick and cool desktop software application which will do this job for us.

Thanks for reading and until next time!