Blog title updated, meet Elvis!

I feel like updating my blog’s title a bit, to reflect the evolvement of the content of my articles, and to show off a cool concise way to express yourself in Groovy. Can you guess the meaning?

It’s the “?:”, aka. the Elvis Operator, introduced in Groovy 1.5. Pretty awesome, eh?

Grails IDE – Eclipse vs. IntelliJ IDEA vs. NetBeans

I’ve written an new article on feedlr.blog. Check it out.

Downloading YouTube Videos with a Groovy one-liner, and convert it to mpeg4

Today I used LiveHTTPHeaders to take a closer look at how the youtube video streams are directed to the browser. And I found that my previous attempt actually has some redundency in it. To get the flv video stream, simply request the URL:


where vid is simply the v string in the original URL. e.g. http://www.youtube.com/watch?v=5C0I7Ef4gQI

So the simplified version is one line of code even shorter now.

def vid = (args[0] =~ (/(? new URL("http://cache.googlevideo.com/get_video?video_id=${vid}").openStream().eachByte{it -> os.write(it)}}

And now here’s a bonus. Grab ffmpeg and put the ffmpeg executable to your system path. Then add the following line to the Groovy script above, and the downloaded flv file will be converted to an mpeg4 format mpg file immediately. You can also play with many ffmpeg parameters to fine tune your convertion.

For Windows

Runtime.getRuntime().exec(['cmd', '/c', "ffmpeg -i ${vid}.flv ${vid}.mpg"] as String)

For Linux

Runtime.getRuntime().exec(['sh', '-c', "ffmpeg -i ${vid}.flv ${vid}.mpg"] as String)

Downloading YouTube Videos with a Groovy one-liner, kind of

Inspired by Downloading YouTube videos with a Perl one-liner, I’ve put together a piece of code to do the same thing with Groovy. Not as succinct as Perl. But figure out how much noise there will be in plain Java.

def vid = (args[0] =~ (/(? new URL("http://www.youtube.com/get_video?video_id=$vid&t=${(uri =~ (/(? os.write(it)}}

Let’s take a closer look at the code. To capture the redirect location, we must temporarily disallow HttpURLConnection to follow redirects.


Then we extract the v id from the original YouTube URL and compose the URI for the embedded player. Open a connection to the video URI and extract the redirect location from the HTTP header. The input parameter arg[0] should be something like http://www.youtube.com/watch?v=5C0I7Ef4gQI

def vid = (args[0] =~ (/(?<=v=).*$/)).getAt(0)
def uri = ((HttpURLConnection)new URL("http://www.youtube.com/v/$vid").openConnection()).getHeaderField('Location')

Now we must enable http redirects again, otherwise the last line of code won’t work.


Finally, go ahead to compose the video data download URI, open a connection and download the data stream.

new File("${vid}.flv").withOutputStream{os -> new URL("http://www.youtube.com/get_video?video_id=$vid&t=${(uri =~ (/(? os.write(it)}}

Save the script to a groovy file like “YoutubeSaver.groovy”. Run the script from command line and give the video URL as the parameter. For example,

groovy youtubesaver http://www.youtube.com/watch?v=5C0I7Ef4gQI

Browse Happy logo

My tweets

« 八    



Fancy Stats