`

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.

   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”
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics