Skip to content

Commit b8ef755

Browse files
authored
Merge pull request #2 from kaifcodec/feat/subtitles
feat: add subtitles options
2 parents 9e018aa + e81180d commit b8ef755

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

ytconverter/downloaders/multi_mp4.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,30 @@ def run():
7171
)
7272
)
7373

74+
ask_subs = input(
75+
apply_style("\nDownload subtitles for all videos? (y/n): ", "/cyan/bold")
76+
).strip().lower()
77+
sub_options = {}
78+
cookie_path = ""
79+
if ask_subs == "y":
80+
print(apply_style("Note: YouTube may block subtitle extraction with HTTP 429. If that happens provide a cookies file.", "/yellow"))
81+
cookie_path = input(apply_style("Enter path to cookies file for yt-dlp (optional, press Enter to skip): ", "/green")).strip()
82+
lang = input(
83+
apply_style("Enter subtitle language code (e.g. en) or leave blank for all: ", "/green")
84+
).strip()
85+
pref_auto = input(
86+
apply_style("Also download automatic subtitles if manual not available? (y/n) [y]: ", "/green")
87+
).strip().lower()
88+
if pref_auto == "":
89+
pref_auto = "y"
90+
sub_options["writesubtitles"] = True
91+
sub_options["writeautomaticsub"] = pref_auto == "y"
92+
if lang:
93+
sub_options["subtitleslangs"] = lang
94+
sub_options["convertsubtitles"] = "srt"
95+
if cookie_path:
96+
sub_options["cookiefile"] = cookie_path
97+
7498
destination = Path(get_download_path("mp4"))
7599
k = 1
76100
for url in down_list:
@@ -85,10 +109,18 @@ def run():
85109
vid_title = sanitize(info["title"])[:60]
86110
print(apply_style(f"\nStarting Video {k} Download...\n", "/cyan/bold"))
87111
time1 = int(time.time())
112+
88113
ydl_opts = {
89114
"format": format_map[choice],
90115
"outtmpl": str(destination / f"{vid_title}.%(ext)s"),
116+
"quiet": True,
117+
"no_warnings": True,
91118
}
119+
if sub_options:
120+
for k_opt, v_opt in sub_options.items():
121+
if v_opt is not None:
122+
ydl_opts[k_opt] = v_opt
123+
92124
try:
93125
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
94126
ydl.download([url])

ytconverter/downloaders/single_mp4.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ def run():
5555

5656
title = sanitize(info.get("title", "Unknown title"))
5757

58-
# Display formats with size
5958
for idx, fmt in enumerate(formats, 1):
6059
res = (
6160
fmt.get("resolution", "Audio Only")
@@ -142,9 +141,6 @@ def run():
142141
"outtmpl": str(video_out)
143142
}
144143

145-
146-
147-
148144
print("\033[36;1mStarting Video Download...\033[0m\n")
149145
t0 = int(time.time())
150146
try:
@@ -186,5 +182,33 @@ def run():
186182
if Path("audio_temp").exists():
187183
shutil.rmtree("audio_temp", ignore_errors=True)
188184

185+
try:
186+
ask_subs = input("\n\033[36;1mDownload subtitles for this video? (y/n): \033[0m").strip().lower()
187+
if ask_subs == "y":
188+
print("\033[33;1mNote: YouTube may block subtitle extraction with HTTP 429. If that happens provide a cookies file.\033[0m")
189+
cookie_path = input("\033[36;1mEnter path to cookies file (optional, press Enter to skip): \033[0m").strip()
190+
lang = input("\033[36;1mEnter subtitle language code (e.g. en) or leave blank for all: \033[0m").strip()
191+
pref_auto = input("\033[36;1mAlso download automatic subtitles if manual not available? (y/n) [y]: \033[0m").strip().lower()
192+
if pref_auto == "":
193+
pref_auto = "y"
194+
cmd = ["yt-dlp", "--skip-download", "--write-sub"]
195+
if pref_auto == "y":
196+
cmd += ["--write-auto-sub"]
197+
if lang:
198+
cmd += ["--sub-lang", lang]
199+
cmd += ["--convert-subs", "srt"]
200+
if cookie_path:
201+
cmd += ["--cookies", cookie_path]
202+
cmd += ["-o", str(dest / f"{safe_title}.%(ext)s")]
203+
cmd += [url]
204+
print("\033[36;1mDownloading subtitles...\033[0m")
205+
try:
206+
sp.run(cmd, check=True)
207+
print("\033[32;1mSubtitles downloaded (converted to .srt where possible).\033[0m")
208+
except Exception as e:
209+
print(f"\033[31;1mSubtitle download error: {e}\033[0m")
210+
except Exception:
211+
pass
212+
189213
if __name__=="__main__":
190214
run()

0 commit comments

Comments
 (0)