From 7d33da707d93c74a678eab2c08a3f0b495fad451 Mon Sep 17 00:00:00 2001 From: Kifir Kifirkin <6573628+kifirkin@users.noreply.github.com> Date: Mon, 25 Oct 2021 13:52:10 +0300 Subject: [PATCH] fix multiple files conversion (#10) When using the `--multiple` option only one file was being converted into several output files. This is the fix for that wrong behaviour. --- convert/commands/audio.py | 7 +-- convert/commands/base.py | 17 +++--- convert/commands/video.py | 7 +-- convert/converter_utils/utils.py | 99 ++++++++++++++++---------------- 4 files changed, 62 insertions(+), 68 deletions(-) diff --git a/convert/commands/audio.py b/convert/commands/audio.py index 32fd4b0..b3b4a17 100755 --- a/convert/commands/audio.py +++ b/convert/commands/audio.py @@ -1,8 +1,7 @@ -from .base import Base - from ..converter_utils import let_user_pick from ..converter_utils import print_message as prmsg from ..converter_utils import run_ffmpeg +from .base import Base class Audio(Base): @@ -31,8 +30,8 @@ def __init__(self, options, *args, **kwargs): def run(self): """Run the Audio command.""" chosen_option = let_user_pick(self.conversion_map) - source_path, output_paths, params = self.get_user_input( + source_paths, output_paths, params = self.get_user_input( self.conversion_map[chosen_option]) - for output_path in output_paths: + for (source_path, output_path) in list(zip(source_paths, output_paths)): run_ffmpeg(source_path, output_path, params, self.options) prmsg('completed') diff --git a/convert/commands/base.py b/convert/commands/base.py index 0e7510b..c1bdf80 100644 --- a/convert/commands/base.py +++ b/convert/commands/base.py @@ -2,12 +2,9 @@ from termcolor import colored -from ..converter_utils import clear -from ..converter_utils import confirmator -from ..converter_utils import multi_source +from ..converter_utils import clear, confirmator, multi_source from ..converter_utils import print_message as prmsg -from ..converter_utils import single_source -from ..converter_utils import validate_path +from ..converter_utils import single_source, validate_path class Base(object): @@ -30,7 +27,7 @@ def get_user_input(self, conversion_data): :param conversion_data: command based data (see command init) :type conversion_data: dict - :returns: source_path + :returns: source_paths output_paths conversion_data_params :rtype: string @@ -49,6 +46,7 @@ def get_user_input(self, conversion_data): "(Enter for same folder as source): ", 'green')) or default_folder destination = validate_path(destination, 'folder') + source_paths = [] output_paths = [] # multiple files flow @@ -60,9 +58,6 @@ def get_user_input(self, conversion_data): 'out_ext': conversion_data['extension'], 'out_folder': destination}) - # output_paths = get_multiple_outputs( - # source_folder, source_extension) - folder = os.fsencode(source_folder) for file in os.listdir(folder): # get source filename @@ -74,6 +69,7 @@ def get_user_input(self, conversion_data): # check extension fits if source_ext == '.{}'.format(source_extension): + source_paths.append(source_path) output_path = '{}{}.{}'.format( destination, source_name, conversion_data['extension']) output_paths.append(output_path) @@ -87,10 +83,11 @@ def get_user_input(self, conversion_data): 'out_ext': conversion_data['extension'], 'out_folder': destination}) + source_paths = [source_path] output_paths = ['{}{}.{}'.format( destination, source_name, conversion_data['extension'])] - return source_path, output_paths, conversion_data['params'] + return source_paths, output_paths, conversion_data['params'] def run(self): """All commands must implement this method.""" diff --git a/convert/commands/video.py b/convert/commands/video.py index 4b7ba44..f3be3c8 100755 --- a/convert/commands/video.py +++ b/convert/commands/video.py @@ -1,8 +1,7 @@ -from .base import Base - from ..converter_utils import let_user_pick from ..converter_utils import print_message as prmsg from ..converter_utils import run_ffmpeg +from .base import Base class Video(Base): @@ -63,8 +62,8 @@ def __init__(self, options, *args, **kwargs): def run(self): """Run the Video command.""" chosen_option = let_user_pick(self.conversion_map) - source_path, output_paths, params = self.get_user_input( + source_paths, output_paths, params = self.get_user_input( self.conversion_map[chosen_option]) - for output_path in output_paths: + for (source_path, output_path) in list(zip(source_paths, output_paths)): run_ffmpeg(source_path, output_path, params, self.options) prmsg('completed') diff --git a/convert/converter_utils/utils.py b/convert/converter_utils/utils.py index c3d17ac..a28b37e 100644 --- a/convert/converter_utils/utils.py +++ b/convert/converter_utils/utils.py @@ -2,7 +2,6 @@ import sys import ffmpeg - from termcolor import colored @@ -135,26 +134,26 @@ def let_user_pick(conversion_map): def validate_path(path, path_type): - valid = True - if path_type not in ('file', 'folder'): - print_message('wrong_path_option', **{'path_type': path_type}) - sys.exit(1) - - elif path_type == 'file': - if not os.path.isfile(path): - valid = False - - elif path_type == 'folder': - if path[-1] != '/': - path = path + '/' - if not os.path.exists(path): - valid = False - - if not valid: - print_message('invalid_path', **{'path': path}) - sys.exit(2) + valid = True + if path_type not in ('file', 'folder'): + print_message('wrong_path_option', **{'path_type': path_type}) + sys.exit(1) + + elif path_type == 'file': + if not os.path.isfile(path): + valid = False - return path + elif path_type == 'folder': + if path[-1] != '/': + path = path + '/' + if not os.path.exists(path): + valid = False + + if not valid: + print_message('invalid_path', **{'path': path}) + sys.exit(2) + + return path def single_source(): @@ -198,37 +197,37 @@ def user_confirmed(confirmation): def confirmator(options, **kwargs): - """""" - # Show confirmation message when the --no-confirm option is missing - if not options['--no-confirm']: - # display warning - print_message('warning') - - if options['--multiple']: - print_message( - 'confirm_multi', - **{ - 'ori_ext': kwargs['ori_ext'], - 'ori_folder': kwargs['ori_folder'], - 'out_ext': kwargs['out_ext'], - 'out_folder': kwargs['out_folder']}) + """""" + # Show confirmation message when the --no-confirm option is missing + if not options['--no-confirm']: + # display warning + print_message('warning') - else: - print_message( - 'confirm_single', - **{ - 'ori_path': kwargs['ori_path'], - 'out_ext': kwargs['out_ext'], - 'out_folder': kwargs['out_folder']}) - - confirmation = input( - colored('\nPlease confirm action above [y/n]: ', 'red')) - - if not user_confirmed(confirmation): - sys.exit(2) - else: - # clear screen - clear() + if options['--multiple']: + print_message( + 'confirm_multi', + **{ + 'ori_ext': kwargs['ori_ext'], + 'ori_folder': kwargs['ori_folder'], + 'out_ext': kwargs['out_ext'], + 'out_folder': kwargs['out_folder']}) + + else: + print_message( + 'confirm_single', + **{ + 'ori_path': kwargs['ori_path'], + 'out_ext': kwargs['out_ext'], + 'out_folder': kwargs['out_folder']}) + + confirmation = input( + colored('\nPlease confirm action above [y/n]: ', 'red')) + + if not user_confirmed(confirmation): + sys.exit(2) + else: + # clear screen + clear() def run_ffmpeg(source_path, output_path, ffmpeg_params, options):