Skip to Content

Video encoding for dummies

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)? 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 : -rw-r--r-- 1 oneofone users 26M Jan 7 18:05 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 747 Number of frames : 3214 Frames == b : 2097 / i : 18 / p : 1099 -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 Bill Gates Failure.avi: RIFF (little-endian) data, AVI, 352 x 480, ~30 fps, video: XviD, audio: MPEG-1 Layer 3 (stereo, 48000 Hz) -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! --cut-- --repeat-- --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

  1. !/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; } 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 : 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 :

    codec :

    first pass xvid log (xvid-twopass.stats) :

    References:

    • man:mplayer

    • man:lame



Dr. Radut | book