Skip to content

Commit 7987882

Browse files
authored
Merge pull request #984 from tcely/patch-2
Add a post processor for after move
2 parents c37d0ef + 112b477 commit 7987882

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

tubesync/sync/youtube.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import yt_dlp
2121
import yt_dlp.patch.check_thumbnails
2222
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
2424

2525

2626
_defaults = getattr(settings, 'YOUTUBE_DEFAULTS', {})
@@ -314,9 +314,31 @@ def download_media(
314314
if extension in audio_exts:
315315
pp_opts.extractaudio = True
316316
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
317338

318339
ytopts = {
319340
'format': media_format,
341+
'final_ext': extension,
320342
'merge_output_format': extension,
321343
'outtmpl': os.path.basename(output_file),
322344
'quiet': False if settings.DEBUG else True,
@@ -381,6 +403,15 @@ def download_media(
381403
'modifychapters+ffmpeg': codec_options,
382404
})
383405

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+
384415
# Create the post processors list.
385416
# It already included user configured post processors as well.
386417
ytopts['postprocessors'] = list(yt_dlp.get_postprocessors(pp_opts))

0 commit comments

Comments
 (0)