From b043b79f9de59eae12ac0733cced5e41f5eb0fc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C4=8Cejka?= Date: Fri, 13 Oct 2023 15:36:38 +0200 Subject: [PATCH] Add switch for -n/--dry-run and -h/--help --- recode_video.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/recode_video.py b/recode_video.py index 98ee69f..3d93e4d 100755 --- a/recode_video.py +++ b/recode_video.py @@ -16,9 +16,9 @@ import pipes import platform import subprocess -import sys import config import iso639 +import argparse def get_ffmpeg_parameters(file_path, params): @@ -210,21 +210,29 @@ def get_command(command, file_path): if __name__ == "__main__": parameters = config.get_values() - if len(sys.argv) != 2: - print("Usage: {} ".format(os.path.basename(__file__))) - sys.exit(1) + parser = argparse.ArgumentParser(description='Recode video') - input_file = sys.argv[1] + parser.add_argument('-n', '--dry-run', action='store_true', help='Only display the command, don\'t recode') + parser.add_argument('filename', type=str, help='Media file to recode') + + args = parser.parse_args() + + input_file = args.filename output_file = "{}-{}.{}".format( os.path.splitext(input_file)[0], parameters['files']['output']['suffix'], parameters['files']['output']['extension'] ) + if not os.path.exists(input_file): + print("File '{}' does not exist".format(input_file)) + exit(1) + ffmpeg_parameters = get_ffmpeg_parameters(input_file, parameters['recoding']) ffmpeg_command = ('ffmpeg -i "{}" {} {} "{}"' .format(input_file, ffmpeg_parameters, parameters['recoding']['extra'], output_file)) print("\nCall this command:\n{}\n".format(ffmpeg_command)) - os.system(ffmpeg_command) + if not args.dry_run: + os.system(ffmpeg_command)