Last active
May 7, 2020 12:40
-
-
Save tookdes/75f1b302333175f66fa0a23c7e54ae4a to your computer and use it in GitHub Desktop.
下载 Youtube 视频和字幕的脚本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 需要下载 | |
# https://github.com/iawia002/annie | |
# 将 annie 可执行文件放在本脚本同目录或者加入环境变量 | |
# 或者自行修改相对路径 | |
# 需要自行使用 IFTTT 或者其他手段创建 Youtube 视频地址列表 | |
# 详情请参阅 | |
# https://blog.tookdes.org/archives/download-youtube-subscribe-video-with-srt.html | |
# -*- coding: utf-8 -*- | |
import subprocess | |
import requests | |
import json | |
import time | |
import datetime | |
srt_api_key = u"a2d09c7d76fced01f8be4b1f4cce8bec" #测试用 API,也可以自行更改 | |
url_file = "C:\YourPath\Subscribe.txt"#定义好自己文件路径 | |
url_list = [] | |
video_id_list = [] | |
with open(url_file) as f: | |
url = f.read().splitlines() | |
url_list.extend(url) | |
#print(url_list) | |
for i in range(0,len(url_list)): | |
video_id_list.append(url_list[i].replace('https://youtu.be/', '')) | |
#print(video_id_list) | |
def get_vid(url): | |
exe = "annie.exe -s 127.0.0.1:1080 " + url #自行更改 socks5 代理地址[doge],详见 annie 说明 | |
subprocess.call(exe) | |
def get_srt(video_id, url): | |
params_dict = {"api-key":srt_api_key} | |
exe = "annie.exe -s 127.0.0.1:1080 -j " + url #自行更改代理地址 | |
video_title = json.loads(subprocess.Popen(exe, shell=True,\ | |
stdout=subprocess.PIPE,\ | |
stderr=subprocess.STDOUT).stdout.read())\ | |
["Title"] | |
# API 获得的 title 并不一样,不推荐使用 | |
# video_title = json.loads(requests.get("https://api.zhuwei.me/v1/captions/"+\ | |
# video_id,\ | |
# params = params_dict).text)\ | |
# ["response"]["captions"]["title"] | |
srt_get = json.loads(requests.get("https://api.zhuwei.me/v1/captions/"+\ | |
video_id,\ | |
params = params_dict).text) | |
# print(srt_get) | |
if srt_get["response"]["captions"]["available_captions_count"] == 0: | |
pass | |
else: | |
for i in range(0,srt_get["response"]["captions"]["available_captions_count"]): | |
srt_lang = srt_get["response"]["captions"]["available_captions"][i]["language"] | |
srt_url = srt_get["response"]["captions"]["available_captions"][i]["caption_content_url"] | |
srt_data = json.loads(requests.get(srt_url, params = params_dict).text)["contents"]["content"] | |
print(video_id + " " + srt_lang + " srt done.") | |
srt_file = open(video_title + "." + srt_lang +".srt","w",encoding='utf-8')#如果不转编码可能会导致中文字幕写入失败 | |
srt_file.write(srt_data) | |
srt_file.close | |
def main(): | |
for i in range(0,len(url_list)): | |
get_vid(url_list[i]) | |
get_srt(video_id_list[i], url_list[i]) | |
with open(url_file,"w+") as f: | |
f.truncate() | |
f.close | |
main() #如果采用外部定时运行 | |
exit() | |
##如果后台连续运行 | |
##while True: | |
## now = datetime.datetime.now() | |
## if now.hour==14: #and now.minute==10: #自己定义运行脚本的时间 | |
## main() | |
## time.sleep(3600) #自己定制 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment