MPlayer youtube script

From MultimediaWiki
Jump to: navigation, search

At the end of 2010 (maybe earlier) YouTube introduced new security checks on their web interface. For example, YouTube CDN servers require special token and cookie which can't be extracted in easy way using only grep or awk. Therefore, all simple bash script doesn't work properly.

youtube-dl solution

Solution is using youtube-dl script (https://github.com/rg3/youtube-dl/), that looks maintained and useful for our case.

youtube-dl is a small console program written in Python for downloading videos from YouTube, Google Video, Metacafe, etc.

youtube-dl can be found in your favorite distro repository or cloned from Git [1] (if you don't know what is the Git or don't want to use it, download only one raw script from [2])

First way is most correct, but anyway, if you have already downloaded script, then save it to somewhere in PATH (e.g. /usr/local/bin) and make it executable (chmod a+x youtube-dl).

Downloading and playing

For downloading video and playing it in mplayer (or any other player) simple run youtube-dl with video url. Please don't forget quotes if your url contains ampersand symbol.

youtube-dl "http://www.youtube.com/watch?v={video_id}"

See youtube-dl --help for other options.

Playing on-the-fly

To play video on-the-fly without downloading you need simple shell script:

#!/bin/sh
#
# Public domain
# Author: roman [] tsisyk.com
#
# Usage: ./me url [youtube-dl parameters]
#

COOKIE_FILE=/var/tmp/youtube-dl-cookies.txt
mplayer -cookies -cookies-file ${COOKIE_FILE} $(youtube-dl -g --cookies ${COOKIE_FILE} $*)

Save this script to somewhere in your PATH (like /usr/local/bin/youtube-mplayer) and make it executable (chmod a+x youtube-mplayer).

This script runs youtube-dl with -g option (fetch url only) and saves all cookies to COOKIE_FILE in Netscape-compatible format. After this mplayer is started using this cookies and fetched URL.

To avoid seeking issues, try updating to at least 2010 year MPlayer SVN/Git version (see fix commit [3]). It contains a special hack that works around a bug in youtube's HTTP server (it does not set Accept-Ranges, which incorrectly indicates it does not support seeking/partial requests - MPlayer now just assumes that seeking is possible if the server string is "gvs 1.0" - which is what youtube currently uses) [4].

By default youtube-dl gives URL for best aviable format, if you want smaller version, please provide -f flag to youtube-dl. List of all format codes available from Wikipedia article [5].