HOWTO Convert video files

From MattWiki
Jump to: navigation, search

Converting video formats between common file formats and codecs can easily be done from the command line. There are many good reasons to convert video files you made yourself - or video files forwarded to you which you plan to distribute.

Good reasons to change a video file can be:

  • Playability
  • Size

This howto will to introduce to you some of the basic commands to use the most common Linux command-line program to convert video files from one format to another.

File-size matters

Size is important, specially when publishing video on the web. A videofile reduced by 100 MB will save you 100 GB of bandwidth if 1000 people download it; obviously the reduction of 100 MB pr. file will save you 1 GB of harddrive space if you host 10 video-files on a web-server.

Thus; You will likely want files to be as small as possible without reducing the video quality

Playability

HTML4 can use three formats, webm, ogg video and mp4. The HTML5 video tag allows you to provide the same file in multiple formats. Supplying both ogv and mp4 lets almost everybody play your video file. Those using this MSIE thing will not be able to play mp4. Those users are mostly braindead zombie-like sheep and you may think that these non-thinking visitors do not matter, but they do, all visitors matter regardless of them being braindead.

The Basic Tools

The basic GNU/Linux tools you will want to learn are: mplayers mencoder and ffmpeg. Both are powerful, ffmpeg is the l33t shit you'll enjoy most.

HTML 5

ffmpeg2theora uses ffmpeg and you can also (ab)use ffmpeg directly. The trick is to use -f ogg, -vcodec libtheora and -acodec libvorbis like this:

Shell command:
ffmpeg -i inputfile.dv -f ogg -vcodec libtheora -b 800k -g 300 -acodec libvorbis -ab 128k outputfile.ogv

ffmpeg2theora is typically used like this:

Shell command:
ffmpeg2theora --optimize --audioquality 4 --videoquality 9 $*


Basics for making other formats

Mencoder:

input_filename.mpg
-ovc codec for video output
-oac codec for audio output
-o output_filename.avi
mencoder <filename.avi> -ovc lavc -oac lavc -o <output.avi> 

Transcode:

-i input_filename.mpg

-y codec for audio/video output
-o output_filename.avi
transcode -i movie.mpg -o movie.avi -y divx

These are just the basics. There are many more advanced options for choosing codecs, bitrate and so on for both these tools. dad

XviD Encoding

Using MPlayer (mencoder)

You can encode XviD using one or two passes of the original file. You will get better quality if you use two-pass encoding, but does take longer to encode the video.

One-Pass Encoding

You can only set the bitrate or fixed_quant using one-pass encoding. You can only choose bitrate if you do two-pass encoding.

You must set one of these when encoding XviD using mencoder.

By setting bitrate

  • Using bitrate will encode the video to a constant bitrate (CBR).
  • Set bitrate=<value>
  • Higher = better quality with larger files, and
  • lower = less quality with smaller files.
  • A bitrate of 800 will give you near DVD quality when encoding hi-quality video like a DVD disc.
  • VCD quality is around 400-500.
  • Internet streaming quality is generally around 80-150.
  • Mencoder's default bitrate is 687 kbits/s.
  • You need about ~800kb/s to get high quality on a 720x480 sized movie.

Encoding example by setting the bitrate:

mencoder <filename.avi> -ovc xvid -oac mp3lame -xvidencopts bitrate=687 -o <output.avi>
Setting the audio bitrate

You may also want to change the audio bitrate for the audio encoded using mp3lame when you are making a XviD file with the goal of making a small file using -lameopts abr:br=<audiobitrate>. For example, an audio bitrate of 92 (-lameopts abr:br=92) will do nicely for a video bitrate of 150.

mencoder <filename.avi> \
   -ovc xvid \
   -oac mp3lame \
   -lameopts abr:br=92 \
   -xvidencopts bitrate=150 \
   -o <output.avi>

By bitrate calculated by a fixed file-size

You can set the filesize you want in KiloBytes in order to make mencoder calculate the bitrate for you. This can be done by setting a "negative" bitrate, bitrate=-700000 will give you a movie file which fits nicely on a CD.

mencoder <filename.avi> -ovc xvid -oac mp3lame -xvidencopts bitrate=-700000 -o <output.avi>

By setting a fixed quality

fixed_quant can also be used when doing one-pass encoding.

  • fixed_quant=<1-31> specifies what "quality level" you want and will make mencoder automatically try to obtain the best quality/size ratio.
  • 1 is the highest quality, and will result in very large file if you use it.
  • 31 is the lowest, and the video will look very poor.
  • 4 and 5 are good setting for high-quality video. 1, 2 and 3 will give you very little improvement compared to 4, but give you way bigger files.(example:1 is 20mb if 4 is 5mb)
