diff --git a/src/tscut/coarse.py b/src/tscut/coarse.py index c10a235..b94b50f 100644 --- a/src/tscut/coarse.py +++ b/src/tscut/coarse.py @@ -5,9 +5,8 @@ import logging from typing import IO -from tscut.tools.mkvtoolnix import ( - extract_mkv_part, -) +from tscut.tools.mkvtoolnix import extract_mkv_part +from tscut.exceptions import TemporaryFileError logger = logging.getLogger(__name__) @@ -22,8 +21,7 @@ def do_coarse_processing(ffmpeg_path:str, ffprobe_path:str, mkvmerge_path:str, try: internal_mkv = open(internal_mkv_name, 'wb+') except OSError: - logger.error('Impossible to create file: %s', internal_mkv_name) - exit(-1) + raise TemporaryFileError(f"Impossible to create {internal_mkv_name}") # Extract internal part of MKV extract_mkv_part(mkvmerge_path=mkvmerge_path, input_file=input_file, output_file=internal_mkv, diff --git a/src/tscut/models.py b/src/tscut/models.py index e254180..5fd3f7b 100644 --- a/src/tscut/models.py +++ b/src/tscut/models.py @@ -57,5 +57,5 @@ class PreparedMedia: @dataclass class CutResult: filename: str - movie: IO[bytes] + movie: BinaryIO check_positions: list[timedelta] diff --git a/src/tscut/pipeline.py b/src/tscut/pipeline.py index afbd9ad..228d5b2 100644 --- a/src/tscut/pipeline.py +++ b/src/tscut/pipeline.py @@ -7,13 +7,12 @@ import os from datetime import datetime, timedelta from os import unlink from shutil import copyfile, move -from sys import exit from typing import IO, Any, BinaryIO import hexdump from tscut.cutting import cut_recording -from tscut.exceptions import InvalidMediaError +from tscut.exceptions import InvalidMediaError, TemporaryFileError from tscut.h264.avc import parse_codec_private from tscut.matroska.codec import dump_codec_private_data from tscut.models import PreparedMedia, ProcessingOptions, SupportedFormat @@ -55,8 +54,7 @@ def process_recording(options: ProcessingOptions) -> None: input_file = open(options.input_file, mode='rb') logger.debug("Type of input file: %s", type(input_file)) except OSError: - logger.error("Impossible to open %s", options.input_file) - exit(-1) + raise InvalidMediaError(f"Impossible to open {options.input_file}") format_of_file = get_format(options.tools_paths['ffprobe'], input_file) @@ -95,21 +93,22 @@ def process_recording(options: ProcessingOptions) -> None: try: mkv = open(mkvfilename, 'wb+') except OSError: - logger.error('') + raise TemporaryFileError(f"Impossible to create: {mkvfilename}") ffmpeg_convert(options.tools_paths['ffmpeg'], options.tools_paths['ffprobe'], mp4, 'mp4', mkv, 'matroska', duration) if nb_parts > 0: temporaries.append(mkv) except OSError: - logger.error('') + raise TemporaryFileError(f"Impossible to create: {mp4filename}") + elif final_format_of_file == SupportedFormat.MP4: logger.info("Converting MP4 to MKV") try: mkv = open(mkvfilename, 'wb+') except OSError: - logger.error('') + raise TemporaryFileError(f"Impossible to create: {mkvfilename}") ffmpeg_convert(options.tools_paths['ffmpeg'], options.tools_paths['ffprobe'], input_file, 'mp4', mkv, 'matroska', duration) if nb_parts > 0: @@ -120,8 +119,7 @@ def process_recording(options: ProcessingOptions) -> None: streams = get_streams(options.tools_paths['ffprobe'], mkv) if streams is None: - logger.error("No streams found in file: %s", mkv) - exit(-1) + raise InvalidMediaError(f"No streams found in file: {mkv}") logger.debug('Streams: %s', streams) main_video = None @@ -142,8 +140,7 @@ def process_recording(options: ProcessingOptions) -> None: main_video = None if main_video is None: - logger.error('Impossible to find main video stream.') - exit(-1) + raise InvalidMediaError(f"Impossible to find main video stream.") # We retrieve the main private codec data _, main_codec_private_data = get_codec_private_data_from_mkv( @@ -154,8 +151,7 @@ def process_recording(options: ProcessingOptions) -> None: hexdump.dump(main_codec_private_data, sep=':')) if main_codec_private_data is None: - logger.error("Impossible to retrieve private data from MKV file %s", mkv) - exit(-1) + raise InvalidMediaError(f"Impossible to retrieve private data from MKV file {mkv}") # We parse them main_avc_config = parse_codec_private(main_codec_private_data) @@ -176,9 +172,8 @@ def process_recording(options: ProcessingOptions) -> None: # If there exists a difference between our own reconstructed AVC configuration and the # original one, we abandon if iso_avc_config != main_avc_config: - logger.error('AVC configurations are different: %s\n%s\n', main_avc_config, - iso_avc_config) - exit(-1) + raise InvalidMediaError(f"AVC configurations are different:\ + {main_avc_config}\n{iso_avc_config}\n") prepared_movie = PreparedMedia( basename = basename, @@ -237,13 +232,11 @@ def process_recording(options: ProcessingOptions) -> None: try: idx = open(idx_name,'rb') except OSError: - logger.error("Impossible to open %s.", idx_name) - exit(-1) + raise TemporaryFileError(f"Impossible to open {idx_name}") try: sub = open(sub_name,'rb') except OSError: - logger.error("Impossible to open %s.", sub_name) - exit(-1) + raise TemporaryFileError(f"Impossible to open {sub_name}") temporaries.append(idx) temporaries.append(sub) diff --git a/src/tscut/tools/mkvtoolnix.py b/src/tscut/tools/mkvtoolnix.py index 053122f..a8bfe63 100644 --- a/src/tscut/tools/mkvtoolnix.py +++ b/src/tscut/tools/mkvtoolnix.py @@ -129,7 +129,7 @@ def change_codec_private_data(mkvinfo_path:str, input_file: IO[bytes], logger.info('Current size of file: %d', current_length) position, current_data = get_codec_private_data_from_mkv(mkvinfo_path, input_file) if position is None or current_data is None: - return None + raise InvalidMediaError(f"Impossible to retrieve private data from file") current_data_length = len(current_data) future_length = current_length - current_data_length + len(codec_data) logger.info('Expected size of file: %d', future_length) @@ -147,8 +147,7 @@ def change_codec_private_data(mkvinfo_path:str, input_file: IO[bytes], break if not found: - logger.error('Impossible to retrieve the key of codec private data') - raise InvalidMediaError("") + raise InvalidMediaError(f"Impossible to retrieve the key of codec private data") if current_length < future_length: lseek(infd, position+current_data_length, SEEK_SET)