linux
web
life
ctrv
man
link

Video Editing in Linux Console

Video Editing in Linux Console

Linux is considered the best platform for working with video. Whole video and audio processing centers are built on the Linux OS. A lot of professional video studios work with Linux. For example, the post-processing of the well-known cartoon Madagascar was done specifically in Linux. The list of famous movies created using the Linux OS can go on endlessly, but today we will talk about something else, namely how Linux can help in processing home video.

For this, there is a wonderful set of programs. I will say right away that people not familiar with Linux at all will have to figure things out a bit. But it’s worth it, or rather it costs nothing :), because almost all programs under Linux are free.

The best and most popular under Linux for video processing is considered to be mencoder. This program works in terminal mode. And in fact, in this note I will tell you about working with it from the command line, and you will see that it is in no way inferior to GUI shells.

Mencoder supports many file types, here are some of them: MPEG/VOB, AVI, ASF/WMA/WMV, RM, QT/MOV/MP4, Ogg/OGM, MKV, VIVO, FLI, FLV. Command syntax is fairly simple despite a large number of options. Despite its “modest” interface, mencoder can do amazing things with multimedia files.

A simple command to convert an MPG file to AVI format:

mencoder file.mpg -o file.avi -ovc lavc -oac lavc

Options -oac and -ovc indicate the desired audio and video codecs. You can view the list of available codecs using the commands mencoder -ovc help and mencoder -oac help

Suppose you need an uncompressed audio stream and decided to use PCM. You can specify the desired audio format using the acodec option:

mencoder file.mpg -o file.avi -ovc lavc -oac lavc -lavcopts acodec=pcm

If you use mp3 compression, you can also specify the bitrate:

mencoder file.mpg -o file.avi -ovc lavc -oac lavc -lavcopts acodec=libmp3lame:abitrate=128

You can use lameopts if libmp3lame is installed on your system and you want to expand the list of options available during audio encoding. Also with lameopts you can create files with variable bitrate:

mencoder file -o file.avi -ovc lavc -oac mp3lame -lameopts vbr=2:q=3

where q is any number in the range from 0 to 9.
Also you can work with audio tracks from video files:

mencoder file.mpg -o file.avi -ovc lavc -oac lavc -lavcopts acodec=libmp3lame:abitrate=128 vcodec=xvid

If you don’t want to use video compression, use vcodec=copy. With this option, the video stream will simply be copied byte for byte from the source file.

You can use DivX and xvid directly without the lavc option:

mencoder -ovc xvid -oac mp3lame -o destination.avi source.avi

To specify more detailed parameters, you can use xvid options:

mencoder -ovc xvid -oac mp3lame -xvidencopts bitrate=878 -o destination.avi source.avi

Increasing the bitrate improves image quality, but along with quality, the file size also grows. Keep this in mind.

The next step, or rather two, will be describing the process of transcoding DVD to XviD. First mencoder analyzes the file, then, based on the data obtained, transcodes the DVD stream. Using two passes is not much longer, but on output you get a higher quality file. Be prepared that during the encoding process CPU load will be around 90%:

mencoder dvd:// -oac mp3lame -ovc xvid -xvidencopts pass=1 -o /dev/null
mencoder dvd:// -oac mp3lame -ovc xvid -xvidencopts pass=2:bitrate=800 -o xvidfile.avi

You can specify bitrate to get the required quality. For example, to fit a DVD into a 700-megabyte xvid file, use the following options:

mencoder dvd:// -ovc xvid -oac mp3lame -xvidencopts bitrate=-700000 -o file.avi

If you don’t want maximum CPU load during encoding, use the nice utility, which will lower the priority of the running process:

nice -n 19 mencoder dvd:// -ovc xvid -oac mp3lame -xvidencopts bitrate=-700000 -o file.avi

If you have many small video files and want to make one big one out of them, mencoder can help you… Rename the source files so that they sort in the required order. Then use:

mencoder * -o output.avi

If you want to add an audio track to a video file, use:

mencoder source.avi -o destination.avi -ovc copy -oac mp3lame -audiofile file.wav (for uncompressed files)
mencoder source.avi -o destination.avi -ovc copy -oac copy -audiofile file.mp3 (for compressed files)

To get video suitable for playback on iPodLinux, use:

mencoder -ovc raw -ofps 15 -oac pcm -vf scale=176:-2,expand=176:132,format=bgr16 input.file -o output.avi

This command will create a RAW AVI file with uncompressed audio stream and scale it to the Nano screen size.

I have a PocketPC, which often accompanies me on business trips. I use mencoder to get movies that fit on a 512Mb SD-Card and play on my Pocket:

mencoder -oac mp3lame -lameopts mode=3:preset=24 -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=384:keyint=250 -vop expand=”320:240″ -o outputfile.avi inputfile.avi

or

mencoder input.avi -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=200:abitrate=48 -vop scale=320:240 -oac copy -o output.avi

The difference is that the second command scales the movie to the PocketPC screen size — 320×240 pixels.

If you have a webcam and want to record video from it, use:

mencoder tv:// -tv driver=v4l:device=/dev/video0:width=640:height=480:forceaudio -ovc lavc -oac lavc -lavcopts vcodec=mpeg4:acodec=mp3 -ffourcc divx -o test.avi

The command records all /dev/video0 output into a file with 640×480 resolution and mp3 audio.

As you can see, with mencoder you can work with a large number of formats, a huge number of options are available, it is small, lightweight, and works very fast. And I won’t trade it for any utility with a more “friendly” interface.

Specially for those interested, a list of front-ends for mencoder:

Kmencoder http://sf.net/projects/kmencoder/
Konverter http://www.libsdl.de/projects/konverter/
Kmenc 15 http://kmenc15.sf.net/
GMencoder http://gmencoder.sf.net/
AcidRIP http://untrepid.com/acidrip/
MenGUI http://jahjah.free.fr/morphos/