Stream YouTube URL directly to mplayer.

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 '^[^|,]*')
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")")
Sample Output
MPlayer SVN-r32999-4.4.5 (C) 2000-2011 MPlayer Team

Playing http://v3.lscache2.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=901031%2C907508%2C900304&algorithm=throttle-factor&itag=34&ipbits=8&burst=40&sver=3&signature=97D6F8D1A948EF2451FD4F093ECA7EF3795EE424.73F2B07D5F478ACEAFCEAC98E7BF2623FEED20EC&expire=1299384000&key=yt1&ip=97.0.0.0&factor=1.25&id=f2ecb156675a27ec.
Resolving v3.lscache2.c.youtube.com for AF_INET6...

Couldn't resolve name for AF_INET6: v3.lscache2.c.youtube.com
Resolving v3.lscache2.c.youtube.com for AF_INET...
Connecting to server v3.lscache2.c.youtube.com[74.125.15.14]: 80...

Cache size set to 320 KBytes
Cache fill: 19.54% (64036 bytes)   

libavformat file format detected.
[flv @ 0x2acf540] Estimating duration from bitrate, this may be inaccurate
[lavf] stream 0: video (h264), -vid 0
[lavf] stream 1: audio (aac), -aid 0
VIDEO:  [H264]  640x360  0bpp  24.000 fps  557.2 kbps (68.0 kbyte/s)
Clip info:
 duration: 162
 starttime: 0
 totalduration: 162
 width: 640
 height: 360
 videodatarate: 544
 audiodatarate: 97
 totaldatarate: 649
 framerate: 24
 bytelength: 13120260
 canseekontime: true
 sourcedata: B4A7D0F03HH1299359484777471
 purl: 
 pmsg: 
==========================================================================
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
Selected video codec: [ffh264] vfm: ffmpeg (FFmpeg H.264)
==========================================================================
==========================================================================
Opening audio decoder: [ffmpeg] FFmpeg/libavcodec audio decoders
AUDIO: 44100 Hz, 2 ch, s16le, 99.0 kbit/7.02% (ratio: 12379->176400)
Selected audio codec: [ffaac] afm: ffmpeg (FFmpeg AAC (MPEG-2/MPEG-4 Audio))
==========================================================================
[AO OSS] audio_setup: Can't open audio device /dev/dsp: Device or resource busy
AO: [alsa] 44100Hz 2ch s16le (2 bytes per sample)
Starting playback...
Movie-Aspect is 1.78:1 - prescaling to correct movie aspect.
VO: [xv] 640x360 => 640x360 Planar YV12  [fs]
A:   2.4 V:   2.4 A-V: -0.000 ct:  0.000   0/  0 19% 15%  0.4% 7 0 47% 

58
By: lrvick
2009-03-09 03:57:44

3 Alternatives + Submit Alt

What Others Think

Works great!
kpax · 879 weeks and 2 days ago
This is the freakest comand in this whole site :)
kovan · 872 weeks and 5 days ago
Great command ! Thanks so much. Using '-vo caca', I can finally watch youtube videos in ascii art :P.
pyrho · 872 weeks ago
mmm awesome just what I was looking for, but; could there be a way to download a full playlists? Is too much to ask?
wrongloop · 870 weeks ago
This is a very useful command- but I found that VLC opens Youtube URLs directly. Much simpler! vlc youtube-url-here
polar · 864 weeks and 6 days ago
hmm. well, it did have indentation.
intuited · 852 weeks and 3 days ago
@intuited: Could you try compiling that into a one liner?
matthewbauer · 843 weeks and 3 days ago
hey lrvick it's great to hear someone else who appreciates a non-X, non-flash environment. youtube makes it very easy for us (one more reason i appreciate google). mplayer now hosts rtmpdump. this is an important step toward educating people about how flash is not at all needed to watch video and also how it is not securing any content... it's a marketing gimmick. but i'm not sure rtmpdump, nor the "get-flash-videos" project, makes watching video in the non-flash environment much easier. the real "work" is looking at the tcp traces. and these efforts do not address that. here's what i'd like to see: a site where people post tcp traces when they access pages with video at various video sites. looking at these traces, it is trivial to figure out how to watch without flash. getting the traces is not difficult. users could use something as easy as the livehttpheaders addon for firefox or something more traditional like tcpdump, wireshark. it doesn't matter. there are so many ways to get a trace. once there is a "database" of traces, no one will have to ever endure the flash hassle again. cheers.
argv · 799 weeks and 6 days ago
Wow all kinds of comments. I have not looked at this in ages. I went ahead and updated it to work with the current incarnation of YouTube. It could be prettier but it works. Argy: That would ultimately be the best way to go, but it is going to take some real, probably team, effort. If you set out to do anything along those lines let me know. I always love good hack :-)
lrvick · 775 weeks and 4 days ago
+1 for the youtube-dl option, makes format selection easy. Need it for my old computer :-).
thor · 703 weeks ago
youtube-dl was the only one wich works in the first time. My vote for that.
goviedo · 641 weeks 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: