Fix yt-dlp call

This commit is contained in:
Grizouille
2025-11-20 16:20:57 +01:00
parent 1577eced3f
commit fb866f32be

View File

@@ -13,20 +13,22 @@ class DownloadedFileNotFoundException(Exception):
pass pass
def execute(cmd): def execute(cmd, isVideo):
print('Executing "{}"'.format(cmd)) print('Executing "{}"'.format(cmd))
p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
p.wait()
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
print(stdout.decode()) print(stdout.decode())
if p.returncode != 0: if p.returncode != 0:
print(stderr.decode()) print(stderr.decode())
raise YoutubeDLFailedException("ERROR: youtube-dl exited with non-zero: \n{}\nYou may have to update it!".format(stderr.decode())) raise YoutubeDLFailedException("ERROR: youtube-dl exited with non-zero: \n{}\nYou may have to update it!".format(stderr.decode()))
return get_absolute_filename(stdout.decode(), stderr.decode()) return get_absolute_filename(stdout.decode(), stderr.decode(), isVideo)
def get_absolute_filename(stdout, stderr): def get_absolute_filename(stdout, stderr, isVideo):
regex_foo = re.search(r'Destination:\s(.*mp3)', stdout) if isVideo:
regex_foo = re.search(r'Destination:\s(.*mp4)', stdout)
else:
regex_foo = re.search(r'Destination:\s(.*mp3)', stdout)
if not regex_foo: if not regex_foo:
raise DownloadedFileNotFoundException("ERROR: Can not extract output file via regex. \nstderr: {}\nstdout: {}".format(stderr, stdout)) raise DownloadedFileNotFoundException("ERROR: Can not extract output file via regex. \nstderr: {}\nstdout: {}".format(stderr, stdout))
return regex_foo.group(1) return regex_foo.group(1)
@@ -45,21 +47,23 @@ def youtubedl_download(url, destination_dir, proxy=None, video=False):
if video: if video:
youtube_dl_cmd = config["youtubedl"]["command"] + \ youtube_dl_cmd = config["youtubedl"]["command"] + \
proxy_command + \ proxy_command + \
" -f \"best\" " + \ " -f b " + \
"--no-cache-dir " + \
"--merge-output-format mp4 " + \ "--merge-output-format mp4 " + \
f"-o \"{destination_dir}/%(title)s.%(ext)s\" " + \ f"-o \"{destination_dir}/%(title)s [video].%(ext)s\" " + \
quote(url) quote(url)
else: else:
youtube_dl_cmd = config["youtubedl"]["command"] + \ youtube_dl_cmd = config["youtubedl"]["command"] + \
proxy_command + \ proxy_command + \
" -x --audio-format mp3 " + \ " -x --audio-format mp3 " + \
"--audio-quality 0 " + \ "--audio-quality 0 " + \
f"-o \"{destination_dir}/%(title)s.%(ext)s\" " + \ f"-o \"{destination_dir}/%(title)s [audio].%(ext)s\" " + \
"--embed-metadata " + \ "--embed-metadata " + \
"--no-embed-chapters " + \ "--no-embed-chapters " + \
"--no-cache-dir " + \
quote(url) quote(url)
filename_absolute = execute(youtube_dl_cmd) filename_absolute = execute(youtube_dl_cmd, video)
return filename_absolute return filename_absolute