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
Downloading 4_darknet19_448-main.zip
此压缩包文件"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`是其中一个重要的扩展库,专为处理张量的分散操作而设计。...
onnxruntime-1.9.1-cp310-cp310-linux_armv7l.whl
该资源为matplotlib-3.8.2-cp311-cp311-win_amd64.whl,欢迎下载使用哦!
该资源为numpy-2.1.1-cp312-cp312-win_amd64.whl,欢迎下载使用哦!
压缩包解压后为.whl文件,直接终端输入pip install xxx.whl安装即可,避免pip install安装失败。 基本介绍 名称与起源:Pillow,原名为PIL(Python Imaging Library),但PIL只支持Python 2版本。...
arm64平台python 离线安装包,whl安装包
scipy-1.14.1
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 ...
标题 "tensorflow-2.11.0-cp37-cp37m-manylinux-2-17-x86-64.zip" 提供的是一个TensorFlow库的特定版本,适用于Python 3.7(由`cp37`标识)且为64位架构(由`x86_64`表明)。`manylinux-2-17`是针对多平台兼容性的...
pandas-2.2.3-cp313-cp313-win_amd64.whl pandas whl
pandas whl安装包,对应各个python版本和系统(具体看资源名字),找准自己对应的下载即可! 下载后解压出来是已.whl为后缀的安装包,进入终端,直接pip install pandas-xxx.whl即可,非常方便。 ...
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 ...
Cython-3.0.0a9-cp310-cp310-win_amd64
matplotlib-3.9.1.post1-cp39-cp39-macosx_10_12_x86_64.whl matplotlib-3.9.1.post1-cp39-cp39-macosx_10_12_x86_64.whl matplotlib-3.9.1.post1-cp39-cp39-macosx_10_12_x86_64.whl matplotlib-3.9.1.post1-cp39-...
scipy-1.14