From 9a3d04af7fe7d0da7647b2a06083fd5118f4656e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Tronel?= Date: Sun, 23 Nov 2025 15:56:09 +0100 Subject: [PATCH] Better typing. --- removeads.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/removeads.py b/removeads.py index e0e0ea6..e6e650c 100755 --- a/removeads.py +++ b/removeads.py @@ -220,7 +220,7 @@ def get_subtitles_tracks(ffprobe_path:str, mkv_path: str) -> dict[str,str]|None: @typechecked def extract_srt(mkvextract:str, filename:str, subtitles:dict[str, list[int]], - langs:dict[Lang,str]) -> list|None: + langs:dict[Lang,str]) -> list[tuple[str,str,str,str]]|None: logger = logging.getLogger(__name__) params = [mkvextract, filename, 'tracks'] @@ -277,7 +277,8 @@ def extract_srt(mkvextract:str, filename:str, subtitles:dict[str, list[int]], return res -def do_ocr(vobsubocr, idxs, duration, temporaries, dump_mem_fd=False): +@typechecked +def do_ocr(vobsubocr:str, idxs: list[tuple[str,str,str,str]], duration:timedelta, temporaries:list[IO[bytes]], dump_mem_fd:bool=False): logger = logging.getLogger(__name__) res = [] @@ -362,7 +363,8 @@ class SupportedFormat(IntEnum): # Found codec private data using mkvinfo @typechecked -def get_codec_private_data_from_mkv(mkvinfo_path:str, input_file: IO[bytes]) -> tuple[int, bytes]: +def get_codec_private_data_from_mkv(mkvinfo_path:str, + input_file: IO[bytes]) -> tuple[int, bytes]|tuple[None,None]: logger = logging.getLogger(__name__) infd = input_file.fileno() @@ -625,7 +627,7 @@ def parse_scaling_list(buf:bytes, bit_position: int, size) -> tuple[int,list[int # The ISO/IEC H.264-201602 seems to take into account the case where the end of the deltas list # is full of zeroes. @typechecked -def write_scaling_list(buf:bytes, bit_position: int, size, matrix, +def write_scaling_list(buf:bytes, bit_position: int, size, matrix:list[int], optimized: bool = False) -> int: logger = logging.getLogger(__name__) logger.debug('Dumping matrix: %s of size: %d, size parameter: %d.', matrix, len(matrix), size) @@ -1713,7 +1715,7 @@ def parse_mkv_tree(mkvinfo_path:str, input_file: IO[bytes]) -> dict[str,tuple[in # value 0 to 2^56-2 @typechecked -def get_ebml_length(length) -> bytes|None: +def get_ebml_length(length:int) -> bytes|None: logger = logging.getLogger(__name__) if 0 <= length <= 2**7-2: @@ -1762,7 +1764,7 @@ def dump_codec_private_data(avc_decoder_configuration: AVCDecoderConfiguration) return res @typechecked -def change_ebml_element_size(input_file: IO[bytes], position:int, addendum) -> int: +def change_ebml_element_size(input_file: IO[bytes], position:int, addendum:int) -> int: logger = logging.getLogger(__name__) initial_position = position @@ -1855,7 +1857,7 @@ def change_ebml_element_size(input_file: IO[bytes], position:int, addendum) -> i return delta @typechecked -def change_codec_private_data(mkvinfo_path:str, input_file: IO[bytes], codec_data) -> None: +def change_codec_private_data(mkvinfo_path:str, input_file: IO[bytes], codec_data:bytes) -> None: logger = logging.getLogger(__name__) infd = input_file.fileno() @@ -2268,8 +2270,8 @@ def get_frames_in_stream(ffprobe_path: str, input_file: IO[bytes], begin:timedel # TODO: Finish implementation of this function and use it. @typechecked -def get_nearest_idr_frame(ffprobe_path: str, input_file: IO[bytes], timestamp, before: bool=True, - delta: timedelta=timedelta(seconds=2)): +def get_nearest_idr_frame(ffprobe_path: str, input_file: IO[bytes], timestamp:timedelta, + before: bool=True, delta: timedelta=timedelta(seconds=2)): # pylint: disable=W0613 logger = logging.getLogger(__name__) @@ -2438,7 +2440,7 @@ def extract_mkv_part(mkvmerge_path:str, input_file:IO[bytes], output_file:IO[byt @typechecked def extract_pictures(ffmpeg_path:str, input_file:IO[bytes], begin:timedelta, nb_frames:int, - width:int=640, height:int=480) -> tuple[bytes,int]: + width:int=640, height:int=480) -> tuple[bytes,int]|tuple[None,None]: logger = logging.getLogger(__name__) infd = input_file.fileno() @@ -2477,7 +2479,8 @@ def extract_pictures(ffmpeg_path:str, input_file:IO[bytes], begin:timedelta, nb_ @typechecked def extract_sound(ffmpeg_path:str, input_file: IO[bytes], begin:timedelta, output_filename:str, packet_duration:int, sub_channel:int=0, - nb_packets:int=0, sample_rate:int=48000, nb_channels:int=2) -> tuple[bytes,int]: + nb_packets:int=0, sample_rate:int=48000, + nb_channels:int=2) -> tuple[bytes,int]|tuple[None,None]: logger = logging.getLogger(__name__) outfd = memfd_create(output_filename, flags=0) @@ -2512,7 +2515,7 @@ def extract_sound(ffmpeg_path:str, input_file: IO[bytes], begin:timedelta, outpu return sound, outfd @typechecked -def dump_ppm(pictures: list, prefix: str, temporaries: list[IO[bytes]]) -> None: +def dump_ppm(pictures: bytes, prefix: str, temporaries: list[IO[bytes]]) -> None: logger = logging.getLogger(__name__) # "P6\nWIDTH HEIGHT\n255\n" @@ -2546,7 +2549,7 @@ def dump_ppm(pictures: list, prefix: str, temporaries: list[IO[bytes]]) -> None: header_len=2+1+ceil(log(width, 10))+1+ceil(log(height, 10))+1+3+1 try: - with open(filename, 'wb', encoding='utf8') as out: + with open(filename, 'wb') as out: temporaries.append(out) outfd = out.fileno() length=header_len+3*width*height @@ -2686,7 +2689,7 @@ def extract_all_streams(ffmpeg_path:str, ffprobe_path:str, input_file:IO[bytes], if dump_mem_fd: try: - with open(tmpname,'wb', encoding='utf8') as output: + with open(tmpname,'wb') as output: temporaries.append(output) outfd = output.fileno() pos = 0 @@ -2804,7 +2807,7 @@ def extract_all_streams(ffmpeg_path:str, ffprobe_path:str, input_file:IO[bytes], # Merge a list of mkv files passed as input, and produce a new MKV as output @typechecked def merge_mkvs(mkvmerge_path:str, inputs: list[IO[bytes]], output_name:str, - concatenate: bool=True, timestamps: dict[int, IO[str]]={}) -> IO[bytes]: + concatenate: bool=True, timestamps: dict[int, IO[str]]={}) -> IO[bytes]|None: logger = logging.getLogger(__name__) fds = []