Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Universal configuration monitoring and system of record for IT.
Hide

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:

Hide

News

May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Hide

Top Tags

Hide

Functions

Psst. Open beta.

Wow, didn't really expect you to read this far down. The latest iteration of the site is in open beta. It's a gentle open beta-- not in prime-time just yet. It's being hosted over at UpGuard (link) and you are more than welcome to give it a shot. Couple things:

  • » The open beta is running a copy of the database that will not carry over to the final version. Don't post anything you don't mind losing.
  • » If you wish to use your user account, you will probably need to reset your password.
Your feedback is appreciated via the form on the beta page. Thanks! -Jon & CLFU Team

Stream YouTube URL directly to mplayer.

Terminal - 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 '^[^|,]*')
2009-03-09 03:57:44
User: lrvick
Functions: echo grep sed
58
Stream YouTube URL directly to mplayer.

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

Alternatives

There are 6 alternatives - vote for the best!

Terminal - Alternatives
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')
2009-08-13 14:16:01
User: matthewbauer
Functions: id sed
12

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.

mplayer -fs -cookies -cookies-file /tmp/cookie.txt $(youtube-dl -g --cookies /tmp/cookie.txt "http://www.youtube.com/watch?v=PTOSvEX-YeY")
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).*$/')")
2010-09-17 17:06:55
User: lslah
Functions: echo
6

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=.*?)&.*$/')")

Know a better way?

If you can do better, submit your command here.

What others think

Works great!

Comment by kpax 403 weeks and 5 days ago

This is so awesome. It takes all the pain out of youtube. I was planning to do something like this, but figured the procedure would be *way* more complicated than a call to Sed. Much thanks for showing us this, Irvick.

Just thought I'd note that getting the same command to download HQ takes only a minor twist at the end of the echo:

mplayer $(echo -s "http://youtube.com/get_video.php?$(curl $youtube_url | sed -n "/watch_fullscreen/s;.*\(video_id.\+\)&title.*;\1;p")&fmt=22")

...And &fmt=6 for HQ.

Also, if you want to download videos, just add the -dumpstream option to mplayer.

Best command ever! :D

Comment by dnavarre 402 weeks and 6 days ago

This is the freakest comand in this whole site :)

Comment by kovan 397 weeks and 1 day ago

Great command ! Thanks so much.

Using '-vo caca', I can finally watch youtube videos in ascii art :P.

Comment by pyrho 396 weeks and 3 days 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?

Comment by wrongloop 394 weeks and 3 days ago

This is a very useful command- but I found that VLC opens Youtube URLs directly. Much simpler!

vlc youtube-url-here
Comment by polar 389 weeks and 2 days ago

Looks like they've started randomizing the variable order, so a somewhat different approach is required. sed is still key though.

I ended up writing a few functions that you can put in a file. You'll need to have tidy installed to run this code, although it might work anyway if you just take out the call to tidy; I didn't try that. Anyway here it is:

[code]

#!/bin/bash

# emit a last gasp of information

function croak {

echo "$0: $2" >&2; exit $1;

}

# parse incoming html and return evaluable bash code setting the t and video_id variables

function get_params {

grep '^[ ]*var[ ]\+swfArgs[ ]*=[ ]*{[ ]*' \

| sed 's/.*{\s*//; # remove begin section

s/}\s*;\s*$//; # remove end section

s/"\([^"]*\)"\s*:\s*\("[^"]*"\)\s*,\?\s*/\1=\2\n/g # translate js array mappings into bash assignments' \

| egrep '^(t|video_id)=' \

|| croak 1 "couldn't grep url parameters; pipe status='${PIPESTATUS[@]}'";

}

# get the html and make it parseable

function get_html {

{ curl -s "$1" || croak 2 "error $? from curl"; } | tidy -quiet -wrap 0 2>/dev/null;

}

# fetch the frontend web page and parse the backend url out of it

function get_url {

eval "$(get_html "$1" | get_params)";

echo "

}

[/code]

so put the above in a file, and then source it with

source get_youtube.bash

or whatever you called it.

Then you can do

mplayer "$(get_url "$youtube_url")"

Which yeah might be less than 14 characters if you renamed the variable and the function. Is that the point? You could rewrite the whole thing into an single-line intestinal clog of jibberish... but given that it will probably break in a month anyway it seems better to have it be modular so you can fix it quickly.

Though actually it might be much more modular to just install the "youtube-dl" package, or yeah use vlc -- thanks for the tip by the way. Seems a bit more Sun Tzu than Shao Lin, but who's complaining?

Anyway this is perhaps an interesting example of using sed to generate what's basically an rc file. It should in theory exit on parsing errors instead of causing random code to be executed from your shell prompt.. but I wouldn't run it sudo.

It may not be particularly portable, I'm pretty Linux-centric, specifically Ubuntu. Advisements will be taken under advisement.

PS Hope that turned out okay presentation-wise.. are there tags to delimit code on this site? I used "[code] ... [/code]" ...as you can see if it didn't work.

Comment by intuited 376 weeks and 6 days ago

hmm.. i guess not.

