---
title: "Downloading YouTube Videos with a Groovy one-liner, and convert it to mpeg4"
author: "ev0"
date: "2007-08-15T14:31:23Z"
tags: []
slug: "downloading-youtube-videos-with-a-groovy-one-liner-and-convert-it-to-mpeg4"
draft: false
lastmod: "2007-08-15T14:31:23Z"
id: 112877
url: "https://ev0.iteye.com/blog/112877"
---

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](http://blog.lifedreamr.com/?p=44) actually has some redundency in it. To get the flv video stream, simply request the URL:

```
http://cache.googlevideo.com/get_video?video_id=${vid}
```

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

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

```
def vid = (args[0] =~ (/(?<=v=).*$/)).getAt(0)
```

```
new File(“${vid}.flv”).withOutputStream{os -> 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](http://ffdshow.faireal.net/mirror/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)
```

[![](http://feeds.feedburner.com/~f/intrc?i=TF8UySFa)](http://feeds.feedburner.com/~f/intrc?a=TF8UySFa) [![](http://feeds.feedburner.com/~f/intrc?i=NXxC1Mpi)](http://feeds.feedburner.com/~f/intrc?a=NXxC1Mpi) [![](http://feeds.feedburner.com/~f/intrc?i=iMqmVDPF)](http://feeds.feedburner.com/~f/intrc?a=iMqmVDPF) [![](http://feeds.feedburner.com/~f/intrc?i=GDbG3MVV)](http://feeds.feedburner.com/~f/intrc?a=GDbG3MVV) [![](http://feeds.feedburner.com/~f/intrc?i=oMtWEFMH)](http://feeds.feedburner.com/~f/intrc?a=oMtWEFMH) [![](http://feeds.feedburner.com/~f/intrc?i=7XMsGEMC)](http://feeds.feedburner.com/~f/intrc?a=7XMsGEMC)

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