Skip to content

Commit

Permalink
Logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuchulain committed Dec 28, 2023
1 parent 06df65d commit 652f4e3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
11 changes: 9 additions & 2 deletions config.py
Expand Up @@ -2,7 +2,7 @@

import toml

CONFIG_FILE_NAME = 'video-transcoder.toml'
CONFIG_FILE_NAME = '.video-transcoder.toml'

DEFAULT_VALUES = {
'recoding': {
Expand Down Expand Up @@ -42,13 +42,20 @@
'suffix': 'recoded4tv',
'extension': 'mkv',
}
},
'logging': {
'to_console': True,
'to_file': False,
'file_path': 'recode.log',
'format': '%(asctime)s [%(levelname)s] %(message)s',
'level': 'ERROR',
}
}


def get_values():
home_directory = os.path.expanduser('~')
full_path = os.path.join(home_directory, ".{}".format(CONFIG_FILE_NAME))
full_path = os.path.join(home_directory, CONFIG_FILE_NAME)

if not os.path.exists(full_path):
toml.dump(DEFAULT_VALUES, open(full_path, 'w'))
Expand Down
46 changes: 39 additions & 7 deletions recode_video.py
Expand Up @@ -20,6 +20,7 @@
import config
import iso639
import argparse
import logging


def get_ffmpeg_parameters(file_path, params):
Expand Down Expand Up @@ -213,20 +214,50 @@ def get_command(command, file_path):


if __name__ == "__main__":
parameters = config.get_values()

parser = argparse.ArgumentParser(description='Recode video')

parser.add_argument('-n', '--dry-run', action='store_true', help='Only display the command, don\'t recode')
parser.add_argument('-f', '--force', action='store_true', help='Rewrite output file if it already exists')
parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output')
parser.add_argument('-v', '--verbose', action='count', default=0, help='Verbose output')
parser.add_argument('filename', type=str, help='Media file to recode')
parser.add_argument('outfilename', type=str, help='Recoded media file', nargs='?')

args = parser.parse_args()

logLevels = {
0: logging.ERROR,
1: logging.WARNING,
2: logging.INFO,
3: logging.DEBUG
}

parameters = config.get_values()

if args.verbose == 0:
logLevel = parameters['logging']['level']
else:
logLevel = logLevels[min(args.verbose, 3)]

logHandlers = []
if parameters['logging']['to_file']:
logHandlers.append(logging.FileHandler(parameters['logging']['file_path']))
if parameters['logging']['to_console']:
logHandlers.append(logging.StreamHandler())

logging.basicConfig(
encoding='utf-8',
format=parameters['logging']['format'],
level=logLevel,
handlers=logHandlers
)

logging.debug(f"arguments: {args}")
logging.debug(f"parameters: {parameters}")

input_file = args.filename

logging.debug(f"input_file: {input_file}")

if args.outfilename:
output_file = args.outfilename
else:
Expand All @@ -236,12 +267,14 @@ def get_command(command, file_path):
parameters['files']['output']['extension']
)

logging.debug(f"output_file: {output_file}")

if not os.path.exists(input_file):
print("File '{}' does not exist".format(input_file), file=sys.stderr)
logging.error("File '{}' does not exist".format(input_file))
exit(1)

if os.path.exists(output_file) and not args.force:
print("File '{}' already exists".format(output_file), file=sys.stderr)
logging.error("File '{}' already exists".format(output_file))
exit(1)

ffmpeg_parameters = get_ffmpeg_parameters(input_file, parameters['recoding'])
Expand All @@ -251,8 +284,7 @@ def get_command(command, file_path):
if args.force:
ffmpeg_command = ffmpeg_command + ' -y'

if args.verbose:
print("\nCall this command:\n{}\n".format(ffmpeg_command))
logging.info(f"command: {ffmpeg_command}")

if not args.dry_run:
result = subprocess.run(ffmpeg_command, shell=True, capture_output=True, text=True)
Expand Down

0 comments on commit 652f4e3

Please sign in to comment.