MPlayer youtube script: Difference between revisions

From MultimediaWiki
Jump to navigation Jump to search
No edit summary
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
 
===Alternative Version===
Here is an alternative version by [[User:Elte|Elte]].


The differences are
=== Playing on-the-fly ===
* fewer programm calls
* less shell pipes
* no dependency on grep, awk or tr
* supports double quotes in video url


This version is nevertheless heavily based on the previous script.
To play video on-the-fly without downloading you need simple shell script:
<pre>
<pre>
#!/bin/bash
#!/bin/sh
# © 2008,2009 Elte GPL V2.1
#
 
# Public domain
URL="$1"
# Author: roman [] tsisyk.com
shift
#
 
# Usage: ./me url [youtube-dl parameters]
if [ "${URL#http://}" = "$URL" ]
#
then
    echo "usage: $0 <youtube-URL> [mplayer args ...]"
    exit
fi


echo \"http://www.youtube.com/get_video?video_id=$(wget -q -O - "$URL"                    \
COOKIE_FILE=/var/tmp/youtube-dl-cookies.txt
                                                  | sed -e '/fullscreenUrl/!d'            \
mplayer -cookies -cookies-file ${COOKIE_FILE} $(youtube-dl -g --cookies ${COOKIE_FILE} $*)
                                                        -e "s/.*video_id=\([^']*\).*/\1/" \
                                                        -e 's/ /_/g'                      \
                                                        -e 's/\\\"/"/g'                  \
                                                  )\"                                      \
| xargs mplayer "$@"
</pre>
</pre>


===Or just use youtube-dl===
Save this script to somewhere in your PATH (like /usr/local/bin/youtube-mplayer) and make it executable (chmod a+x youtube-mplayer).
Available in almost all Linux distributions.
 
Usage:
 
mplayer $(youtube-dl -g http://www.youtube.com/watch?v=.....)
 
or
 
mplayer $(youtube-dl -b -g http://www.youtube.com/watch?v=.....)
 
to get the HD version if available.


To avoid seeking issues, try updating to latest SVN. 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).
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.


If that does not help, try configuring MPlayer with http support via FFmpeg (--enable-protocol="http_protocol" or --enable-protocol="file_protocol pipe_protocol http_protocol rtmp_protocol tcp_protocol udp_protocol") and use instead
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].


mplayer ffmpeg://$(youtube-dl -b -g http://www.youtube.com/watch?v=.....)
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 aviable from Wikipedia article [http://en.wikipedia.org/wiki/Features_of_YouTube#ref_media_type_table_note_1].

Revision as of 23:17, 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 aviable from Wikipedia article [5].