|
20 | 20 | import yt_dlp
|
21 | 21 | import yt_dlp.patch.check_thumbnails
|
22 | 22 | import yt_dlp.patch.fatal_http_errors
|
23 |
| -from yt_dlp.utils import remove_end, OUTTMPL_TYPES |
| 23 | +from yt_dlp.utils import remove_end, shell_quote, OUTTMPL_TYPES |
24 | 24 |
|
25 | 25 |
|
26 | 26 | _defaults = getattr(settings, 'YOUTUBE_DEFAULTS', {})
|
@@ -314,9 +314,31 @@ def download_media(
|
314 | 314 | if extension in audio_exts:
|
315 | 315 | pp_opts.extractaudio = True
|
316 | 316 | pp_opts.nopostoverwrites = False
|
| 317 | + # The ExtractAudio post processor can change the extension. |
| 318 | + # This post processor is to change the final filename back |
| 319 | + # to what we are expecting it to be. |
| 320 | + final_path = Path(output_file) |
| 321 | + try: |
| 322 | + final_path = final_path.resolve(strict=True) |
| 323 | + except FileNotFoundError: |
| 324 | + # This is very likely the common case |
| 325 | + final_path = Path(output_file).resolve(strict=False) |
| 326 | + expected_file = shell_quote(str(final_path)) |
| 327 | + cmds = pp_opts.exec_cmd.get('after_move', list()) |
| 328 | + # It is important that we use a tuple for strings. |
| 329 | + # Otherwise, list adds each character instead. |
| 330 | + # That last comma is really necessary! |
| 331 | + cmds += ( |
| 332 | + f'test -f {expected_file} || ' |
| 333 | + 'mv -T -u -- %(filepath,_filename|)q ' |
| 334 | + f'{expected_file}', |
| 335 | + ) |
| 336 | + # assignment is the quickest way to cover both 'get' cases |
| 337 | + pp_opts.exec_cmd['after_move'] = cmds |
317 | 338 |
|
318 | 339 | ytopts = {
|
319 | 340 | 'format': media_format,
|
| 341 | + 'final_ext': extension, |
320 | 342 | 'merge_output_format': extension,
|
321 | 343 | 'outtmpl': os.path.basename(output_file),
|
322 | 344 | 'quiet': False if settings.DEBUG else True,
|
@@ -381,6 +403,15 @@ def download_media(
|
381 | 403 | 'modifychapters+ffmpeg': codec_options,
|
382 | 404 | })
|
383 | 405 |
|
| 406 | + # Provide the user control of 'overwrites' in the post processors. |
| 407 | + pp_opts.overwrites = opts.get( |
| 408 | + 'overwrites', |
| 409 | + ytopts.get( |
| 410 | + 'overwrites', |
| 411 | + default_opts.overwrites, |
| 412 | + ), |
| 413 | + ) |
| 414 | + |
384 | 415 | # Create the post processors list.
|
385 | 416 | # It already included user configured post processors as well.
|
386 | 417 | ytopts['postprocessors'] = list(yt_dlp.get_postprocessors(pp_opts))
|
|
0 commit comments