diff --git a/src/ytdl_sub/utils/ffmpeg.py b/src/ytdl_sub/utils/ffmpeg.py index affed520..f8ccdbfa 100644 --- a/src/ytdl_sub/utils/ffmpeg.py +++ b/src/ytdl_sub/utils/ffmpeg.py @@ -1,4 +1,3 @@ -import shutil import subprocess import tempfile from typing import Dict @@ -7,6 +6,7 @@ from typing import Optional from ytdl_sub.utils.chapters import Chapters from ytdl_sub.utils.exceptions import ValidationException +from ytdl_sub.utils.file_handler import FileHandler from ytdl_sub.utils.logger import Logger logger = Logger.get(name="ffmpeg") @@ -132,8 +132,7 @@ def set_ffmpeg_metadata_chapters( output_file_path, ] ) - - shutil.move(src=output_file_path, dst=file_path) + FileHandler.move(output_file_path, file_path) def add_ffmpeg_metadata_key_values(file_path: str, key_values: Dict[str, str]) -> None: @@ -154,4 +153,4 @@ def add_ffmpeg_metadata_key_values(file_path: str, key_values: Dict[str, str]) - ffmpeg_args.extend(["-codec", "copy", output_file_path]) FFMPEG.run(ffmpeg_args) - shutil.move(src=output_file_path, dst=file_path) + FileHandler.move(output_file_path, file_path) diff --git a/src/ytdl_sub/utils/file_handler.py b/src/ytdl_sub/utils/file_handler.py index ddabadd4..6a3dc6c9 100644 --- a/src/ytdl_sub/utils/file_handler.py +++ b/src/ytdl_sub/utils/file_handler.py @@ -353,8 +353,23 @@ class FileHandler: Source file dst_file_path Destination file + + Raises + ------ + OSError + Cross-device link workaround """ - shutil.move(src=src_file_path, dst=dst_file_path) + try: + shutil.move(src=src_file_path, dst=dst_file_path) + except OSError as os_error_exc: + # Invalid cross-device link + # Can happen from using os.rename under the hood, which requires the two file on the + # same filesystem. Work around it by copying and deleting the file + if os_error_exc.errno == 18: + cls.copy(src_file_path, dst_file_path) + cls.delete(src_file_path) + else: + raise @classmethod def delete(cls, file_path: Union[str, Path]):