Powerful Multimedia Command-Line Tools, Part II—Transcode
MEncoder has supported video encoding for a long time with the MPlayer Project and FFmpeg, which also now is part of MPlayer. Transcode is a new command-line tool on the horizon for video and audio transformations. Transcode used to give me horrors, but it is much better now. It does take some time to learn its wonderfully unintuitive syntax—the author used all the lowercase and uppercase English alphabet letters for specifying the command-line options. Using longer mnemonic options common in other Linux commands might have made things easier. Anyway, let's get to the meat of the matter.
You can apply certain video filters to smooth, blur, enhance, add or remove logos, apply masks and so on with Transcode. But before we get to all of that, here's a piece of advice from the trenches. Do the following:
$ tcmodinfo -p
Then, go to the directory and see what import modules, export modules and filters came with your Transcode program. Mine did not contain several export modules, and a few filters were missing.
Transcode's error reporting is not very user-friendly. You need to look at the output carefully to figure out whether Transcode understood a certain command switch. Sometimes it hangs, and occasionally it tells you what went wrong on the command line. So, you need to take some time to gain familiarity with using the commands.
Another problem with Transcode is that it supports a huge list of options, so you'll want to do some memorizing. Keep the man page open and make sure you don't miss anything. Once you find a combination that works, you're all set. Some of the most important switches are -x, -y, -J, -i and -o.
The -x and -y switches are for specifying the import and export modules, respectively. Depending on the input/output video/audio formats, you may need to change them. Use the tcprobe tool that comes with Transcode to figure out whether a particular audio or video input is autoprobed by Transcode. If tcprobe is happy with it, you can skip the -x option.
The -J option is the most versatile. It is the filer option, and you use it to specify the filter chain. All video preprocessing and postprocessing is performed with this switch.
The -i and -o options are for video input and output files. Either or both of them could be /dev/null. There also are switches for specifying an external audio file as input (-p) and for dumping the resulting audio after processing to a separate file (-m). You can do a lot of tweaking and add a great deal of filtering subsystems into the chain. Transcode is a young program, and I expect many more filters will be added.
It is odd that Transcode supports all kinds of imaginable input and export modules. Its notion of what constitutes a video is very broad. It can take images from the ImageMagick library; it can do audio-only processing; it can take sound files as audio input for the video and redirect the output audio to a different file and so on.
I tested a long list of commands to get an idea of what it could do. Once I figured out the basics, the rest was smooth sailing.
The following is a command to cull out the audio from video.mpg:
$ transcode -i /home/girish/video.mpg -x mpeg2 -y null,ogg -J aclip,resample -m /tmp/foo.ogg -E 22050
I don't have the MP3 export module, so I encoded it into Ogg Vorbis format. The -E 22050 switch specifies the output sample rate. You also can specify the bits per sample and the number of output channels with the same switch.
This next command saves the output audio in AC3 format after resampling to 48,000Hz as per AC3 standard. The -J resample filter option is necessary but is invoked internally by Transcode:
$ transcode -i /home/girish/videos/video.mpg -x mpeg2 -E 48000 -y null,ac3 -m /tmp/foo
The following line dumps the video frames in motion JPEG format along with the audio processing as above:
$ transcode -i /home/girish/videos/video.mpg -x mpeg2 -E 48000 -y mjpeg,ac3 -J aclip,resample -m /tmp/foo -o /tmp/fop.mjpeg
This line dumps the video frames into separate ppm files in the /tmp directory with the prefix ppm:
$ transcode -i /home/girish/videos/video.mpg -x mpeg2 -E 48000 -y ppm,ac3 -m /tmp/foo -o /tmp/ppm
This line does the same but in JPEG format:
$ transcode -i /home/girish/videos/video.mpg -x mpeg2 -E 48000 -y jpeg,ac3 -m /tmp/foo -o /tmp/foo
There also is support for PNG, but I do not have the export module in my transcode install.
Figure 1. Transcode Architecture
DVDs contain videos in a VOB container format that can consist of several audio and video streams, as well as subtitle files, and all are organized into chapters. Transcode can understand DVD titles with the -T switch. There are many on-line resources with further information on DVD handling.
Let's get back to our examples. The following line does the same as the last example command but ignores the audio:
$ transcode -i /home/girish/videos/video.mpg -x vob -y jpg -o /tmp/foo
Note that transcode can use external commands, like ffmpeg or oggenc, for its processing, and you can observe what is going on in the output.
Transcode uses the ANSI terminal color sequences effectively, and you can turn that on or off with a command-line switch. There also are several other switches for exporting a particular profile or aspect ratio. DVD profiles are well supported, by which you can short-circuit some of the other options. There also are switches for flipping, taking a color negative, mirroring and so on.
This command inverts the video colors to take a color negative:
$ transcode -i /home/girish/videos/video.mpg -x vob -y mjpeg,ogg -J invert -o /tmp/foo.mjpeg -m /tmp/foo.ogg
The -J invert filter does this magic. The filtering subsystem is Transcode's most powerful component; take particular note of its power and usage.
The following is an example of applying a huge filter chain. It demonstrates the power of the Linux command line and also the architecture of Transcode that can handle such a filter chain:
$ transcode -i /home/girish/videos/video.mpg -x vob, -y mjpeg, -J mask=lefttop=0x0:rightbot=500x500,msharpen=strength=205:threshold=20,nored,normalize=smooth=0.2,pp=hb/vb/dr/al-o /tmp/foo.mjpeg
Don't be scared by the length. It is simple once you break it down. Most of the switches should be familiar; only the -J switch is so lengthy.
In the above example, first I apply the mask filter. It picks out portions of the video and ignores the rest. I specified rectangular coordinates for masking. I get a square of 500x500 pixels starting at the top-left corner of the screen. The msharpen filter is one of Transcode's sharpening filters. I tweak the algorithm by specifying the threshold and strength of sharpening. The nored and normalize filters remove red components and normalize the audio, respectively.
You can mix audio and video filters in the same chain. The pp filter does the same thing as MPlayer's postprocessing filter subsystem. Here I am using the same options as given in MPlayer's man page section for pp. It does horizontal and vertical deblocking, deringing and automatic brightness/contrast correction.
When dealing with such a long filter chain, observe the effect of each filter before arriving at something as complex as the above.
This example demonstrates the unsharp filter, which is similar to MPlayer's filter of the same name:
$ transcode -i /home/girish/videos/video.mpg -x vob, -y mjpeg, -J unsharp=amount=-1.5:luma=0.6:matrix=7x7 -o /tmp/foo.mjpeg
And, this command is cool:
$ transcode -i /home/girish/videos/video.mpg -x vob, -y mjpeg, -J text=string='transcode is cool':posdef=2:font=/home/girish/.ttffonts/comicbd.ttf -o /tmp/foo.mjpeg
As shown above, you can add text to a video at any position indicated by the pos coordinates or the posdef predefined locations. Transcode also can render ppml subtitles and play with DVD subtitles.
Because I did not have certain filters and modules, I couldn't try much more. But that's something you can fix by compiling Transcode yourself. As you can see here, Transcode is powerful stuff, but it can be unfriendly to beginners. Once you get a firm grip on its power, you can blow the top off GUI tools and commercial alternatives that come with a huge price tag.
Audio Physics: http://www.harmony-central.com
Audio Engineering: http://www.ee.washington.edu/conselec/CE/kuhn/audio/95x3.htm
See part I of this article HERE.
Girish Venkatachalam is an open-source hacker deeply interested in UNIX. In his free time, he likes to cook vegetarian dishes and actually eat them. He can be contacted at firstname.lastname@example.org.
Special Reports: DevOps
Have projects in development that need help? Have a great development operation in place that can ALWAYS be better? Regardless of where you are in your DevOps process, Linux Journal can help!
With deep focus on Collaborative Development, Continuous Testing and Release & Deployment, we offer here the DEFINITIVE DevOps for Dummies, a mobile Application Development Primer, advice & help from the experts, plus a host of other books, videos, podcasts and more. All free with a quick, one-time registration. Start browsing now...