31 December, 2008

Video Editing Ninja Moves with Linux

Our program focus for the past few months has concentrated on advanced image processing algorithm development. Pre-collected video streams are utilized as inputs to our development process, leading us to become familiar with Linux video conversion utilities. I'll briefly identify some of the features we've encountered that were useful to us.

Source Stream
You need to first start with a collected video stream. There are numerous methods of gettin' hold of an input stream: download it, extract it from a dvd, collect if from your favorite Tivo device, etc.

To extract an uncompressed video stream into an Avi container from a dvd title:
Note, we are attempting to preserve the highest level of video quality by preventing lossy compression.

$ ffmpeg -i /media/cdrom/VTS_01_1.VOB -vcodec rawvideo /tmp/source.avi

An alternative (Tivo'lious) video stream can be extracted from a Tivo Mpeg-Ps container. A SourceForge project Tivo File Decoder provides the means of extracting the Mpeg video stream from the proprietary Tivo file container.

$ tivodecode --mak 0012345678 -o /tmp/source.mpg ~/Desktop/BigBangTheory.TiVo

We'll use the Duke as our source video stream:

We can get a few details of the video file by using the file *nix command:

$ file /tmp/source.avi
/tmp/source.avi: RIFF (little-endian) data, AVI, 720 x 480, ~30 fps, video:, audio: (stereo, 48000 Hz)

Of particular interest are the frame dimensions 720x480 and 30fps frame rate. These details will be useful later in examining the effects of some of our commands.

Frame Extraction/Reassembly
Extracting frames from a source video stream is useful for many possible reasons. Extracted frames can be stored in a series of Ppm or Jpeg image files.

Extracting frames to Jpg image files can be done by:

$ ffmpeg -i /tmp/source.avi %08d.jpg

Extracting frames to Ppm image files done by:

$ ffmpeg -i /tmp/source.avi %08d.ppm

And extracting frames to Pgm image files can be done by:

$ ffmpeg -i /tmp/source.avi %08d.pgm

You can reassemble the video element of the source video by assembling the extracted images back into a video stream. In order to preserve the framerate of the source video stream you need to know and specify the frame rate of the encoded video. If you don't specify the original frame rate, a default of 25fps will be used which may result in a faster or slower playback than the original.

$ ffmpeg -r 30/1 -i %08d.jpg videoOut.mpg
The resultant video is a near visual duplicate of the original, the sound channel however was eliminated as a result of the disassembly/reassembly procedure.

Frame Rate
Re-sampling the source video into an alternative frame rate is advantageous.

The original source video was sampled at 30fps, to sample the destination at 5fps you'd command:

$ ffmpeg -i /tmp/source.avi -r 5/1 /tmp/videoOut.avi

To upsample to 60fps you'd command:

$ ffmpeg -i /tmp/source.avi -r 60/1 /tmp/videoOut.avi

Frame Cropping
Faking a letterbox can be emulated by cropping the top and bottom off the original video. Cropping the top and bottom 160 rows can be done by:

$ ffmpeg -i /tmp/source.avi -croptop 160 -cropbottom 160 /tmp/letter.avi
$ file /tmp/letter.avi
/tmp/letter.avi: RIFF (little-endian) data, AVI, 720 x 160, ~30 fps, video:, audio: (stereo, 48000 Hz)

Video Segments
Snaggin' a clip out of a source video is always useful. To extract the Dukes classic line you can specify the starting position and duration of the clip. Specifying a starting location using the -ss specifier followed by the -t duration specifier can be done as follows:

$ ffmpeg -ss 85 -t 18 -i /tmp/source.avi /tmp/clip.avi


Altering Video Dimensions
It is sometimes advantageous to change the video dimensions of the source video. If high resolution imagery isn't required changing the video stream to a smaller dimension reduces the file size and expedites processing.

$ ffmpeg -i /tmp/source.avi -s 320x240 /tmp/small.avi
$ file /tmp/small.avi
/tmp/small.avi: RIFF (little-endian) data, AVI, 320 x 240, ~30 fps, video:, audio: (stereo, 48000 Hz)

Video Format Conversions
Converting to various file formats and codecs is the real intention of ffmpeg. Ffmpeg allows conversions to pretty much any popular (and many unpopular) containers and encoders. Below are a few of the popular formats we used, many others are available.

$ ffmpeg -i /tmp/source.avi /tmp/source.mpg
$ file /tmp/source.mpg
/tmp/source.mpg: MPEG sequence, v1, system multiplex

$ ffmpeg -i /tmp/source.avi -vcodec rawvideo /tmp/source.avi

The raw video ensures the highest quality of video, useful if you wish to preserve the highest degree of resolution of the original video. An alternative is to specify -qmin 1 and -qmax 1 which specifies a range 1-51 (1-highest quality).