Skip to content

Commit 1e8b911

Browse files
committed
🎉 feat(youtube): 添加下载字幕功能并更新参数设置
1 parent 2ce3c59 commit 1e8b911

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

src/ParseHub/parsers/base/yt_dlp_parser.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@
2020
class YtParse(Parse):
2121
"""yt-dlp解析器"""
2222

23+
params = {
24+
"format": "(mp4+bestaudio) / (bestvideo* + bestaudio / best)",
25+
"quiet": True, # 不输出日志
26+
"writethumbnail": True, # 下载缩略图
27+
"writesubtitles": False, # 下载字幕
28+
"writeautomaticsub": True, # 下载自动翻译的字幕
29+
"subtitlesformat": "ttml", # 字幕格式
30+
"subtitleslangs": ["en", "zh-CN"], # 字幕语言
31+
"postprocessors": [
32+
{
33+
"key": "FFmpegVideoConvertor",
34+
"preferedformat": "mp4", # 视频格式
35+
}
36+
],
37+
"playlist_items": "1", # 分p列表默认解析第一个
38+
# "progress_hooks": [self.hook], # 进度回调
39+
}
40+
2341
async def parse(
2442
self, url: str, progress=None, progress_args=()
2543
) -> Union["YtVideoParseResult", "YtImageParseResult"]:
@@ -36,8 +54,8 @@ async def parse(
3654
else:
3755
return YtVideoParseResult(video=video_info.url, **_d)
3856

39-
async def _parse(self, url) -> "YtVideoInfo":
40-
with YoutubeDL(self.build_yto()) as ydl:
57+
async def _parse(self, url, params=None) -> "YtVideoInfo":
58+
with YoutubeDL(params or self.params) as ydl:
4159
dl = ydl.extract_info(url, download=False)
4260
if dl.get("_type"):
4361
dl = dl["entries"][0]
@@ -64,26 +82,6 @@ async def _parse(self, url) -> "YtVideoInfo":
6482
# self.set_status(0, f"下 载 中...|{current * 100 / total:.0f}%")
6583
# )
6684

67-
@staticmethod
68-
def build_yto():
69-
return {
70-
"format": "(mp4+bestaudio) / (bestvideo* + bestaudio / best)",
71-
"quiet": True, # 不输出日志
72-
"writethumbnail": True, # 下载缩略图
73-
"writesubtitles": True, # 下载字幕
74-
"writeautomaticsub": True, # 下载自动翻译的字幕
75-
"subtitlesformat": "ttml", # 字幕格式
76-
"subtitleslangs": ["en", "zh-CN"], # 字幕语言
77-
"postprocessors": [
78-
{
79-
"key": "FFmpegVideoConvertor",
80-
"preferedformat": "mp4", # 视频格式
81-
}
82-
],
83-
"playlist_items": "1", # 分p列表默认解析第一个
84-
# "progress_hooks": [self.hook], # 进度回调
85-
}
86-
8785

8886
class YtVideoParseResult(VideoParseResult):
8987
def __init__(
@@ -113,8 +111,8 @@ async def download(
113111
dir_.mkdir(parents=True, exist_ok=True)
114112

115113
# 输出模板
116-
yto = YtParse.build_yto()
117-
yto["outtmpl"] = f"{dir_.joinpath(f'ytdlp_%(id)s')}.%(ext)s"
114+
yto = YtParse().params
115+
yto["outtmpl"] = f"{dir_.joinpath('ytdlp_%(id)s')}.%(ext)s"
118116

119117
text = "下载合并中...请耐心等待..."
120118
if self.dl.duration > 1800:

src/ParseHub/parsers/parser/youtube.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ async def parse(
1111
self, url: str, progress=None, progress_args=()
1212
) -> Union["YtVideoParseResult", "YtImageParseResult"]:
1313
url = await self.get_raw_url(url)
14-
14+
self.params["writesubtitles"] = True
1515
return await super().parse(url, progress, progress_args)

0 commit comments

Comments
 (0)