how about

#!/bin/bash # emit a last gasp of information function croak { echo "$0: $2" >&2; exit $1; } # parse incoming html and return evaluable bash code setting the t and video_id variables function get_params { grep '^[ ]*var[ ]\+swfArgs[ ]*=[ ]*{[ ]*' \ | sed 's/.*{\s*//; # remove begin section s/}\s*;\s*$//; # remove end section s/"\([^"]*\)"\s*:\s*\("[^"]*"\)\s*,\?\s*/\1=\2\n/g # translate js array mappings into bash assignments' \ | egrep '^(t|video_id)=' \ || croak 1 "couldn't grep url parameters; pipe status='${PIPESTATUS[@]}'"; } # get the html and make it parseable function get_html { { curl -s "$1" || croak 2 "error $? from curl"; } | tidy -quiet -wrap 0 2>/dev/null; } # fetch the frontend web page and parse the backend url out of it function get_url { eval "$(get_html "$1" | get_params)"; echo "http://youtube.com/get_video.php?t=$t&video_id=$video_id"; }
Comment by intuited 376 weeks and 6 days ago

hmm. well, it did have indentation.

Comment by intuited 376 weeks and 6 days ago

@intuited:

Could you try compiling that into a one liner?

Comment by matthewbauer 367 weeks and 6 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.

Comment by argv 324 weeks and 2 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 :-)

Comment by lrvick 300 weeks ago

+1 for the youtube-dl option, makes format selection easy. Need it for my old computer :-).

Comment by thor 227 weeks and 3 days ago

youtube-dl was the only one wich works in the first time. My vote for that.

Comment by goviedo 165 weeks and 4 days ago

2016-1-26 xiaozhengm

http://www.coachfactory--outletonline.com

http://www.fitflopuk.me.uk

http://www.michaelkorsukbag.co.uk

http://www.get-coachoutletonline.org

http://www.michaelkors-handbag.me.uk

http://www.runningshoes.com.co

http://www.toms-outlet.cc

http://www.ugg-boots-store.com

http://www.louboutinshoes.cc

http://canadagoose.outletmalls.us.com

http://www.nike-blazerlow.fr

http://uggs.uggaustralia.qc.com

http://www.nikestoreuk.me.uk

http://www.true-religionjeans.net.co

http://coach.outletmalls.us.com

http://www.adidasshoes.name

http://michaelkors.adsmall.us.com

http://www.hermesbelt.in.net

http://www.mulberry-bags.org.uk

http://coach.eshopping.us.com

http://louisvuitton.outletmalls.us.com

http://www.hermesuk.org.uk

http://www.katespadeoutletsale.in.net

http://toms.adsmall.us.com

http://www.fitflopssaleclearance.com

http://michaelkors.eshopping.us.com

http://ugg.adsmall.us.com

http://www.louis-vuitton-outlet.us.com

http://www.canadagoosejackets.org.uk

http://ugg.eshopping.us.com

http://www.timberlanduk.me.uk

http://www.jordan13.org

http://christianlouboutin.outletmalls.us.com

http://www.coachoutlet-storeonline.in.net

http://www.instylermax.in.net

http://www.coachfactoryoutletus.in.net

http://www.coachoutlet-storeonline.us.com

http://tiffany.outletmalls.us.com

http://www.louboutin.org.uk

http://www.monclerjacketsoutlet.us.com

http://louboutin.adsmall.us.com

http://www.louisvuitton-borse.it

http://www.mcmhandbags.name

http://www.truereligion-outlet.com

http://www.new-balanceoutlet.us

http://www.louisvuitton-sac-pascher.fr

http://www.outlet-toms.cc

http://canadagoose.shopgioia.com

http://www.coach-factory-outlet-online.us.com

http://www.top-coachoutletstore.org

http://www.edhardy-outlet.name

http://www.jordan6.net

http://www.longchamphandbags.com.co

http://www.chaussurelouboutin-pascher.fr

http://www.ghdstraighteners.me.uk

http://www.nike-tn-pascher.fr

http://www.saclongchamp-solde.fr

http://www.coachcanadaoutlet.ca

http://burberryoutlet.outletmalls.us.com

http://sunglasses.outletmalls.us.com

http://www.michael--korsoutlet.us.com

http://www.nikefreeruns.uk

http://michaelkors.outletmalls.us.com

http://www.mk-michaelkorsoutletonline.com

http://www.cheaplouisvuittonhandbags.org

http://burberry.eshopping.us.com

http://www.michaelkors-outletonlines.in.net

http://www.airjordan-pascher.fr

http://www.ralphlaurenuk.me.uk

http://www.moncler-jackets.org

http://www.adidassuperstar.co.uk

http://abercrombie.shopgioia.com

http://www.authenticlouisvuittonhandbags.net

http://www.airforce1-nike.fr

http://www.coach-factoryoutlet.cc

http://www.coachoutletonlinespick.org

http://www.coachouletbtf.com

http://rayban.eshopping.us.com

http://www.ghdhairstraighteners.com.co

http://www.ralph-lauren-pas-cher.fr

