Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to download subtitles #133

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.venv
__pycache__
.vscode
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.PHONY=venv
venv:
test -d .venv || python3 -m venv .venv
. .venv/bin/activate

requirements:
pip install -r requirements.txt

build:
docker build . --tag youtube-dl-server:develop

run:
docker run -v /tmp/youtube-dl:/youtube-dl -p 8080:8080 youtube-dl-server:develop
5 changes: 5 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ <h1 class="display-4">youtube-dl</h1>
<option value="wav">WAV</option>
</optgroup>
</select>
<select class="form-select" name="subtitles">
<option value="no-subtitles">No Subtitles</option>
<option value="all">All Subtitles</option>
<option value="all-srt">All Subtitles - SRT format (not compatible with webm)</option>
</select>
<button class="btn btn-primary" type="submit" id="button-submit">
Submit
</button>
Expand Down
Binary file modified youtube-dl-server.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 37 additions & 2 deletions youtube-dl-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"YDL_OUTPUT_TEMPLATE": config("YDL_OUTPUT_TEMPLATE", cast=str, default="/youtube-dl/%(title).200s [%(id)s].%(ext)s"),
"YDL_ARCHIVE_FILE": config("YDL_ARCHIVE_FILE", default=None),
"YDL_UPDATE_TIME": config("YDL_UPDATE_TIME", cast=bool, default=True),
"YDL_WRITE_SUBTITLES": config("YDL_WRITE_SUBTITLES", cast=bool, default=False),
"YDL_SUBTITLES_SRT": config("YDL_SUBTITLES_SRT", cast=bool, default=False),
}


Expand All @@ -38,7 +40,10 @@ async def q_put(request):
form = await request.form()
url = form.get("url").strip()
ui = form.get("ui")
options = {"format": form.get("format")}
options = {
"format": form.get("format"),
"subtitles": form.get("subtitles")
}

if not url:
return JSONResponse(
Expand Down Expand Up @@ -82,13 +87,19 @@ def get_ydl_options(request_options):
}

requested_format = request_options.get("format", "bestvideo")
requested_subtitles = request_options.get("subtitles", "no-subtitles")

if requested_format in ["aac", "flac", "mp3", "m4a", "opus", "vorbis", "wav"]:
request_vars["YDL_EXTRACT_AUDIO_FORMAT"] = requested_format
elif requested_format == "bestaudio":
request_vars["YDL_EXTRACT_AUDIO_FORMAT"] = "best"
elif requested_format in ["mp4", "flv", "webm", "ogg", "mkv", "avi"]:
request_vars["YDL_RECODE_VIDEO_FORMAT"] = requested_format

if requested_subtitles != "no-subtitles":
request_vars["YDL_WRITE_SUBTITLES"] = True
if requested_subtitles == "all-srt":
request_vars["YDL_SUBTITLES_SRT"] = True

ydl_vars = app_defaults | request_vars

Expand All @@ -110,18 +121,42 @@ def get_ydl_options(request_options):
"preferedformat": ydl_vars["YDL_RECODE_VIDEO_FORMAT"],
}
)

if ydl_vars["YDL_SUBTITLES_SRT"] == True and ydl_vars["YDL_WRITE_SUBTITLES"] == True:
# convert the subtitles to SRT - required if your video player cannot read new formats (e.g. Kodi 18)
postprocessors.append(
{
"key": "FFmpegSubtitlesConvertor",
'format': "srt"
}
)

if ydl_vars["YDL_WRITE_SUBTITLES"] == True:
# merge the subtitles in the output file
postprocessors.append(
{
"key": "FFmpegEmbedSubtitle",
'already_have_subtitle': False
}
)


return {
"format": ydl_vars["YDL_FORMAT"],
"postprocessors": postprocessors,
"outtmpl": ydl_vars["YDL_OUTPUT_TEMPLATE"],
"download_archive": ydl_vars["YDL_ARCHIVE_FILE"],
"updatetime": ydl_vars["YDL_UPDATE_TIME"] == "True",
"writesubtitles": ydl_vars["YDL_WRITE_SUBTITLES"],
"subtitleslangs": ["all"],
"allsubtitles": True
}


def download(url, request_options):
with YoutubeDL(get_ydl_options(request_options)) as ydl:
opts = get_ydl_options(request_options)
print(f"downloading {url} with options {opts}")
with YoutubeDL(opts) as ydl:
ydl.download([url])


Expand Down