MPlayer youtube script: Difference between revisions

From MultimediaWiki
Jump to navigation Jump to search
(support double quote in video url)
m (spelling)
 
(6 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
*youtube.sh http://www.youtube.com/watch?v=example -aspect 16:9
*youtube.sh http://www.youtube.com/watch?v=example -xy 2


<pre>
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).
#!/bin/bash


if [ -z "$1" ]; then
=== Downloading and playing ===
        echo "No URL!"
        exit
fi


url=$1
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.
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 $*
 
###
# 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 #"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
# 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>
youtube-dl "http://www.youtube.com/watch?v={video_id}"
</pre>
</pre>


created by enouf and n3kl and amphi
See youtube-dl --help for other options.


RE-modified by enouf
=== Playing on-the-fly ===


===Alternative Version===
To play video on-the-fly without downloading you need simple shell script:
Here is an alternative version by [[User:Elte|Elte]].
<pre>
#!/bin/sh
#
# Public domain
# Author: roman [] tsisyk.com
#
# Usage: ./me url [youtube-dl parameters]
#


The differences are
COOKIE_FILE=/var/tmp/youtube-dl-cookies.txt
* fewer programm calls
mplayer -cookies -cookies-file ${COOKIE_FILE} $(youtube-dl -g --cookies ${COOKIE_FILE} $*)
* less shell pipes
</pre>
* no dependency on grep, awk or tr
* supports double quotes in video url


This version is nevertheless heavily based on the previous script.
Save this script to somewhere in your PATH (like /usr/local/bin/youtube-mplayer) and make it executable (chmod a+x youtube-mplayer).
<pre>
#!/bin/bash
# © 2008,2009 Elte GPL V2.1


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


if [ "${URL#http://}" = "$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].
then
    echo "usage: $0 <youtube-URL> [mplayer args ...]"
    exit
fi


echo \"http://www.youtube.com/get_video?video_id=$(wget -q -O - "$URL"                    \
By default youtube-dl gives URL for best aviable format, if you want smaller version, please provide -f flag to youtube-dl.
                                                  | sed -e '/fullscreenUrl/!d'            \
List of all format codes available from Wikipedia article [http://en.wikipedia.org/wiki/Features_of_YouTube#ref_media_type_table_note_1].
                                                        -e "s/.*video_id=\([^']*\).*/\1/" \
                                                        -e 's/ /_/g'                      \
                                                        -e 's/\\\"/"/g'                      \
                                                  )\"                                      \
| xargs mplayer "$@"
</pre>

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