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”
分享到:
相关推荐
groovy-all-2.4.15.jar文件,MAC使用时需存放在/Users/用户名/.gradle/caches/jars-3/某一缓存目录下,找不到就都看一下,我遇到的问题是缓存目录中下载的是2.4.17版本,应该跟gradle版本升级有关
meta-llama-3-8b-instruct 的 model-00001-of-00004.safetensors 的1/3
此压缩包文件"iDRAC-with-Lifecycle-Controller_Firmware_40T1C_WN32_2.63.60.61_A00.zip"是针对iDRAC及其Lifecycle Controller的固件更新程序。版本号2.63.60.61表明这是该组件的一个特定版本,可能包含了性能改进...
Python常用库,官方原版whl文件,文件下载到本地后, 直接终端 pip install xxx.whl 安装
《PyTorch中的torch_scatter模块详解》 在深度学习领域,PyTorch是一个非常流行的框架,它提供了丰富的工具和库来支持模型的构建和训练。`torch_scatter`是其中一个重要的扩展库,专为处理张量的分散操作而设计。...
根据提供的文件信息,我们可以深入探讨与“Downloading 4_darknet19_448-main.zip”相关的知识点。由于标题与描述相同,我们可以推断这是一个关于下载Darknet19模型主文件压缩包的内容。Darknet19是深度学习领域中一...
scipy-1.14.1
onnxruntime-1.9.1-cp310-cp310-linux_armv7l.whl
Django whl安装包,可以直接pip install安装即可,不再下载网络超时
该资源为matplotlib-3.8.2-cp311-cp311-win_amd64.whl,欢迎下载使用哦!
该资源为numpy-2.1.1-cp312-cp312-win_amd64.whl,欢迎下载使用哦!
它是基于 libxml2 和 libxslt 这两个强大的 C 语言库构建的,因此相比纯 Python 实现的解析器(如 xml.etree.ElementTree),lxml 在速度和功能上都更为强大。 主要特性 ... XPath 和 CSS 选择器:支持 XPath 和 CSS ...
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 ...
Typically, this involves downloading the Groovy distribution package, extracting it to a desired location, and adding the `bin` directory to the system's PATH environment variable. The official ...
标题 "tensorflow-2.11.0-cp37-cp37m-manylinux-2-17-x86-64.zip" 提供的是一个TensorFlow库的特定版本,适用于Python 3.7(由`cp37`标识)且为64位架构(由`x86_64`表明)。`manylinux-2-17`是针对多平台兼容性的...
安装前的准备 1、安装Python:确保你的计算机上已经安装了Python。你可以在命令行中输入python --version或python3 --version来检查是否已安装以及安装的版本。 个人建议:在anaconda中自建不同python版本的环境,...
onnxruntime-1.20.0-cp311-cp311-win_amd64.whl
pandas-2.2.3-cp313-cp313-win_amd64.whl pandas whl
arm64平台python 离线安装包,whl安装包
压缩包解压后为.whl文件,直接终端输入pip install xxx.whl安装即可,避免pip install安装失败。 基本介绍 名称与起源:Pillow,原名为PIL(Python Imaging Library),但PIL只支持Python 2版本。...