mencoder <filename.avi> -ovc xvid -oac mp3lame -xvidencopts fixed_quant=4 -o <output.avi>

Two-Pass Encoding

nice -n 19 mencoder file.wmx -ofps 23.976 -oac mp3lame -lameopts abr:br=92 -ovc xvid -xvidencopts pass=2:bitrate=150 -o audiofix-150bit-xvid.avi



Windows Media files (.wmv/.asf/.wmx)

mencoder can convert wmv files. However, it seems that files created with mencoder from .wmv can not be played in many mediaplayers, namely xine. Adding -ofps 23.976 to the command-line makes files made with mencoder from .wmv play in xine.

mencoder infile.wmv -ofps 23.976 -ovc lavc -oac copy -o outfile.avi'

xvid from .wmv or .wmx, using two-pass xvid encoding:

mencoder file.wmx \
   -ofps 23.976 \
   -oac mp3lame \
   -ovc xvid \
   -xvidencopts pass=1 -o /dev/null
mencoder file.wmx \
   -ofps 23.976 \
   -oac mp3lame \
   -ovc xvid \
   -xvidencopts pass=2:bitrate=250 \
   -o 250bit-xvid.avi

The audio track on Windows Media files are sometimes low. You can use midentify to get information about a video file.

midentify file.wmx
ID_AUDIO_BITRATE=64064

With a bitrate of 64k, add -lameopts abr:br=64 when you encode a divx:

mencoder file.wmx -ofps 23.976 \
   -oac mp3lame \
   -ovc xvid \
   -xvidencopts pass=1 \
   -o /dev/null
mencoder file.wmx \
   -ofps 23.976 \
   -oac mp3lame \
   -lameopts abr:br=64 \
   -ovc xvid \
   -xvidencopts pass=2:bitrate=250 \
   -o 250bit-xvid.avi

Storage space is simply wasted if you encode the audio track on the new video file with a higher bitrate than the audio bitrate on the original track.


Convert a video to xVid format:

transcode -i movie.mpg -o movie.avi -y xvid

divx

Using transcode

-i ((input file) -o (output) -y (format - xvid/divx)

transcode -i movie.mpg -o movie.avi -y divx

Video-CD

Using FFMPEG

ffmpeg can make VCD mpeg video files using -target where the target can be "vcd", "svcd", "dvd", "dv", "pal-vcd", "ntsc-svcd". These switches will set the output format options (bitrate, codecs, buffer sizes) automatically.

The default vcd switch makes a PAL vcd.

ffmpeg -i myfile.avi -target vcd /tmp/vcd.mpg

A ntsc vcd:

$ ffmpeg -i myfile.avi -hq -target ntsc-vcd /tmp/vcd.mpg

Same example using same quality as source:

$ ffmpeg -i myfile.avi -sameq -target vcd /tmp/vcd.mpg

Converting a file for VCD format using a and b frames for MPEG 2:

$ ffmpeg -i myfile.avi -target ntsc-vcd -bf 2 /home/user/Video/vcd.mpg

Flash video (flv)

ffmpeg is the best choice for making flash video sites (.flv files). The syntax is simple:

ffmpeg -i inputfile.avi flashfile.flv

A parameter you likely want is -ab, audio bitrate. The default is 64, which may be fine for home videos, but is is too poor if you want to promote your local talented singer in a music video. Example:

ffmpeg -i musicvideo.mpg -ab 256 Som_du_visnar2.flv

Also note that .flv files do not support a samlerate of 48000, but it will convert using the original file's rate anyway and fault on exit. Flash video can have a sample rate of 44100, 22050 and 11025. Set the audio sampling frequency with -ar freq (usually -ar 44100) if the original file has something other than the valid rates.

Options you may want to consider:

-ab (number)     # audio bitrate
-ar freq         # audio sampling frequency
-b video bitrate # defaults to 200, which is kind of low

You should also be aware of -sameq. This makes the video the same quality as the original, but tends to make flash video files twice the size as original mpeg's. This is bad when it comes to serving files, but it may be worth it if you really want your flash video to have high quality (and you have the bandwidth..).

DVD

Using FFMPEG

ffmpeg can also make DVD video files using -target, where the target can be "vcd", "svcd", "dvd", "dv", "pal-vcd", "ntsc-svcd".

ffmpeg -i myfile.avi -target ntsc-dvd /tmp/dvd.mpg