Skip to content

Commit

Permalink
fix multiple files conversion (#10)
Browse files Browse the repository at this point in the history
When using the `--multiple` option only one file was being converted into several output files.
This is the fix for that wrong behaviour.
  • Loading branch information
kifirkin authored Oct 25, 2021
1 parent d59ef31 commit 7d33da7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 68 deletions.
7 changes: 3 additions & 4 deletions convert/commands/audio.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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')
17 changes: 7 additions & 10 deletions convert/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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."""
Expand Down
7 changes: 3 additions & 4 deletions convert/commands/video.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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')
99 changes: 49 additions & 50 deletions convert/converter_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import sys

import ffmpeg

from termcolor import colored


Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 7d33da7

Please sign in to comment.