Video encoding for dummies
Hmm ok, so if you've read anything I wrote before you, then should already know that writing isn't my thing..
Yep and grammar isn't my thing either
Before we start you need :
- xvid for linux (emerge xvid or find a package for your distro).
- lame/libmp3lame (emerge lame or find a package for your distro).
- vorbis-tools (only for using ogg sound).
- mkvtoolnix/libmatroska (only for using the matroska container and ogg sound)
- mplayer/mencoder (USE="encode xvid matroska oggvorbis" emerge mplayer or find a package for your distro).
- the ability to read and understand bad grammar.
- some common sense (search for it in your favorite S-Mart).
notes
- You can use ogg as a container, however I prefer matroska and since I wrote this guide, I can choose what I like :P
- I'm using xvid/mplayer from cvs head but it shouldn't be much different than latest _preX mplayer.
Q: How do I find info about an .avi (or any media file)?
[~] -> idmedia /media/movies/DareDevil.Directors.Cut.DVDRip.XviD-DoNE.CD1.avi ID_VIDEO_ID=0 ID_AUDIO_ID=1 ID_FILENAME=/media/movies/DareDevil.Directors.Cut.DVDRip.XviD-DoNE.CD1.avi ID_VIDEO_FORMAT=XVID ID_VIDEO_BITRATE=1148520 ID_VIDEO_WIDTH=640 ID_VIDEO_HEIGHT=272 ID_VIDEO_FPS=23.976 ID_VIDEO_ASPECT=0.0000 ID_AUDIO_CODEC=a52 ID_AUDIO_FORMAT=8192 ID_AUDIO_BITRATE=448000 ID_AUDIO_RATE=48000 ID_AUDIO_NCH=2 ID_LENGTH=3649
Q: How do you I do a high quality video encodes with mencoder (xvid/mp3)?
Well There're 2 ways so lets see :
If you want the highest quailty without caring about size :
[/mnt/tmp] -> l Bill\ Gates\ Failure.mpg -rw-r--r-- 1 oneofone users 26M Jan 7 18:05 Bill Gates Failure.mpg [/mnt/tmp] -> export XVIDOPTS="me_quality=6:chroma_me:chroma_opt:trellis:max_key_interval=300:vhq=4" [/mnt/tmp] -> xvidpass1 $XVIDOPTS "Bill Gates Failure.mpg" MEncoder dev-CVS-050109-00:59-3.4.3 (C) 2000-2005 MPlayer Team ---cut--- Video stream: 1401.710 kbit/s (175213 bps) size: 18830952 bytes 107.474 secs 3223 frames The value 99.99dB is a special value and represents the upper range limit xvid: Min PSNR y : 39.62 dB, u : 46.56 dB, v : 47.19 dB, in frame 2178 xvid: Average PSNR y : 43.91 dB, u : 48.28 dB, v : 49.08 dB, for 3216 frames xvid: Max PSNR y : 52.63 dB, u : 56.58 dB, v : 56.72 dB, in frame 0 [/mnt/tmp] -> xvid-log-analyzer.pl 747 Number of frames : 3214 Frames == b : 2097 / i : 18 / p : 1099 [/mnt/tmp] -> mencoder -ovc xvid -xvidencopts $XVIDOPTS:pass=2:bitrate=747 \ -oac mp3lame -lameopts preset=standard -o Bill\ Gates\ Failure.avi Bill\ Gates\ Failure.mpg MEncoder dev-CVS-050109-00:59-3.4.3 (C) 2000-2005 MPlayer Team ---cut--- Video stream: 727.332 kbit/s (90916 bps) size: 9771172 bytes 107.474 secs 3223 frames Audio stream: 130.982 kbit/s (16372 bps) size: 1767072 bytes 107.928 secs [mnt/tmp] -> file Bill\ Gates\ Failure.avi Bill Gates Failure.avi: RIFF (little-endian) data, AVI, 352 x 480, ~30 fps, video: XviD, audio: MPEG-1 Layer 3 (stereo, 48000 Hz) [/mnt/tmp] -> l Bill\ Gates\ Failure.avi -rw-r--r-- 1 root root 12M Jan 19 14:17 Bill Gates Failure.avi
Now you can do the victory dance, you've just encoded your first high quality video!
Specific size :
under construction.
Q: How to remove black borders?
Easy, use the cropdetect filter!
[/mnt/tmp] -> mencoder -nosound -ovc xvid -xvidencopts bitrate=5000 -o /dev/null -vf cropdetect -ss 0:10:00 test.mpg --cut-- crop area: X: 0..351 Y: 0..239 (-vf crop=336:224:8:8)V:0.000 [0:0] crop area: X: 0..351 Y: 0..239 (-vf crop=336:224:8:8)V:0.000 [0:0] --repeat-- crop area: X: 0..351 Y: 0..239 (-vf crop=336:224:8:8)V:0.000 [1118:0] crop area: X: 0..351 Y: 0..239 (-vf crop=336:224:8:8)V:0.000 [1117:0] --other crap-- then simply append the crop value from that output to your pass2 command line : mencoder -ovc xvid -xvidencopts $XVIDOPTS:pass=2:bitrate=747 \ -oac mp3lame -lameopts preset=standard -vf crop=336:224:8:8 \ -o Bill\ Gates\ Failure.avi Bill\ Gates\ Failure.mpg
Important scripts/bash aliases used in this guide :
Bash aliases:
goes to ~/.bashrc or wherever your bash profile is
makes life easier and they are used a lot in the guide.
xvidpass1(){
time mencoder -nosound -ovc xvid -xvidencopts turbo:stats:${1}:pass=1 -o /dev/null "${2}"
}
idmedia(){
mplayer -identify -frames 0 -ao null -vo null -vc dummy "$@" 2>/dev/null | grep ID_
}
perl scripts :
xvid first pass log analyzer usage : perl xvid-log-analyzer.pl / append 2>/dev/null to remove the extra statistics
this script analyzes the xvid-twopass.stats file and outputs the average bitrate to be used with the second pass.
#!/usr/bin/perl
use strict;
open(XVID_LOG, '<:mmap', 'xvid-twopass.stats') or die "xvid-twopass.stats doesn't exist!!!!111oneoneone";
my ($br, $frames, $i, $b, $p);
while(<XVID_LOG>){
next if /^#/; next unless split (/\s+/) && $#_ == 6;
$br += $_[6]; $frames++;
$i++ if $_[0] eq 'i'; $b++ if $_[0] eq 'b'; $p++ if $_[0] eq 'p';
}
printf ("%d\n",$br / $frames);
print STDERR "Number of frames : $frames\n";
print STDERR "Frames == b : $b / i : $i / p : $p\n";
What is?
mplayer :
mplayer is a movie player for Linux (runs on many other platforms and CPU architectures, see the documentation). It plays most MPEG/VOB, AVI, ASF/WMA/WMV, RM, QT/MOV/MP4, OGG/OGM, MKV, VIVO, FLI, NuppelVideo, yuv4mpeg, FILM and RoQ files, supported by many native and binary codecs. You can watch VideoCD, SVCD, DVD, 3ivx, DivX 3/4/5 and even WMV movies, too.
mencoder :
mplayer is a movie player for Linux (runs on many other platforms and CPU architectures, see the documentation). It plays most MPEG/VOB, AVI, ASF/WMA/WMV, RM, QT/MOV/MP4, OGG/OGM, MKV, VIVO, FLI, NuppelVideo, yuv4mpeg, FILM and RoQ files, supported by many native and binary codecs. You can watch VideoCD, SVCD, DVD, 3ivx, DivX 3/4/5 and even WMV movies, too.
container :
A video file format is a standard for encoding digital video, audio and some auxiliary information into a file. In contrast to audio and image formats, most video file formats allow a variety of codecs, both audio and video, to be used.
** quote from http://en.wikipedia.org/wiki/Video_file_format
codec :
Codec is a portmanteau of "coder/decoder", which describes a device or program capable of performing transformations on a data stream or signal. Codecs can both put the stream or signal into an encoded form (often for transmission, storage or encryption) and retrieve, or decode that form for viewing or manipulation in a format more appropriate for these operations. Codecs are often used in videoconferencing and streaming media solutions.
** quote from http://en.wikipedia.org/wiki/Codec
first pass xvid log (xvid-twopass.stats) :
2 pass in the video world is like using vbr for mp3, aka "variable bitrate".
When you run mencoder with -xvidencopts pass=1, xvid analyzes the video and output it to xvid-twopass.stats so it can use that data for pass=2.
References:
- http://wwww.google.com
- http://www.bunkus.org/dvdripping4linux/other/ripping_dvds_to_divx_with_mencoder.html
- man:mplayer
- http://gentoo-wiki.com/HOWTO_Mencoder_Introduction_Guide
- man:lame

Get GNU / Linux
News Feed
Blogs
Event Photos
Screen Shots
Polls
Popular Content
Members
Search
Wall Papers