http://ralphlauren.shopgioia.com

http://rayban.adsmall.us.com

http://www.mcmoutlet.us

http://www.louisvuittonuk.me.uk

http://www.jordan-femmepascher.fr

http://www.toms.in.net

http://burberry.outletmalls.us.com

http://www.cheap-uggsonsale.in.net

http://oakley.adsmall.us.com

http://www.basketballshoes.net.co

http://ugg.shopgioia.com

http://www.raybansoutlet.in.net

http://www.mizunorunningshoes.in.net

http://www.pandoracharms.com.co

http://www.truereligion-outlet.in.net

http://www.michaelkorsoutletoff.in.net

http://www.tomswedges.in.net

http://ugg.outletmalls.us.com

http://www.cheapjordansshoes.in.net

http://www.mymichaelkorssoutlets.org

http://www.airjordan-uk.org.uk

http://www.michaelkors-handbags.me.uk

http://www.nikeblazers.me.uk

http://burberry.shopgioia.com

http://timberland.adsmall.us.com

http://coach.adsmall.us.com

http://www.nike-airmax90.me.uk

http://www.nikesb.uk

http://www.nikerosherunwomen.me.uk

http://www.christianlouboutinuk.me.uk

http://www.ugg-boots-australia.com

http://www.adidas-trainers.me.uk

http://www.michaelkors-discount.com

http://www.polooutlet.us.com

http://www.niketrainers.com.co

http://www.airmax95.org.uk

http://www.montblancpens.us.org

http://www.adidasuk.org.uk

http://www.poloralphlauren.net.co

http://www.rolex-replicawatches.com.co

http://www.ralph-lauren.me.uk

http://www.jordan3.net

http://www.cheapwholesaleuggboots.com

http://hollister.shopgioia.com

http://www.michaelkorshandbags.cc

http://www.fitflops.in.net

http://www.snapbacks-wholesale.us

http://www.edhardyukclothing.me.uk

http://www.michaelkors-outlet-handbags.com

http://www.fitflops.us

http://www.raybans.org.uk

http://northface.outletmalls.us.com

http://nike.outletmalls.us.com

http://www.michaelkorsoutletforus.com

http://www.toryburchoutlet-sale.in.net

http://www.juicy-couture.us

http://www.raybansunglassesdeals.com.co

http://www.louis-vuittonhandbags.us

http://www.quality-outlet.com

http://christianlouboutin.eshopping.us.com

http://www.cheap-tomsoutlet.us

http://www.longchampoutlet.net.co

http://www.michaelkors-bags.me.uk

http://www.louisvuittonoutlets.name

http://www.coach-factoryoutlet.eu.com

http://www.tomsshoes.us.com

http://www.canadagooseoutlet.cc

http://canadagoose.adsmall.us.com

http://www.adidasgazelle.me.uk

http://christianlouboutin.shopgioia.com

http://www.kate-spadeoutlet.com.co

http://www.pradaoutlet.name

http://www.guccioutlet.com.so

http://www.jordan4.net

http://canadagoose.eshopping.us.com

http://www.michael-korshandbags.net.co

http://www.soccer-jerseys.us.com

http://www.nikerosheruns.me.uk

http://www.truereligion-outlet.net.co

http://oakley.shopgioia.com

http://www.sunglass-outlet.us

http://www.chiflatiron.in.net

http://www.tomsoutletstore.in.net

http://www.coachoutletstoreonlines.us

http://www.gucci-borse.it

http://www.jordan8.net

http://www.michaelkors-mkoutlet.in.net

http://www.chaussure-louboutin.fr

http://www.michaelkors-outletsale.com

http://www.michael-korsuk.me.uk

http://www.nike-airmax.org.uk

http://burberry.adsmall.us.com

http://www.montblanc-pens.name

http://www.sac-longchamppliage.fr

http://www.saclongchamp--pascher.fr

http://www.michaelkorsoutletvip.in.net

http://www.cheapjordans.com.co

http://www.louisvuitton-outletstores.org

http://www.marc-jacobs.in.net

http://www.pandorajewelryoutlet.name

http://www.christianlouboutinsell.net

http://www.michael-kors-handbags.uk

http://rayban.shopgioia.com

http://www.oakleysunglassesfake.in.net

http://www.kate-spadeoutlet.us

http://www.coach-factoryoutlet.com.co

http://www.gucci-outlet.org

http://www.adidas-shoes.me.uk

http://ralphlauren.eshopping.us.com

http://oakley.eshopping.us.com

http://www.vipmichaelkors-outletonline.com

http://www.canadagooseuk.me.uk

http://www.oakley--sunglasses.us.org

http://www.kate-spadeoutlet.in.net

http://truereligion.adsmall.us.com

http://jordan.outletmalls.us.com

http://www.guccioutlet.net.co

http://www.nikeuk.me.uk

http://www.katespadeuk.me.uk

http://www.christianlouboutinoff.us

http://www.nikecortez.com.co

http://www.nikehuarache.com.co

Comment by xiaozhengm 44 weeks and 4 days ago

Your point of view

You must be signed in to comment.