Stream YouTube URL directly to MPlayer

yt () mplayer -fs -quiet $(youtube-dl -g "$1")
A function for streaming youtube to mplayer. The option "-g" for youtube-dl tells it to output the direct video URL, instead of downloading the video. "-fs" tells MPlayer to go FullScreen, and "-quit" makes it less verbose. Requires: youdube-dl ( http://bitbucket.org/rg3/youtube-dl/ ) (Tested in zsh)
Sample Output
20:33:16 Wed 29 [~] elfreak : pts/6 on machine
$ yt "http://www.youtube.com/watch?v=1ZvCqslpp6M"                                           
MPlayer SVN-r31774-4.5.0 (C) 2000-2010 MPlayer Team
158 audio & 340 video codecs

Playing http://www.youtube.com/get_video?video_id=1ZvCqslpp6M&t=vjVQa1PpcFOoqGwpVRIy9cfN57JrQuZ8rvLNfyXM5lQ=&eurl=&el=&ps=&asv=&fmt=22.
Resolving www.youtube.com for AF_INET6...

Couldn't resolve name for AF_INET6: www.youtube.com
Resolving www.youtube.com for AF_INET...
Connecting to server www.youtube.com[74.125.77.190]: 80...
Resolving v17.lscache8.c.youtube.com for AF_INET6...

Couldn't resolve name for AF_INET6: v17.lscache8.c.youtube.com
Resolving v17.lscache8.c.youtube.com for AF_INET...
Connecting to server v17.lscache8.c.youtube.com[74.125.8.82]: 80...
Resolving r17.ams04s02.c.youtube.com for AF_INET6...

Couldn't resolve name for AF_INET6: r17.ams04s02.c.youtube.com
Resolving r17.ams04s02.c.youtube.com for AF_INET...
Connecting to server r17.ams04s02.c.youtube.com[74.125.8.96]: 80...

Cache size set to 320 KBytes
Cache fill:  0.00% (0 bytes)   

libavformat file format detected.
[lavf] stream 0: audio (aac), -aid 0, -alang und
[lavf] stream 1: video (h264), -vid 0
VIDEO:  [H264]  1280x720  24bpp  29.917 fps  1105.0 kbps (134.9 kbyte/s)
Clip info:
 major_brand: mp42
 minor_version: 0
 compatible_brands: isomavc1mp42
==========================================================================
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
Selected video codec: [ffh264] vfm: ffmpeg (FFmpeg H.264)
==========================================================================
==========================================================================
Opening audio decoder: [faad] AAC (MPEG2/4 Advanced Audio Coding)
AUDIO: 44100 Hz, 2 ch, s16le, 125.6 kbit/8.90% (ratio: 15697->176400)
Selected audio codec: [faad] afm: faad (FAAD AAC (MPEG-2/MPEG-4 Audio))
==========================================================================
[AO OSS] audio_setup: Can't open audio device /dev/dsp: Device or resource busy
AO: [alsa] 48000Hz 2ch s16le (2 bytes per sample)
Starting playback...
Movie-Aspect is 1.78:1 - prescaling to correct movie aspect.
VO: [xv] 1280x720 => 1280x720 Planar YV12  [fs]
[ASPECT] Warning: No suitable new res found!
[ASPECT] Warning: No suitable new res found!

Exiting... (Quit)

15
By: elfreak
2010-09-29 18:48:19

These Might Interest You

  • Streams youtube video with v=ID directly into the mplayer. If exists, it uses the HD-quality stream. If you don't want to watch it in HD-quality, you can use the shorter form: ID=52DnUo6wJto; mplayer -fs $(echo "http://youtube.com/get_video.php?&video_id=$ID$(wget -qO - 'http://youtube.com/watch?v='$ID | perl -ne 'print $1."&asv=" if /^.*(&t=.*?)&.*$/')")


    5
    ID=52DnUo6wJto;mplayer -fs $(echo "http://youtube.com/get_video.php?&video_id=$ID$(wget -qO - 'http://youtube.com/watch?v='$ID | perl -ne 'print $1."&asv=" if /^.*(&t=.*?)&.*$/; print "&fmt=".$1 if /^.*&fmt_map=(22).*$/')")
    lslah · 2010-09-17 17:06:55 0
  • The original doesn't work for me - but this does. I'm guessing that Youtube updated the video page so the original doesn't work.


    11
    id="dMH0bHeiRNg";mplayer -fs http://youtube.com/get_video.php?video_id=$id\&t=$(curl -s http://www.youtube.com/watch?v=$id | sed -n 's/.*, "t": "\([^"]*\)", .*/\1/p')
    matthewbauer · 2009-08-13 14:16:01 0

  • 6
    mplayer -fs -cookies -cookies-file /tmp/cookie.txt $(youtube-dl -g --cookies /tmp/cookie.txt "http://www.youtube.com/watch?v=PTOSvEX-YeY")
    davro · 2011-07-19 23:53:01 0
  • This is the result of a several week venture without X. I found myself totally happy without X (and by extension without flash) and was able to do just about anything but watch YouTube videos... so this a the solution I came up with for that. I am sure this can be done better but this does indeed work... and tends to work far better than YouTube's ghetto proprietary flash player ;-) Replace $i with any YouTube ID you want and this will scrape the site for the _real_ URL to the full quality .FLV file on Youtube's server and will then will hand that over to mplayer (or vlc or whatever you want) to be streamed. In some browsers you can replace $i with just a % or put this in a shell script so all YouTube IDs can be handed directly off to your media player of choice for true streaming without the need for Flash or a downloader like clive. (I do however fully recommend clive if you wish to archive videos instead of streaming them) If any interest is shown I would be more than happy to provide similar commands for other sites. Most streaming flash players use similar logic to YouTube. Edit: 05/03/2011 - Updated line to work with current YouTube. It could be a lot prettier but I will probably follow up with another update when I figure out how to get rid of that pesky Grep. Sed should take that syntax... but it doesn't. Original (no longer working) command: mplayer -fs $(echo "http://youtube.com/get_video.php?$(curl -s $youtube_url | sed -n "/watch_fullscreen/s;.*\(video_id.\+\)&title.*;\1;p")") Show Sample Output


    57
    i="8uyxVmdaJ-w";mplayer -fs $(curl -s "http://www.youtube.com/get_video_info?&video_id=$i" | echo -e $(sed 's/%/\\x/g;s/.*\(v[0-9]\.lscache.*\)/http:\/\/\1/g') | grep -oP '^[^|,]*')
    lrvick · 2009-03-09 03:57:44 15

What Others Think

now, if only I could define which monitor to output to...
zed · 403 weeks and 1 day ago
-xineramascreen 1 :)
zed · 403 weeks and 1 day ago
404 on your link
TheMightyBuzzard · 403 weeks ago
@TheMightyBuzzard: Thank you, comandlinefu parsed the last parentheses as part of the URL. Now fixed.
elfreak · 403 weeks ago
Unfortunately, this entry did not get listed as an alternative to the existing YouTube / MPlayer one-liners (my bad). Any way to do that now?
elfreak · 403 weeks ago
should I firs make an alias from this comand? How should I invoke this? I will be appreciate for details.
noisy · 403 weeks ago
yt is a function. You either add it (as it is) to your .bashrc / .zshrc, or just paste it in a terminal (it will be available for that session only). After that, as you can see from the sample output, you invoke it by typing yt and the YouTube URL: yt "http://www.youtube.com/watch?v=1ZvCqslpp6M" (The quotation signs are needed because zsh interprets the '?' as a special character)
elfreak · 403 weeks ago
I don't get the usefulness of this command. I download videos for two reasons: 1. I one to keep them on my disc to watch them again later, and 2. When streaming on browser is slow. So I don't get the point of this. If you're going to stream it, then just see it on the browser.
conan · 402 weeks and 6 days ago
just what I was looking for. thanks :)
erikano · 402 weeks and 6 days ago
You use youtube-dl for those reasons, and if that's what you want, great ;) I made this function to avoid using YouTube's proprietary flash player. Here is a list, from the top of my head, of why: 1) I generally dislike flash, and I will do everything I can do to avoid it. 2) Fullscreen does not work well with my window manager (Xmonad). 3) I would like to be able to use my remote to control the video. 4) I want to be able to start a YouTube video on my mediapc (connected to the TV), from any of my computers. (I just SSH and give yt the link (probably going to make a script to automate that). 5) I want to be able to view YouTube videos without having X11. 6) I regularly do not want to store the videos, since I usually only watch them once. If it lags, then I will download it to /tmp. 7) I regularly do not want to wait while the video is downloading. The beauty with YouTube is that they have a massive collection of videos, instantly available to you. Bottom-line, if YouTube's proprietary flash player works for you, that's wonderful; otherwise, here's an alternative :)
elfreak · 402 weeks and 6 days ago
Wow! This is great! It works perfectly!
unixmonkey12633 · 402 weeks and 6 days ago
I have added "-vo null" option in mplayer so it does not stream video when I listen to music only! This way it does not fil in the cache quickly. (:
KoboYagee · 247 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: