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”
分享到:
相关推荐
带Lifecycle的iDrac控制器固件v2.63.60.61,firmimg.d7用于iDrac网页控制台更新,exe用于windows
Python常用库,官方原版whl文件,文件下载到本地后, 直接终端 pip install xxx.whl 安装
pandas-1.4.3-cp38-cp38-win_amd64.whl
Cython-3.0.0a9-cp310-cp310-win_amd64
downloading-web-page-source-code-android-app:一个可下载指定网页源代码的android应用
Gets the number of child objects of the designated object. Parameters: In: inBaseRef - The reference of the list. Out: outCount - Number of elements in this list. Returns: Returns EDS_ERR_OK ...
numpy-1.26.3-cp312-cp312-win_amd64.whl
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
tornado-6.0.3-cp38-cp38-win_amd64.whl官网下载很慢,把自己下载的一些上传给大家下载,速度更快。
资源分类:Python库 所属语言:Python 使用前提:需要解压 资源全名:pytorch_accelerated-0.1.6-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
protobuf-3.17.3-cp310-cp310-win_amd64
caffe mnist 训练所需图片数据
ActiveState对Free账户下载ActivePerl已不再提供离线安装包,需要在线安装,在线安装会碰到不同的问题,其中存在StateTool安装不上的情况,命令行提示“ Preparing Installer for State Tool Package Manager...”后...
Pillow-8.3.1-cp39-cp39-win_amd64
需要配和指定版本torch-2.1.0+cpu使用,请在安装该模块前提前安装官方命令安装torch-2.1.0+cpu
该资源为matplotlib-3.8.2-cp310-cp310-win_amd64.whl,欢迎下载使用哦!
离线安装包,测试可用。使用 pip install [完整包名] 进行安装
Ubuntu18.04+OpenCV3.2.0源码安装时,出现错误:Failed to download v3.1.0/protobuf-cpp-3.1.0.tar.gz ,解决方法:下载附件zip文件,解压后得到protobuf-cpp-3.1.0.tar.gz文件,替换/opencv_contrib-3.2.0/modules...
资源分类:Python库 所属语言:Python 资源全名:aiohttp-3.6.0a3-cp35-cp35m-win_amd64.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源来自pypi官网。 资源全名:scipy-1.5.0rc2-cp38-cp38-manylinux1_x86_64.whl