Mplayer - The multimedia dynamite II Last month we got introduced to some of the features of the coolest multimedia player on earth. This time we will go deeper and explore what more it can do for us. We will also take a dekko at encoding videos, applying filters for enhancing video, removing letterboxing, hardcoding subtitles, compressing videos , combining and cutting videos and so on. Let us take a peek at mplayer's video filters. It has a vast collection and it takes advanced video processing skills to use them effectively. And there is no hard and fast rule here. You have to observe it on a case to case basis and figure out what works best for you. $ mplayer -vf help lists them all. $ mplayer -vf flip video.avi $ mplayer -vf mirror video.avi and $ mplayer -vf rotate video.avi will do what you guessed - flip, mirror and rotate. It also has a swapuv filter which swaps the blue and red colors. mplayer supports several different deinterlacing filters, noise removal plugins, sharpening and blurring filters and filters specific to a vast array of codecs. However the lavc codec family is the best and it is a native family that is most useful to us. There is also a special codec called 'copy' which comes in handy for concatenating and cutting videos. $ mencoder video.avi -ss 200 -endpos 600 -o portion.avi -oac copy -ovc copy will fish out only that portion of the video starting from 200 seconds and running for 600 seconds. As you can see, we are already in mencoder territory. The command line for mencoder is similar to mplayer but there are several switches that only mplayer or mencoder understands. $ mencoder -idx video1.avi video2.avi -o combined.avi -oac copy -ovc copy will concatenate the two videos. Cool. $ mencoder video1.avi video2.avi -o mix.avi -oac lavc -ovc lavc will mix them. $ mencoder video1.mpg -audiofile foo.mp3 -o output.mpg -oac lavc -ovc lavc will get the audio file from foo.mp3 into the output.mpg with video frames from video1.mpg. It is a good idea to check with the video filters of mplayer and then use them with mencoder to permanently encode them into the output video. Let us get to something quite useful and interesting for audiophiles. Nowadays motherboards have 5.1 audio built in and it is a good idea to have high quality speakers placed strategically in your living room. Acoustics is a field of its own and beyond the scope of this article but I will tell you how to extract 5.1 audio from matrix encoded 2 channel audio. This will work even with plain 2 channel audio but obviously it has its drawbacks. First figure out if your audio stream has 5.1 or not. $ mplayer -msglevel identify=9 -vo null -ao null If you don't see AC3 or (3f+2r+lfe) , then you don't have it. $ sox -V input.wav -r 48000 -c1 left.wav avg -l This uses the average effect of sox to separate out the left channel. $ sox -V input.wav -r 48000 -c1 right.wav avg -r Ditto for right channel. Then create the center channel. $ soxmix -V left.wav right.wav -r 48000 -c1 center.wav Then, create the LFE channel $ sox -V -v 0.5 center.wav lfe.wav lowp 150 You could try with some other frequency for lowpass filtering than 150 Hz. You could also use the sub audio filter of mplayer for this purpose. Low cutoff frequencies give better surround effect. Let us create the combined signal. $ multimux left.wav center.wav right.wav left.wav right.wav lfe.wav -o surround.wav Now you are left with the task of converting surround.wav into AC3 or DTS with ffmpeg or some other tool. $ ffmpeg -f s216e -ar 48000 -ac 6 -i surround.wav -ab 384 output.ac3 Usually this is done for DVD mastering. Incidentally mencoder can be used for this along with other MJPEG tools. This mencoder command line will do the bulk of the work for you. $ mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=dvd:tsaf -vf scale=720:480,harddup -srate 48000 -af lavcresample=48000 -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=5000: keyint=18:vstrict=0:acodec=ac3:abitrate=192:aspect=16/9 -ofps 30000/1001 -o movie.mpg movie.avi This for NTSC standard. Replace the ofps value with 25 and the scale factor to 720:576 for PAL. It will be a good idea to normalize the audio of the videos to ensure that their root mean square amplitudes are identical. The normalize command line tool can do it for you. $ ffmpeg -i video1.avi video1.wav $ ffmpeg -i video2.avi video2.wav . . $ normalize-audio video1.wav video2.wav .. videon.wav It will run in two passes figuring out the average correction to apply to each audio and then applying them in the second pass. You can create the DVD file structure without any menus using the dvdauthor command line tool. $ dvdauthor -x dvd.xml with this file will do the trick. Then test the authoring with $ mplayer dvd://1-2 -dvd-device dvd/ If mplayer plays it correctly you are ready for burning the DVD. $ growisofs -Z /dev/dvd -R -J dvd/ will do the trick. To create an iso and then burn, $ mkisofs -dvd-video -udf -R -J dvd -o image.iso $ growisofs -Z /dev/dvd=image.iso mencoder can apply a variety of video filters. It is best to check with mplayer what the filters really do. In case you are interested in correcting the A/V sync of a video, you can check out the -audio-delay option of mencoder. It works exactly like the -delay option of mplayer but it is contrariwise in operation. $ mencoder -audio-delay 5 video.avi -o corrected.avi -oac copy -ovc copy will delay the audio by 5 seconds. In case you want to delay the video by 5 seconds, specify -audio-delay -5. Note that this often does not correct serious A/V sync issues. If your encode has a problem, you have to re encode it. No other way out. The crop and cropdetect filters are very useful to us when dealing with letterboxed videos. Removing the black borders is extremely crucial for any video processing as compression algorithms perform very badly with these black borders. You could always add them using the expand filter later once you perform your video processing if you prefer to preserve aspect ratio. $ mplayer -vf cropdetect=52:16 video.mpg will print out the necessary parameter you have to pass to remove the letterboxing. I got this line [CROP] Crop area: X: 7..629 Y: 84..393 (-vf crop=608:304:16:88).0 Then I run $ mplayer -vf crop=608:304:16:88 video.mgp and voila, the borders are gone! Now it is a simple matter of removing them permanently with mencoder. I felt like scaling it too. It is good to use sharpen filters for scaling, but I did not use them. $ mencoder -vf crop=608:304:6:90,spp,scale=1024:768 video.mpg -o cropped.mpg -oac copy -ovc lavc It chugs on for a while and gives me the result. The expand filter merely adds black borders and it is much more generic. You may prefer to use it to add a band under the movie to display subtitles. That way you can watch the subtitles without occluding the video area. $ mplayer -vf expand=0:-100:0:0 video.mpg adds a black band to the bottom. The scale filter is of particular interest to us for obvious reasons. It takes several parameters and it is supposed to be used in tandem with several other video filters for offsetting the negative effects on video quality due to scaling. I much prefer the hardware scaling of xv driver. You can never get better than that. But even otherwise scaling is useful. The '-vf pp' switch enables a chain of postprocessing filters. For medium quality videos, it will be a boon for enhancing quality. This happens often when recording from analog television on linux. $ mplayer -vf pp=hb/vb/dr/al/tmpnoise:1:2:3,screenshot video.avi demonstrates pp filter along with filter chaining in mplayer. This line does horizontal and vertical deblocking, deringing, automatic brigthness/contrast correction and also temporal denoising. You can inspect the screenshots with different filter combinations and arrive at the right knobs for enhancing the video perceptual quality. It is much easier to observe artifacts in screenshots. Other filters that piqued my interest are denoise3d, noise, hqdn3d, swapuv and unsharp. You could play with the hue and eq2 filters for brightness and color correction. As you can see, it is an ocean. Go explore for more! mencoder has excellent support for inverse telecining film videos, a variety of deinterlacing filters, algorithms for compressing different types of videos like anime, action packed sports shows and other static content in which few objects move. As a result of the wide variety of what a video can possibly contain, you have to use your own judgment and tweak the knobs provided to get the desired result. MPEG videos are by far the most common and some familiarity with its internal workings and terminology will surely help you use some of mencoder's advanced video plugins. MPEG video frame formats and compression schemes are different depending upon the version and profile. But the overall architecture is quite similar. There is the concept of a Group of pictures in short known as GOP. And it typically consists of 12 to 15 frames. If it is interlaced video, then one frame consists of two fields - even and odd. Each GOP is independent of the following and preceding GOP. The frames are categorized into I (Intra) frame, Bidirectional or B frame and Predictive or P frame. I frames contain the most information and are hence very critical. Other frame types store only the delta depending upon how many pixels of the frame actually change. A macroblock is the fundamental unit of image processing and video processing too. It is typically 16x16 pixels. And all of the compression algorithms based on Discrete Cosine Transform use this as the fundamental unit. This is the reason we should always crop and scale into a dimension that is a multiple of 16. Let us wind up after looking at few other interesting aspects of mencoder. You can hardcode subtitles into a video (hard subbing) with this command. $ mencoder -sub foo.srt video.avi -o out.avi -oac copy -ovc lavc You can of course apply the expand filter if you want. I used mencoder for recording analog television and it was extremely convenient to invoke from a cron job thereby enabling me to record shows to hard disk even when I am sleeping. mencoder can also rip DVDs for personal purposes with elan. $ mencoder dvd//# -dumpstream -dumpfile dvd1.vob mencoder supports DVD's vobsub subtitle format very well. You might have to specify the language ID in case there are many. Other topics like two pass encoding, encoding to flash format, tips on better compression, number of keyframes for seeking and so on are dealt with in graphic detail in the html documentation that comes with mplayer. Here is an interesting tidbit before we end this series. There is a 'family friendly' mode in mplayer called Edit Decision List. You can mute or censor part of the video by simply specifying the time range in seconds and a flag for muting(1) or skipping(1). 15.3 70.1 0 95 106.7 1 450 522 0 If the above are the file contents of edl.txt you could invoke mplayer with $ mplayer -edl edl.txt video.mpg and it will skip the video from 15.3 seconds till 70.1. It will mute the audio between 95 and 106.7 seconds. And if you have another unfriendly scene at 450 seconds, it could censor that too. In case you want to customize mplayer for your needs and make it support more features, you can download the codecs zipfile from the mplayer website. It supports very Windows codecs and other proprietary formats using this mechanism. mplayer also has several GUI skins in case you prefer that. I am quite happy with the command line. There are also plenty of mplayer frontends listed in the mplayer website. For certain advanced use of mencoder or mplayer you will have to compile them enabling those options. Compiling mplayer has another side benefit. It will be optimized for your CPU and consequently run faster. There are also LiveCD distributions based on mplayer. I particularly liked the womp project. Refer to resources for more information. Happy mplaying! Resources: SOX homepage: http://sox.sourceforge.net multimux : http://ip51cf87c4.direct-adsl.nl/panteltje/dvd/ dvdauthor: http://dvdauthor.sourceforge.net