MPlayer youtube script

From MultimediaWiki
Jump to navigation Jump to search

bash script for playing youtube videos

  1. save it as
  2. chmod +x
  3. place it somewhere in PATH (like /usr/local/bin )



if [ -z "$1" ]; then
        echo "No URL!"


echo \"`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 "" 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 #" 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). 

created by enouf and n3kl and amphi

RE-modified by enouf

Alternative Version

Here is an alternative version by Elte.

The differences are

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

# © 2008,2009 Elte GPL V2.1


if [ "${URL#http://}" = "$URL" ]
    echo "usage: $0 <youtube-URL> [mplayer args ...]"

echo \"$(wget -q -O - "$URL"                     \
                                                   | sed -e '/fullscreenUrl/!d'            \
                                                         -e "s/.*video_id=\([^']*\).*/\1/" \
                                                         -e 's/ /_/g'                      \
                                                         -e 's/\\\"/"/g'                   \
                                                  )\"                                      \
| xargs mplayer "$@"

Or just use youtube-dl

Available in almost all Linux distributions.


mplayer $(youtube-dl -g


mplayer $(youtube-dl -b -g

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

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

mplayer ffmpeg://$(youtube-dl -b -g