From fd99b47b5d9790fcff327872f4fa4f8927b8ee1c Mon Sep 17 00:00:00 2001 From: Arne de Laat Date: Sun, 29 Dec 2024 15:44:26 +0100 Subject: [PATCH] Add CLI for detect audio function Rewrite function to only return each file once, even if there are multiple audio streams. Rename other CLI related functions. Remove name-main part. --- pyproject.toml | 3 ++- time_lapse/cli.py | 26 +++++++++++++++++++++++--- time_lapse/detect_audio.py | 8 +++++--- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 250407a..78988d2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,7 +50,8 @@ Homepage = 'https://arne.delaat.net/timelapse.html' Repository = 'https://github.com/153957/time-lapse' [project.scripts] -timelapse = 'time_lapse.cli:main' +timelapse = 'time_lapse.cli:timelapse' +detect_audio = 'time_lapse.cli:detect_audio' [tool.mypy] ignore_missing_imports = true diff --git a/time_lapse/cli.py b/time_lapse/cli.py index e8ffbf1..9523ee3 100644 --- a/time_lapse/cli.py +++ b/time_lapse/cli.py @@ -1,6 +1,7 @@ import argparse from time_lapse import output, source +from time_lapse.detect_audio import files_with_audio def make_movie( @@ -17,7 +18,7 @@ def make_movie( output.create_outputs(source_input, name, watermark=watermark, verbose=verbose, dryrun=dryrun) -def main() -> None: +def get_parser_timelapse() -> argparse.ArgumentParser: parser = argparse.ArgumentParser(description='Combine frames into a movie.') parser.add_argument( '--name', @@ -60,10 +61,29 @@ def main() -> None: help='Add watermark, provide two value, one main text and the subtext.', nargs=2, ) + return parser + + +def timelapse() -> None: + parser = get_parser_timelapse() kwargs = vars(parser.parse_args()) make_movie(**kwargs) -if __name__ == '__main__': - main() +def get_parser_detect_audio() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser(description='Find movie files with audio streams.') + parser.add_argument( + '--pattern', + help='Glob pattern with which to find the movies to check.', + default='*.mp4', + ) + return parser + + +def detect_audio() -> None: + parser = get_parser_detect_audio() + kwargs = vars(parser.parse_args()) + + for filename in files_with_audio(**kwargs): + print(filename) diff --git a/time_lapse/detect_audio.py b/time_lapse/detect_audio.py index 5c46ed4..91e8354 100644 --- a/time_lapse/detect_audio.py +++ b/time_lapse/detect_audio.py @@ -6,6 +6,8 @@ def files_with_audio(pattern: str = '*.mp4') -> Iterator[str]: for filename in Path().glob(pattern): - for stream in ffmpeg.probe(filename)['streams']: - if stream['codec_type'] == 'audio': - yield str(filename) + if any( + stream['codec_type'] == 'audio' + for stream in ffmpeg.probe(filename)['streams'] + ): + yield str(filename)