---
title: "Downloading YouTube Videos with a Groovy one-liner, kind of"
author: "ev0"
date: "2007-07-29T13:33:43Z"
tags: []
slug: "downloading-youtube-videos-with-a-groovy-one-liner-kind-of"
draft: false
lastmod: "2007-07-29T13:33:43Z"
id: 106878
url: "https://ev0.iteye.com/blog/106878"
---

Inspired by [Downloading YouTube videos with a Perl one-liner](http://www.catonmat.net/blog/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.

```
   1: HttpURLConnection.setFollowRedirects(false)
```

```
   2: def vid = (args[0] =~ (/(?<=v=).*$/)).collect{it}[0]
```

```
   3: def uri = ((HttpURLConnection)new URL(“http://www.youtube.com/v/$vid”).openConnection()).getHeaderField(‘Location’)
```

```
   4: HttpURLConnection.setFollowRedirects(true)
```

```
   5: new File(“${vid}.flv”).withOutputStream{os -> new URL(“http://www.youtube.com/get_video?video_id=$vid&t=${(uri =~ (/(?<=.*&t=).*$/)).collect{it}[0]}”).openStream().eachByte{it -> 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.

```
HttpURLConnection.setFollowRedirects(false);
```

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=).*$/)).collect{it}[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.

```
HttpURLConnection.setFollowRedirects(true)
```

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 =~ (/(?<=.*&t=).*$/)).collect{it}[0]}”).openStream().eachByte{it -> 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”
```

[![](http://feeds.feedburner.com/~f/intrc?i=UspfbLwD)](http://feeds.feedburner.com/~f/intrc?a=UspfbLwD) [![](http://feeds.feedburner.com/~f/intrc?i=P3eRoY8N)](http://feeds.feedburner.com/~f/intrc?a=P3eRoY8N) [![](http://feeds.feedburner.com/~f/intrc?i=BQo6FKy7)](http://feeds.feedburner.com/~f/intrc?a=BQo6FKy7) [![](http://feeds.feedburner.com/~f/intrc?i=YrOF5g61)](http://feeds.feedburner.com/~f/intrc?a=YrOF5g61) [![](http://feeds.feedburner.com/~f/intrc?i=sBCIV0Cb)](http://feeds.feedburner.com/~f/intrc?a=sBCIV0Cb) [![](http://feeds.feedburner.com/~f/intrc?i=xpuU18Qf)](http://feeds.feedburner.com/~f/intrc?a=xpuU18Qf)

![](http://feeds.feedburner.com/~r/intrc/~4/138525459)