Most YouTube videos lack quality -- both in content and format. The low quality content, like cats playing piano, dry rants, and fake pranks, isn't even worthy of FOX News. However, because the content is so inane, few people notice that the video quality itself is lacking. YouTube downgrades videos to the point that colors blur and visual artifacts become painfully obvious. Obvious, that is, if the content was worthy enough to make the viewer care about the video quality.
A Reason To Care
I recently watched "The Phantom Menace Review" on YouTube. This is a seven-part video that is a must-see for anyone interested in writing stories, directing videos, or doing any kind of videography. Just as my blog posting on "
How Not To Do Image Analysis" tore apart Polarik's false analysis of the Obama birth certificate, the author of the Review essentially created a video on "How Not To Make A Good Movie". He dissects the storyline, characters, arc, dialogue, and other film characteristics. Moreover, he does it in a very entertaining fashion.
- Part I
- Part II
- Part III
- Part IV
- Part V
- Part VI
- Part VII
Each part of the review is 10 minutes long. You can either watch it all at once, or in parts without any loss in continuity. (Some of the language is
NSFW; I'd call it
PG13 for language.)
Quality Matters
I watched the video segments on a couple of different computers. Even though the computers were fast, the quality of the video display varied based on the computer and not the content. The video on a Windows 7 computer was a much higher quality than watching the same video on my Ubuntu Linux or Mac systems.
I decided to look a little deeper into how YouTube streams videos. Unfortunately, few YouTube videos offer options for downloading, and they use JavaScript to obscure the necessary URLs. However, using
Wireshark, I could watch the HTTP requests for downloading videos. That's where I saw the difference. Specifically: The non-Windows systems were downloading a lower quality video stream.
Dissecting YouTube
I decided to trace how a YouTube download works. You can follow along using wget.
- Download the video's HTML web page. For example, use
wget http://www.youtube.com/watch?v=ZG1AWVLnl48 to download Part I of the Phantom Menace Review. This will create an HTML file called "watch?v=ZG1AWVLnl48".
- Inside the HTML is a very long line that begins with "SWF_URL". Extract this line and decode the URI encoding.
- The long line contains field:value pairs that are comma separated ("field:" "value", "field:" "value", etc.). The only fields that seem to be important are the "t", "video_id", and "fmt_map". The "t" and "video_id" identify the video to download. However, the fmt_map is more complicated.
- Parse the fmt_map. This field stores information about the format. It consists of a comma-separated list with five values per element. The five values look kind of like "34/0/9/0/115". The first value is the format. For example, 34 is a high quality mp4. The third value is the required Flash version. In this case, you need Flash 9 or higher to play this format. The fifth value is the priority ranking. If there are two formats listed (e.g., 34/0/9/0/115,5/0/7/0/0) and you support both Flash versions, then choose the video with the highest ranking. (I don't know what positions 2 and 4 do, but they seem to always be zero.) So given 34/0/9/0/115,5/0/7/0/0, I can either use format 34 (fmt=34) with Flash 9, or fmt=5 with Flash 7.
- Put it all together into a URL: http://www.youtube.com/get_video?video_id=$video_id&fmt=$fmt&t=$t
Even though my Linux and Mac systems are very fast computers and both had Flash 10 installed, neither were showing the higher quality video. That's when it hit me: newer Flash needs newer video codecs. I updated the codecs for H.264 and other video formats (gstreamer0.10-plugins-bad_0.10.14-4ubuntu1_i386.deb, gstreamer0.10-ffmpeg_0.10.9-1_i386.deb, and related packages) and tried again. Poof: higher quality video.
Now I can finally watch a high quality video series on YouTube that is actually high quality in both content and format. If only this technique could improve the original
Star Wars: Episode I - The Phantom Menace...
As a side note, I use the great safari plugin ClickToFlash, which lets me watch YouTube videos as H.264 quicktime videos, directly in the browser. I haven't noticed any considerable loss of quality compared to my PC with Windows 7. I haven't been looking, though..
Compare http://www.youtube.com/watch?gl=NL&hl=nl&v=vgmy90H73jk&fmt=22
There are lots of fmts, and not all work with every video. Here's the ones I know of so far:
my %fmtdesc=(
37 => "1080p mp4 (full-screen HQ)",
35 => "high flv",
34 => "standard flv",
22 => "720p mp4 (full-screen HQ)",
18 => "medium mp4",
17 => "mobile 3gp",
6 => "high quality flv",
5 => "low quality flv"
);
Only #18 seems to work when it is not listed in the fmt_map, all others must be listed in order to be supported. Also, my text descriptions may not be completely accurate.