Video Conversion
From
Contents |
Video Conversion
Introduction
There are lots of video and audio codecs and lots of methods and preferences for converting between formats. These are only some basic examples. A good deal of trial and error is often required for successful video conversion.
- Mencoder and ffmpeg are the two packages that are the workhorses of video conversion. Of these, mencoder is faster and generally gives better results.
Mencoder
Mencoder is part of the MPlayer set of libraries (that also uses several of the FFMPEG libraries) for audio/visual conversion. If it is not installed on your system, install it:
sudo apt-get install mencoder
MP4 with AAC audio to AVI with Xvid / MP3
- AAC codecs are proprietary, of course, and many DVD players do not accept it for this reason. Xvid is the open source version of DivX, and of course MP3 is a widely available audio format.
- The AVI container only allows a constant bitrate, so the MP3 audio must be encoded at CBR. If the AAC is 5.1, it will be downcoded to stereo for MP3.
- This example is a two-pass technique that allows the file size to be specified and quality optimized for that filesize (using the information generated in the first pass). In this example, a 700 Mb file is desired (and is specified by the negative value).
This information is from the Gentoo Wiki for Xvid and mencoder.
mencoder <input.mp4> -ovc xvid -oac mp3lame -lameopts cbr:br=192 -xvidencopts pass=1 -o /dev/null mencoder <input.mp4> -ovc xvid -oac mp3lame -lameopts cbr:br=192 -xvidencopts pass=2:bitrate=-700000 -o <output.avi>
AVI to MPG
- The MPG format is sometimes useful for creating DVDs (using the MPEG-1 or MPEG-2 video codec, which can be then used for vob files using QDVDAuthor or ToVid). If the audio codec of the AVI file is already AC3 or MP3, it usually can be copied. This example is take from the MPlayer/Mencoder documentation. Example:
mencoder <input.avi> -of mpeg -ovc lavc -lavcopts vcodec=mpeg1video -oac copy -o <output.mpg>
Add subtitles to video
- Using mencoder:
mencoder -ovc [codec] [codec opts] -oac copy -sub [sub file.srt] -subfont-text-scale [3 normally]
In the example above, this would be:
mencoder <input.mp4> -ovc xvid -oac mp3lame -lameopts cbr:br=192 -xvidencopts pass=2:bitrate=-700000 -sub <subtitles.srt> -subfont-text-scale 3 -o <output.avi>
- Note: When adding subtitles to an .AVI video, you must transcode it completely. It is not sufficient to merely add the subtitle track as listed above -- the entire video must be re-transcoded. So, for example:
mencoder <input.avi> -ovc xvid -oac mp3lame -lameopts cbr:br=192 -xvidencopts pass=1 -o /dev/null mencoder <input.avi> -ovc xvid -oac mp3lame -lameopts cbr:br=192 -xvidencopts pass=2:bitrate=-1400000 -sub <subtitles.srt> -subfont-text-scale 3 -o <output.avi>
Remove subtitles from an .MKV video
Mastroska container (.MKV) video files can have multiple subtitles included. If a conversion to an .AVI container format is desired (for playing on an older DVD player, for example), the default subtitle file is hardcoded into the converted .AVI file, which may be undesirable. To overcome this behaviour (so that the converted .AVI has no subtitles), use the -sid 999 option:
mencoder <input.mkv> -sid 999 -ovc xvid -oac mp3lame -oac mp3lame -lameopts cbr:br=192 -xvidencopts pass=1 -o /dev/null mencoder <input.mkv> -sid 999 -ovc xvid -oac mp3lame -oac mp3lame -lameopts cbr:br=192 -xvidencopts pass=2:bitrate=-1400000 -o <output.avi>
Trim a video
- Using mencoder:
mencoder <input.avi> -ovc copy -oac mp3lame -ss 01:57:12 -endpos 00:04:08 -o <output.avi>
where -ss indicates the start position of the clip (hh:mm:ss) and -endpos indicates how long the clip should be. (I use mp3lame for the audio codec because YouTube accepts that.)
Resize a video
- Using mencoder:
mencoder <input.avi> -ovc xvid -vf scale=320:240 -oac mp3lame -lameopts cbr:br=192 -xvidencopts pass=2:bitrate=-1400000 -o <output.avi>
where -vf scale=320x240 indicates that the resulting video should be of that size. The position of the suboption in the command string is important. It should immediately follow the option -ovc <vcodec>.
- HDTV resolution is usually 1920 x 1080 ("1080p") or 1280 x 720 ("720p"). A standard definition widescreen TV has a maximum height of "480p" (usually 853 x 480 but sometimes 720 x 406). The standard width:height aspect ratio for cinema is 1.85:1, whereas the average aspect ratio for widescreen movies distributed for display on television is 16:9 (1.78:1). When resizing a video, it is good to know the original dimensions of the video and maintain the width to height aspect ratio in the chosen scale.
- Example: A video is distributed as 1280 x 692 (which has an aspect ratio of 1.85:1). The device (a low resolution television) on which it is to be displayed has a maximum width of 720. The desired resolution would then be 720 x 390 to keep the aspect ratio at approximately 1.85:1. The option would then be -vf scale=720:390. An analog television would require 10% overscan, making the maximum width 648. To keep an aspect ratio of 1.85:1 would require a resolution of 648 x 350, or a scale option of -vf scale=648:350.
- Example: An HQ video is distributed as 1920 x 1080 (which has an aspect ratio of 16:9). It is desired to view the video on a television with a maximum width of 720p, which would require a final resolution of 720 x 406 to maintain an aspect ratio of 16:9. The scale option would be -vf scale=720:406.
- Example: An HQ video is distributed as 1920 x 1080 (which has an aspect ratio of 16:9). It is desired to view the video on an analogue television with 10% overscan, which would require a final resolution of 648 x 364 to maintain an aspect ratio of 16:9. The scale option would be -vf scale=648:364.
Convert to .MP3 audio file
(Under construction)
- Use Mplayer to extract audio to pcm .wav file:
mplayer <input.avi> -vc null -oa pcm -aofile -ss 1441.4 -endpos 260.1 <output.wav>
- Then convert the .wav file to .mp3 with your favourite converter (such as SoundConverter).
FFMPEG
FFMPEG is the swiss-army knife of video and audio format conversion. It succeeds when no other program can. It is free and open source. If it not yet installed on your system as part of another package (it is used by many video/audio editors), then install it:
sudo apt-get install ffmpeg
- To convert many different formats, read the FFMPEG documentation. Also see this tutorial.
Flash video (.flv) to MPG-2 using FFMPEG
- To convert a saved Flash video (.flv) to an MPEG-2 format playable on a DVD, convert:
ffmpeg -i samplevideo.flv -target ntsc-dvd samplevideo.mpg
- Then use K3b (or Gnomebaker) to write the mpg file to a New DVD Data Project.
- For PAL use -target pal-dvd. For widescreen, use -target film-dvd. For other conversion tips, see this forum. (Note: Most Flash video has very low resolution, with a screen size of 360x270, for example. You may see a slight diminishment in resolution if you wish to convert it to 720x480 (which is the NTSC standard size) or other screen size. You can keep the original screen size and resolution by omitting the -target parameter.) If your original file is 16:9 widescreen and you desire a 4:3 letterbox output for playing on an overscanned TV, you may need to pad the file so that the widescreen is not compressed (see this forum):
ffmpeg -i samplevideo.flv -target ntsc-dvd -s 648x364 -padleft 36 -padright 36 -padtop 58 -padbottom 58 samplevideo.mpg
- You can also use the WinFF GUI and add the command (as above) as a "Preset," for subsequent use. For example:
- Video converter (WinFF) -> Edit -> Presets ->
- Preset Name: Letterbox -> Preset Label: 16:9 Widescreen to 4:3 Letterbox
- Preset command: -target ntsc-dvd -s 648x364 -padleft 36 -padright 36 -padtop 58 -padbottom 58
- Ouput file extension: mpg -> Category: DVD
- -> Add/Update -> Save
- To convert to MPEG-4 (mp4) files, use
ffmpeg -i samplevideo.flv outputvideo.mp4
- FFMPEG requires that multiple restricted extra codecs be installed. This can be done in a single easy step from the command-line Terminal:
sudo apt-get install kubuntu-restricted-extras
- or
sudo apt-get install ubuntu-restricted-extras
Convert to .MP3 audio file using FFMPEG
Convert Flash video audio to mp3
- Once you have downloaded flash video content (.flv) from the Internet (using the Video Download Helper plug-in for Firefox, for example), the audio component can be converted to an mp3 using this command (from the command line Terminal). (This will work for any type of video file, not just Flash.)
ffmpeg -i nameofvideoclip.flv -ab 160k -ac 2 -ar 44100 -vn nameoffile.mp3
- where -i indicates the input, -ab indicates the bit rate (in this example 160kb/sec), -vn means no video ouput, -ac 2 means 2 channels, -ar 44100 indicates the sampling frequency. See FFMPEG docs for more info.
If I only want a segment of the video to be converted, I can use the time markers:
ffmpeg -i nameofvideoclip.flv -ss 00:00:09 -t 00:03:00 -ab 160k -ac 2 -ar 44100 -vn nameoffile.mp3
- where -ss 00:00:09 indicates the point in the video (hh:mm:ss) at which to start conversion and -t 00:03:00 indicates the amount of time (from the start point) to convert.
- As long as FFMPEG is already installed, the Video DownloadHelper plug-in for Firefox already has an option to automatically convert an online video (such as those found at YouTube) into an .MP3 file. (Settings are adjustable.) From the DownloadHelper icon in Firefox, highlight the video to convert, then
- DownloadHelper icon -> Download and Convert -> Converter options: MP3
Edit/convert screencapture with FFMPEG
Note: This section under construction.
- Note: I now recommend using mencoder for all video conversion techniques. It uses some of the ffmpeg libraries but is faster and gives more reliable and high-quality results.
- This is only one example of a wide variety of techniques. Once I have a captured video, I want to convert it to XVID video (which is the format my older DVD player accepts) and MP3 audio (mp3lame), which I will place in an AVI container (which my DVD player also accepts).
ffmpeg -i Punchcast1.avi -vcodec mpeg4 -vtag xvid -acodec libmp3lame -ss 00:00:09 -t 00:03:00 Punchcast2.avi
I will start conversion (-ss) at second 9 (to eliminate unimportant things at the beginning) and convert 3 minutes (-t) of video (00:03:00).
- I happen to watch my screencasts on my old-fashioned 4:3 television. To do that, I make a letterboxed video:
ffmpeg -i Punchcast1.avi -vcodec mpeg4 -vtag xvid -ss 00:00:09 -t 00:03:00 -s 648x364 -padleft 36 -padright 36 -padtop 58 -padbottom 58 -acodec libmp3lame Punchcast3.avi
My laptop screen is 1366x768, which I reduce to a size of 648x364. My TV wants 720x480, so I pad the sides and top/bottom. Why not a width of 720 initially? My older television has 10% overscan, which cuts off 10% of the video. I therefore use (at least) 10% padding on the edges.
In newer versions of FFMPEG, the padding (and many other) options have changed. The proper command is now:
ffmpeg -i Punchcast1.avi -vcodec mpeg4 -vtag xvid -ss 00:00:09 -t 00:03:00 -s 648x364 -vf pad 720:480:36:58 -acodec libmp3lame Punchcast3.avi
ffmpeg movie=Punchcast1.avi:seek_point=9 -vcodec copy -acodec libmp3lame Punchcast1f.avi
WinFF (FFMPEG GUI)
WinFF is a free, GPL-licensed open source GUI frontend for FFMPEG. Install:
sudo apt-get install winff xterm
Run:
- Menu -> Applications -> Sound & Video -> WinFF
VobSub2SRT (Convert subtitles from .sub/.idx to .srt)
- VobSub2SRT is a simple (GPLv3-licensed) command line program to convert the image-based .idx / .sub subtitle files (used with the .vob format found on commerical DVDs) into text-based .srt text subtitle files by using OCR. It is based on code from the MPlayer project, Tesseract as OCR software, and libavutil (part of the FFmpeg project).
- Install dependencies:
sudo apt-get install libavutil-dev libtiff4-dev tesseract-ocr tesseract-ocr-dev tesseract-ocr-eng sudo apt-get install pkg-config build-essential cmake
- If you will be converting subtitles in languages other than English, you must install tesseract for any or all of those languages as well:
sudo apt-get install tesseract-ocr-vie tesseract-ocr-deu tesseract-ocr-fra tesseract-ocr-ita sudo apt-get install tesseract-ocr-nld tesseract-ocr-spa tesseract-ocr-por tesseract-ocr-deu-f
- where vie is for Vietnamese, deu is for German, fra is for French, ita is for Italian, nld is for Dutch, spa is for Spanish, por is for Portugeuse, and deu-f is for German Fraktur script. If you don't you will get an error of the type: Unable to load unicharset file /usr/share/tesseract-ocr/tessdata/xxx.unicharset.
- Download and unzip the VobSub2SRT .zip file into its own directory:
mkdir vobsub2srt cd vobsub2srt wget -O vobsub2srt-current.zip https://github.com/ruediger/VobSub2SRT/zipball/ca53a18108eb08d6e2b853643d8c6838e2489823 unzip vobsub2srt-current.zip rm vobsub2srt-current.zip
- This will create a subdirectory with the current version. For example, my version is vobsub2srt/ruediger-VobSub2SRT-ca53a18. Change into that directory then compile and install the program.
cd ruediger-VobSub2SRT-ca53a18 ./configure make sudo make install
- This should install the program vobsub2srt to /usr/local/bin. You can uninstall vobsub2srt with sudo make uninstall. You can build a *.deb package (Debian/Ubuntu) with make package. The package is created in the build directory.
- Convert the .sub / .idx pair of subtitle files (named Filename.sub and Filename.idx) into a .srt sbutitle file (named Filename.srt):
vobsub2srt Filename
- where Filename is the file name of the subtitle files WITHOUT the extension (.sub / .idx).
- If there are multiple languages in the .sub / .idx pair of subtitle files, you can select which language to convert (using the 2-letter ISO 639-1 language code, e.g. en, fr, de, it, es, pt, etc.):
vobsub2srt --lang en Filename
- Edit the .srt subtitle file for OCR mistakes (use the gedit text editor instead of kate if using Ubuntu instead of Kubuntu):
kate Filename.srt
Join .MPG video segments
Individual video segments (MPEG-2, for example) can easily be joined:
cat samplevideo1.mpg samplevideo2.mpg samplevideo3.mpg > samplevideo123.mpg
- You can then write the resulting MPEG-2 file to a DVD and play it in most DVD players.
Split a file into segments
Any file can be split into segments using the Linux command:
split -b 1440k my_big_file
which will split my_big_file into equal segments of size 1440 kb.
BlogMarks
del.icio.us
digg
Fark
Furl
Newsvine
reddit
Segnalo
Simpy
Slashdot
smarking
Spurl
Wists
