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:
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
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 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)
本文介绍了一种简化的方法来抓取YouTube上的FLV视频流,并通过一行Groovy代码实现。此外,还提供了使用ffmpeg将下载的FLV文件即时转换为MPEG4格式的方法。
6969

被折叠的 条评论
为什么被折叠?



