ffmpeg hook to aid with “rectangular pixels”
Tired of screen-scraping the output of ffmpeg and/or mplayer to get the parameters / clip info for a media file?
This hook attempts to remedy this by printing simple information about the passed in video from the cmd line.
It will also print out whether or not the clip is using “rectangular pixels”.
WTF is a rectangular pixel?
Well, the easiest examples are DVDs. You only want to buy a DVD if it says one of two standard phrases on it — “Enhanced for 16:9 TVs” or “Anamorphic widescreen“. They both mean the same thing — namely that the video on the DVD disk is wider than 4:3 aspect (pretty much all films are ratio 16:9 or even wider like ratio 2.35:1) *and* that it didn’t *waste* any DVD bytes by encoding “top/bottom black bars”. (If it doesn’t say those code phrases, it’s an older, crappy/low budget produced, or worse a “pan-n-scan” chopped film (where they lop off the left and right sides of each frame to fit into a 4:3 TV!) Worst yet, if the DVD only says something like “widescreen version”, though it sounds good, it means while they didn’t cut off the picture, they wasted 25% (or more!) of the pixels encoding “black bars” on the top and bottom. So you have less pixels in the DVD encoding the picture compared to an “anamorphic” version of the same thing. Hello crappy quality!)
Anamorphic DVDs are encoded internally at 720×480 pixels per image.
Now look at this:
4:3 video == 1.33 ratio == 640×480 pixels image
16:9 video == 1.78 ratio == 854×480 pixels image
so what is 720×480? it’s almost perfectly in the middle of those two — math: 720/480 = 1.5
So the “encoded transport image size” is neither 16:9, nor 4:3 — it’s right in the middle, capable to encode either a 4:3 video (like non-high def TV, many computer screens) or 16:9 video (high def TV, some digital video). I like to think of it as “how the formats that came out right before HDTV took off, compromised to hedge there bets to be between 4:3 and 16:9″.
The final critical bit of information about a DVD is the “aspect ratio” (not of the overall image, confusingly, but of each encoded *pixel*!) This says “wait, this pixel isn’t square, literally, like you’d think if you just read the image — it’s actually supposed to be stretched to make the *overall image* either 640 pixels wide (squoosh down from 720) or 854 (stretch wider to 854). So the video track is “flagged” with a “pixel aspect” (often referred to as PAR (Pixel Aspect Ratio), SAR (Sample Aspect Ratio), or DAR (Display Aspect Ratio) — some of those have some slight nuances/differences, but that’s digging too deep). Anyway, neat, huh? Your anamorphic DVD is a changeling! (maybe “anamorphic” makes more sense mnemonically now 8-) (PS: “DV” video — the most common format that digital camcorders that write to tape use — is similar. 720×480 pixels/image plus a “flag” for what each pixel “shape” is).
This hook I wrote will output information about the clip (like “ffmpeg -i” will do, but in a format easier to parse) as well as information about the pixels (unlike ffmpeg). So you can then know more about a clip if you are going to do things like pull single frames/thumbnails from it or convert it to another format. We have been using this at Internet Archive for our movies for over a year and a half now and it works great!
C Code is here.
There are some instructions for compiling with an ffmpeg source at the top of the C code. (There is also an ubuntu-on-AMD compiled “.so” at that link by changing the suffix from “.c” to “.so”, FWIW)
Example invocation and output:
ffmpeg -vhook "/petabox/deriver/identify.so oldpresidio.mpeg" FFmpeg version SVN-rUNKNOWN, Copyright (c) 2000-2007 Fabrice Bellard, et al. configuration: --enable-gpl --enable-pp --enable-libvorbis --enable-libogg --enable-liba52 --enable-libdts --enable-dc1394 --enable-libgsm --disable-debug --enable-libfaac --enable-libfaad --enable-libmp3lame --enable-x264 --prefix=/usr/ libavutil version: 49.3.0 libavcodec version: 51.38.0 libavformat version: 51.10.0 built on Nov 30 2007 19:09:20, gcc: 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2) Video: mpeg2video, yuv420p, 720x480, q=2-31, 8000 kb/s width: 720 height: 480 aspect: 32/27 fps: 29.97 duration: 00:03:05.2 audio: true Failed to Configure /petabox/deriver/identify.so Failed to add video hook function: /petabox/deriver/identify.so oldpresidio.mpeg
Filed under: code code, ffmpeg, rectangular pixels, video, widescreen · 2 Comments

Looks like this is what we have been looking for. I am not entirely sure though how to install it. Just downloaded the ffmpeg trunk (0.5) and it looks like there is no more vhook directory. Can you provide step by step instructions? MUCH appreciated, thanks.
Kai
hi kai! have you been able to build it? if you are using a mac, here’s another post you may find interesting that shows how i built it on both a PPC (work) and intel (home) mac with OS-X…
http://www.dumbbunny.org/2009/04/01/ffmpeg-building-on-mac-intelx386/
at any rate, the great thing about ffmpeg v0.5 is that, so far, in all the testing i have done, it seems to *finally* report the PAR (Pixel Aspect Ratio) for an item that has “rectangular pixels” right on the command line. so we at archive.org should soon no longer need to use this “vhook” and the C code that i wrote, yay!
so, for example, here is the new output from v0.5 ffmpeg on the same video file above now:
ffmpeg -i oldpresidio.mpeg
….
Duration: 00:03:05.71, start: 0.290656, bitrate: 8070 kb/s
Stream #0.0[0x1e0]: Video: mpeg2video, yuv420p, 720×480 [PAR 32:27 DAR 16:9], 8000 kb/s, 29.97 tbr, 90k tbn, 59.94 tbc
Stream #0.1[0x1c0]: Audio: mp2, 48000 Hz, stereo, s16, 384 kb/s
…
you can parse the PAR output from the output, when it exists, to know that the pixels in the video are rectangular.
so, from some PHP code that we use at archive.org:
$filmWidth = round($filmWidth * $parW / $parH);
we would plug in our numbers from the ffmpeg info/output to be
$filmWidth = round(720 * 32 / 27)
which comes out to round(853.33333…)
or 852 (typically want to round to an even number)