Hack63.Convert from One Video Format to Another


Hack 63. Convert from One Video Format to Another

With transcode you can convert to and from many popular video formats and perform other video manipulation all from the command line.

They say that the problem with standards is that there are so many to choose from. This is definitely true when it comes to video formats. It would be nice to store all video just as it was recorded, but the fact is it would take up more space than many people have available on their computers. As a result, a number of different lossy compression formats have been developed to try to reduce the file size for video while still maintaining reasonable quality. There is also a battle raging between various companies such as Microsoft, Apple, and Real each with their own competing video formats. Plus, as the technology improves, new revisions of old formats appear. The end result is a jumble of MPEG1, MPEG2, DivX, ASF, XVID, and other video formatsall different from one another.

When wading through these choices, often you find you need to convert a video from one format to another. There are a number of special-purpose tools that excel at one particular formats or group of formats, but with transcode you have a single conversion tool you can apply to all of the various video files you might have. Transcode not only supports converting to and from a variety of formats, it also can perform cropping and resizing [Hack #68].

The first step is installing transcode. Because of the number of formats transcode supports, in general it's simpler to use a precompiled binary for your distribution. Most major distributions offer transcode either as part of the distribution or from third-party packagers. These are preferred since you can grab all of the library dependencies from the same place. To install from source, download the latest version from http://www.transcoding.org and follow the instructions at http://www.transcoding.org/cgi-bin/transcode?Building_Transcode.

The most important aspect of installation from source is the options you pass to the configure script. A majority of the useful import and export plug-ins, including ogg, mjpegtools, libquickime, libdv, and others, aren't enabled by default in the source (while usually they are enabled in prepackaged binaries). The number of arguments to pass configure are too numerous to list here, but check the install page linked above for the list of options that aren't enabled by default.

With transcode installed, the next thing to consider is which video format you wish to convert to. The transcode manpage lists all of the different video and audio export modules in the manpage under the -y option. Some export modules are only for video or for audio. In those cases, the manpage will list null next to the video or audio section of the description.

To see what modules your transcode install has, type:

 $ ls -1 `tcmodinfo -p`/export*.so /usr/lib/transcode/export_ac3.so /usr/lib/transcode/export_af6.so /usr/lib/transcode/export_debugppm.so /usr/lib/transcode/export_divx5raw.so … /usr/lib/transcode/export_xvid2.so /usr/lib/transcode/export_xvid3.so /usr/lib/transcode/export_xvid4.so /usr/lib/transcode/export_xvid.so /usr/lib/transcode/export_yuv4mpeg.so $ 

Table 3-3 lists some of the more popular video and audio export modules.

Table 3-3. Popular transcode video and audio export modules

Module name

Video codecs supported

Audio codecs supported

Description

ac3

N/A

AC3

Uses ffmpeg to encode audio to AC3.

divx4

DivX 4.xx

MPEG/AC3/PCM

Encodes MPEG4 video using the closed-source binaries from divx.com.

divx5

DivX 5.xx

MPEG/AC3/PCM

Encodes MPEG4 video using the closed-source binaries from divx.com.

dv

Digital Video

MPEG/AC3/PCM

Encodes DV into an AVI container. DV is a codec used in digital camcorders.

lame

N/A

MPEG1/2

Encodes audio to MPEG layer III (MP3) using LAME.

mjpeg

Motion JPEG

MPEG/AC3/PCM

Encodes MJPEG-based AVI files.

mp1e

MPEG1 video

MPEG1-Layer

Encodes into MPEG1 video and can encode VCD-compliant streams.

mp2

N/A

MPEG1/2

Writes an MP2 (MPEG1-Layer2) audio file.

mpeg2enc

MPEG1/2

N/A

Uses mpeg2enc to encode MPEG1, VCD, SVCD, MPEG2, and DVD videos.

ogg

N/A

Ogg

Encodes audio to Ogg Vorbis.

pcm

N/A

PCM

Encodes audio to PCM format.

raw

N/A

MPEG/AC3/PCM

Does no encoding and instead processes raw streams. Used also for pass-through mode.

xvid4

XviD 1.0.x

MPEG/AC3/PCM

Encodes MPEG4 video using xvid.org encoding libraries. The "xvid" option often is a symlink to this library.


3.18.1. Simple Example: Encode a Video to XviD

One of the easier examples for transcode is to turn a video (such as an .mpg file) into XviD format. XviD is a particular set of MPEG4 encoding libraries that provide high quality encodes with small file sizes. transcode has many different options you can use to tweak its outputso many it's easy to get lost in them all. For the purposes of this hack I'm going to try to limit it down to some essential arguments that you need to convert video between formats.

Many examples you see on the Web show people passing transcode import format options, but this isn't necessary, and if you omit the import format option, transcode will scan the video and figure it out for itself. I've found that for my videos, transcode is good at figuring it out, and one less option on the command line makes things easier to read and understand. So, to convert a sample MPEG file to an XviD AVI, type:

 $ transcode -i  sample.mpg  -o  output.avi  -y xvid  transcode v0.6.14 (C) 2001-2003 Thomas Oestreich, 2003-2004 T. Bitterberg [transcode] (probe) suggested AV correction -D 0 (0 ms) | AV 0 ms | 0 ms [transcode] auto-probing source sample.mpg (ok) [transcode] V: import format | MPEG (V=mpeg2|A=mp3) [transcode] V: AV demux/sync | (0) sync AV at PTS start - demuxer disabled [transcode] V: import frame | 352x240 1.47:1 encoded @ 4:3 [transcode] V: bits/pixel | 0.711 [transcode] V: decoding fps,frc | 29.970,4 [transcode] V: Y'CbCr | YV12/I420 [transcode] A: import format | 0x50 MPEG layer-2 [44100,16,2] 224 kbps [transcode] A: export format | 0x55 MPEG layer-3 [44100,16,2] 128 kbps [transcode] V: encoding fps,frc | 29.970,4 [transcode] A: bytes per frame | 5884 (5885.880000) [transcode] A: adjustment | 1880@1000 [transcode] V: IA32 accel mode | sse2 (sse2 sse mmxext mmx asm C) tc_memcpy: using mmxext for memcpy [transcode] V: video buffer | 10 @ 352x240 [import_mp3.so] v0.1.4 (2003-08-04) (audio) MPEG [import_mpeg2.so] v0.4.0 (2003-10-02) (video) MPEG2 [export_xvid4.so] v0.0.5 (2003-12-05) (video) XviD 1.0.x series (aka API 4. 0) | (audio) MPEG/AC3/PCM … 

As transcode processes the file, it will indicate how many frames per second it is processing and how far along in the video it is, so you can track its progress.

By default transcode will encode XviD files at 1,800 bps. If you want to change that to a larger or smaller value, use the -w option followed by the bps, so to encode to an XviD like in the previous example, only at 1,500 bps, type:

 $ transcode -i  sample.mpg  -o  output.avi  -y xvid -w 1500  


Notice that, by default, transcode used MPEG layer-3 for the audio, since I didn't specify an audio format to use. If you want, you can change that. For instance, to create an XviD with Ogg Vorbis audio, you pass transcode an audio export module as well as a video export module. In this case you also need to provide transcode with a filename to save the audio track to with the -m argument, so that you can combine it and the video later on:

 $ transcode -i  sample.mpg  -o  output.avi  -m  output.ogg  -y xvid,ogg  transcode v0.6.14 (C) 2001-2003 Thomas Oestreich, 2003-2004 T. Bitterberg [transcode] (probe) suggested AV correction -D 0 (0 ms) | AV 0 ms | 0 ms [transcode] auto-probing source sample.mpg (ok) [transcode] V: import format | MPEG (V=mpeg2|A=mp3) [transcode] V: AV demux/sync | (0) sync AV at PTS start - demuxer disabled [transcode] V: import frame | 320x240 1.33:1 encoded @ 1:1 [transcode] V: bits/pixel | 0.782 [transcode] V: decoding fps,frc | 29.970,4 [transcode] V: Y'CbCr | YV12/I420 [transcode] A: import format | 0x50 MPEG layer-2 [44100,16,2] 224 kbps [transcode] A: export format | 0x55 MPEG layer-3 [44100,16,2] 128 kbps [transcode] V: encoding fps,frc | 29.970,4 [transcode] A: bytes per frame | 5884 (5885.880000) [transcode] A: adjustment | 1880@1000 [transcode] V: IA32 accel mode | sse2 (sse2 sse mmxext mmx asm C) tc_memcpy: using mmxext for memcpy [transcode] V: video buffer | 10 @ 320x240 [import_mp3.so] v0.1.4 (2003-08-04) (audio) MPEG [import_mpeg2.so] v0.4.0 (2003-10-02) (video) MPEG2 [export_ogg.so] v0.0.5 (2003-08-31) (video) null | (audio) ogg [export_xvid4.so] v0.0.5 (2003-12-05) (video) XviD 1.0.x series (aka API 4. 0) | (audio) MPEG/AC3/PCM [import_mp3.so] MP3->PCM [import_mp3.so] tcextract -a 0 -i "sample.mpg" -x mp2 -d 0 | tcdecode -x mp2 -d 0 -z 0 [import_mpeg2.so] tcextract -x mpeg2 -i "sample.mpg" -d 0 | tcdecode -x mpeg2 -d 0 -y yv12 … [export_ogg.so] Hint: Now merge the files with [export_ogg.so] Hint: ogmmerge -o complete.ogg output.avi output.ogg clean up | frame threads | unload modules | cancel signal | internal threads | done [transcode] encoded 15491 frames (0 dropped, 0 cloned), clip length 516.88 s $ 

The transcode command even provides you with a hint on how to merge the audio and video streams once it is finished. In the case of Ogg files, use ogmmerge (part of the Ogg Vorbis toolset) to merge the video and audio streams:

 $ ogmmerge -o  final.avi output.avi output.ogg  Using AVI demultiplexer for output.avi. Opening file. This may take some time depending on the file's size. +-> Using video output module for video stream. Using OGG/OGM demultiplexer for output.ogg. +-> Using Vorbis audio output module for stream 1. progress: 15490/15490 frames (100%) $ 

In this example, final.avi would be the final combined video.

3.18.2. Other Multipart Video Formats

When creating other video files, particularly (S)VCDs, DVD, or other MPEG files, often transcode will output into multiple files that you then need to combine into a single file. The complete process of creating (S)VCDs and DVDs is covered in [Hacks #69 and #70] respectively, but if you want, you can also create these file types with transcode.

For this example, I have an MPEG1 file, sample.mpg, that I want to convert to an MPEG2 file. I use transcode with both video and audio export modules selectedin this case mpeg2enc and mp2enc for video and audio, respectively. In addition, since the mpeg2enc supports both MPEG1 and MPEG2, I need to pass an extra option, -F, to specify what type of video to create. The options for the -F argument vary depending on the type of video module you have selected, and all the various options are described in the transcode manpage. For mpeg2enc, the primary option for the -F argument is a number between 0 and 8. Read the manpage for more information on these options, but as an example, 0 specifies a MPEG1 (and is the default), 1 specifies VCD, 3 specifies MPEG2, 4 specifies SVCD, and 8 specifies DVD. Since I want MPEG2, I type the following:

 $ transcode -i  sample.mpg  -o  output  -y mpeg2enc,mp2enc -F 3  

Notice that I didn't add an extension to the output filename. That's because transcode is going to create two files for me, output.m2v (my MPEG2 video) and output.mpa (my MPEG2 audio). Since these files are MPEG format, the next step is to combine these two files into a single file with the tcmplex utility. tcmplex is part of the transcode toolkit up through 0.6.14, although it was removed due to lack of maintenance. If you have a newer version of transcode you can use the mplex tool that is part of the mjpegtools suite (http://mjpeg.sourceforge.net). To combine these two files, I type the following command for tcmplex:

 $ tcmplex -m 2 -i  output.m2v -p output.mpa  -o  output.mpg  

The -m option sets the output to one of a few predefined modes. In this case I set it to 2, which is the predefined mode for MPEG2, but I could have set it to 1 (MPEG1 VBR), d (DVD), s (SVCD), or v (VCD). The -i option specifies the input video stream, and -p specifies the input audio stream. You can even specify multiple audio streams, just use -s to specify the second audio track. Finally, the -o argument specifies the output file to use.

In the case of mplex, type:

 $ mplexf 3o   output.mpg  i  output.m2voutput.mpa  

Thef option is like them option in tcmplex and specifies a predefined mode for the output file. In this case, I chose 3 for standard MPEG2, but I could have set it to 0 (generic MPEG1), 1 (VCD), 2 (SVCD), or 9 (DVD). The manpage for mplex lists a number of other format options. Finally theo argument specifies the output file to use.

Once the command completes, I have an MPEG2 file named output.mpg.

3.18.3. Final Notes

Transcode is a very complicated program with a large number of options. While I can't easily cover all the processes of conversion to every format you might want, the previous examples should be enough to get you started with converting files to various MPEG1, MPEG2, and MPEG4 or XviD files. Generally speaking, to change to a different output format, just change the argument you pass -y, and optionally add -F arguments if they exist for that output module.

For more information on tweaks to specific formats, be sure to read through the examples given in the transcode manpage (type man transcode) as well as the numerous examples on the official transcode page at http://www.transcoding.org.




Linux Multimedia Hacks
Linux Multimedia Hacks
ISBN: 596100760
EAN: N/A
Year: 2005
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net