MPlayer youtube script: Difference between revisions

From MultimediaWiki
Jump to navigation Jump to search
No edit summary
m (spelling)
 
(19 intermediate revisions by 5 users not shown)
Line 1: Line 1:
bash script for playing youtube videos
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.


usage:
youtube-dl is a small console program written in Python for downloading videos from YouTube, Google Video, Metacafe, etc.
*save it as youtube.sh
 
*chmod +x youtube.sh
youtube-dl can be found in your favorite distro repository or cloned from Git [https://github.com/rg3/youtube-dl/] (if you don't know what is the Git or don't want to use it, download only one raw script from [http://rg3.github.com/youtube-dl/download.html])
*place it somewhere in PATH (like /usr/local/bin )
 
*youtube.sh http://www.youtube.com/watch?v=example
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).
*youtube.sh http://www.youtube.com/watch?v=example -dumpstream -dumpfile something.flv
 
*youtube.sh http://www.youtube.com/watch?v=example -aspect 16:9
=== Downloading and playing ===
*youtube.sh http://www.youtube.com/watch?v=example -xy 2
 
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.


<pre>
<pre>
#!/bin/bash
youtube-dl "http://www.youtube.com/watch?v={video_id}"
</pre>
 
See youtube-dl --help for other options.


if [ -z "$1" ]; then echo "No URL!"; exit; fi
=== Playing on-the-fly ===


url=$1
To play video on-the-fly without downloading you need simple shell script:
shift
<pre>
#!/bin/sh
#
# Public domain
# Author: roman [] tsisyk.com
#
# Usage: ./me url [youtube-dl parameters]
#


echo \"http://www.youtube.com/get_video?video_id=`wget -q -O - $url | grep fullscreenUrl | awk -F'video_id=' '{ print $2 }' | sed -e 's/ /_/g' | tr -d \'\; `\" | xargs mplayer $*
COOKIE_FILE=/var/tmp/youtube-dl-cookies.txt
mplayer -cookies -cookies-file ${COOKIE_FILE} $(youtube-dl -g --cookies ${COOKIE_FILE} $*)
</pre>
</pre>


created by enouf and n3kl and amphi
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 [http://git.mplayerhq.hu/?p=mplayer;a=commit;h=61aab8f05cb26a124fa8e4d100809a2c512b8ca4]). 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) [http://lists.mplayerhq.hu/pipermail/mplayer-users/2010-July/080653.html].
 
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 [http://en.wikipedia.org/wiki/Features_of_YouTube#ref_media_type_table_note_1].

Latest revision as of 23:19, 9 January 2011

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].