MPlayer youtube script: Difference between revisions

From MultimediaWiki
Jump to navigation Jump to search
No edit summary
m (spelling)
 
(8 intermediate revisions by 3 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.


#save it as youtube.sh
youtube-dl is a small console program written in Python for downloading videos from YouTube, Google Video, Metacafe, etc.
#chmod +x youtube.sh
#place it somewhere in PATH (like /usr/local/bin )


Usage:
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])
*youtube.sh <url> [mplayer args]
 
*youtube.sh http://www.youtube.com/watch?v=example -dumpstream -dumpfile something.flv
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 -aspect 16:9
 
*youtube.sh http://www.youtube.com/watch?v=example -xy 2
=== 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.


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


if [ -z "$1" ]; then
See youtube-dl --help for other options.
        echo "No URL!"
        exit
fi


url=$1
=== Playing on-the-fly ===
shift


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


###
COOKIE_FILE=/var/tmp/youtube-dl-cookies.txt
# The scipt above grabs the html source to the real stream (the .flv file), which youtube constantly alters. The "http://www.youtube.com/get_video?video_id=" is always the prefix -- and the script uses wget to d/l and append the rest of the 'full' url (which is very long and stupid).. an ex.;
mplayer -cookies -cookies-file ${COOKIE_FILE} $(youtube-dl -g --cookies ${COOKIE_FILE} $*)
</pre>


#mplayer #"http://www.youtube.com/get_video?video_id=L2SED6sewRw&l=2965&sk=GOT2L_qmpVJOx0w#rud5ycdyuWziPg1lcC&fmt_map=6%2F720000%2F7%2F0%2F0&t=OEgsToPDskLlf4ls3xB6V84dMYLu#ndws&hl=en&plid=AARTXK76vCTnvQ5#JAAAC6ADCAAA&sdetail=rv%253AL2S#ED6sewRw&tk=P4Lg#O65y-u5BllZJBsB_e2Gw-OVgaMp4a8prsHTahDhuPN_xsReW2Q%3D%3D&title=Greg Kroah
Save this script to somewhere in your PATH (like /usr/local/bin/youtube-mplayer) and make it executable (chmod a+x youtube-mplayer).
# Hartman on the Linux Kernel"
# as you can see, there are blank spaces also that need to be cleaned up (blank # # spaces replaced with underscores).  


</pre>
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.


created by enouf and n3kl and amphi
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].


RE-modified by enouf
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].