Skip to content

Commit

Permalink
Merge branch 'release/1.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
patillacode committed Oct 26, 2018
2 parents b042a55 + 7efa6a7 commit 0af9d39
Show file tree
Hide file tree
Showing 16 changed files with 208 additions and 132 deletions.
48 changes: 42 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
# Byte-compiled / optimized / DLL files
__pycache__/

# Distribution / packaging
build
dist
*.egg-info

# Unit test / coverage reports
.cache
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/


# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

*.pyc
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ converter-cli
Purpose
-------

This is a custom Python CLI for converting media files
This is a custom Python (3) CLI for converting media files

I've done this to make it easy to convert my media files into different
formats, bitrates and whatnot without having to remember the ffmpeg syntax for
Expand All @@ -24,7 +24,7 @@ Plug & Play:
$ pip install converter-cli


If you want to play around with the code I'd recommend creating a virtual environment:
If you want to play around with the code I'd recommend creating a virtual environment (with python3):

$ mkvirtualenv --python=/usr/local/bin/python3 converter-cli

Expand Down Expand Up @@ -58,24 +58,26 @@ Full command list follows:
Usage:
converter-cli hello
converter-cli audio
converter-cli audio [-m | --multiple | --verbose]
converter-cli audio [-m | --multiple] [--verbose] [-n | --no-confirm]
converter-cli video
converter-cli video [-m | --multiple | --verbose]
converter-cli video [-m | --multiple] [--verbose] [-n | --no-confirm]
converter-cli -h | --help
converter-cli -v | --version

Options:
-h --help Show this screen.
-v --version Show version.
-m --multiple Convert all files within a given folder
-n --no-confirm Avoid user confirmation before converting
--verbose Redirect converting process to stdout


By default the CLI will be used to convert one file, but the `-m`/`--multiple` option will allow you to do multiple files at once:

$ converter-cli video -m

Also by default the CLi will hide the output of the ffmpeg command unless the `--verbose` option is specified:
Also by default, the CLi will hide the output of the ffmpeg command in favor of a more readable custom line,
unless the `--verbose` option is specified:

$ converter-cli audio --verbose

Expand Down Expand Up @@ -105,7 +107,7 @@ Here we will enter something like `/path/to/file/music.wav` and hit enter.
Notice if your click enter without specifying any path then the output file will
be in the same directory as the source file `/path/to/file/`

Next thing to happen will be:
Next thing to happen will be (unless the `--no-confirm option` was given):


WARNING
Expand Down
2 changes: 1 addition & 1 deletion convert/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.3'
__version__ = '1.0.4'
Binary file removed convert/__init__.pyc
Binary file not shown.
5 changes: 3 additions & 2 deletions convert/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
Usage:
converter-cli hello
converter-cli audio
converter-cli audio [-m | --multiple | --verbose]
converter-cli audio [-m | --multiple] [--verbose] [-n | --no-confirm]
converter-cli video
converter-cli video [-m | --multiple | --verbose]
converter-cli video [-m | --multiple] [--verbose] [-n | --no-confirm]
converter-cli -h | --help
converter-cli -v | --version
Options:
-h --help Show this screen.
-v --version Show version.
-m --multiple Convert all files within a given folder
-n --no-confirm Avoid user confirmation before converting
--verbose Redirect converting process to stdout
Examples:
Expand Down
Binary file removed convert/cli.pyc
Binary file not shown.
Binary file removed convert/commands/__init__.pyc
Binary file not shown.
13 changes: 10 additions & 3 deletions convert/commands/audio.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from .base import Base

from .utils import let_user_pick
from ..converter_utils import let_user_pick
from ..converter_utils import print_message as prmsg
from ..converter_utils import run_ffmpeg


class Audio(Base):
"""Get user input to execute different video conversions"""

def __init__(self, options, *args, **kwargs):
super(Audio, self).__init__(options, *args, **kwargs)
super().__init__(options, *args, **kwargs)
self.conversion_map = {
1: {
'option_text': 'Convert to .mp3 (320k)',
Expand All @@ -28,4 +30,9 @@ def __init__(self, options, *args, **kwargs):

def run(self):
"""Run the Audio command."""
self.convert(self.conversion_map[let_user_pick(self.conversion_map)])
chosen_option = let_user_pick(self.conversion_map)
source_path, output_paths, params = self.get_user_input(
self.conversion_map[chosen_option])
for output_path in output_paths:
run_ffmpeg(source_path, output_path, params, self.options)
prmsg('completed')
Binary file removed convert/commands/audio.pyc
Binary file not shown.
139 changes: 36 additions & 103 deletions convert/commands/base.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import os
import sys

import ffmpeg

from termcolor import colored

from .utils import clear
from .utils import multi_source
from .utils import print_message as prmsg
from .utils import single_source
from .utils import validate_path
from ..converter_utils import clear
from ..converter_utils import confirmator
from ..converter_utils import multi_source
from ..converter_utils import print_message as prmsg
from ..converter_utils import single_source
from ..converter_utils import validate_path


class Base(object):
"""A base command."""

def __init__(self, options, *args, **kwargs):
"""Creates the Base object (a command object).
"""Create the Base object (a command object).
:param options: command options
:type options: dict
Expand All @@ -27,44 +25,17 @@ def __init__(self, options, *args, **kwargs):
self.kwargs = kwargs
clear()

def confirm_multiple(self, ori_ext, ori_folder, out_ext, out_folder):
""""""
prmsg('confirm_multi',
**{
'ori_ext': ori_ext,
'ori_folder': ori_folder,
'out_ext': out_ext,
'out_folder': out_folder})

confirmation = input(
colored('\nPlease confirm action above [y/n]: ', 'red'))

if confirmation not in ('y', ''):
return False

return True

def confirm_single(self, ori_path, out_ext, out_folder):
""""""
prmsg('confirm_single',
**{
'ori_path': ori_path,
'out_ext': out_ext,
'out_folder': out_folder})

confirmation = input(
colored('\nPlease confirm action above [y/n]: ', 'red'))

if confirmation not in ('y', ''):
return False

return True

def convert(self, conversion_data):
def get_user_input(self, conversion_data):
"""Set all needed variables via user input for the conversion.
:param conversion_data: command based data (see command init)
:type source_path: dict
:type conversion_data: dict
:returns: source_path
output_paths
conversion_data_params
:rtype: string
list
list
"""

if self.options['--multiple']:
Expand All @@ -78,19 +49,19 @@ def convert(self, conversion_data):
"(Enter for same folder as source): ", 'green')) or default_folder
destination = validate_path(destination, 'folder')

# display warning
prmsg('warning')
output_paths = []

# multiple files flow
if self.options['--multiple']:
confirmator(
self.options,
**{'ori_ext': source_extension,
'ori_folder': source_folder,
'out_ext': conversion_data['extension'],
'out_folder': destination})

if not self.confirm_multiple(source_extension,
source_folder,
conversion_data['extension'],
destination):
sys.exit(2)

# clear screen
clear()
# output_paths = get_multiple_outputs(
# source_folder, source_extension)

folder = os.fsencode(source_folder)
for file in os.listdir(folder):
Expand All @@ -105,59 +76,21 @@ def convert(self, conversion_data):
if source_ext == '.{}'.format(source_extension):
output_path = '{}{}.{}'.format(
destination, source_name, conversion_data['extension'])
output_paths.append(output_path)

self.run_ffmpeg(
source_path, output_path, conversion_data['params'])

# single file flow
else:
# do not show confirmation message if the option is enabled
confirmator(
self.options,
**{'ori_path': source_path,
'out_ext': conversion_data['extension'],
'out_folder': destination})

if not self.confirm_single(source_path,
conversion_data['extension'],
destination):
sys.exit(2)

output_path = '{}{}.{}'.format(
destination, source_name, conversion_data['extension'])

# clear screen
clear()

self.run_ffmpeg(
source_path, output_path, conversion_data['params'])

prmsg('completed')

def run_ffmpeg(self, source_path, output_path, params):
"""Trigger ffmpeg command via the ffmpeg-python lib and given params.
:param source_path: source media file path
:type source_path: string
:param output_path: output media file path
:type output_path: string
:param params: ffmpeg command options
:type params: dict
"""
output_paths = ['{}{}.{}'.format(
destination, source_name, conversion_data['extension'])]

try:
if not self.options['--verbose']:
prmsg('converting',
**{
'source_path': source_path,
'output_path': output_path})
(
ffmpeg
.input(source_path)
.output(
output_path,
**params,
)
.overwrite_output()
.run(quiet=not(self.options['--verbose']))
)

except Exception as exc:
prmsg('exception', **{'exc': exc})
sys.exit(1)
return source_path, output_paths, conversion_data['params']

def run(self):
"""All commands must implement this method."""
Expand Down
Binary file removed convert/commands/base.pyc
Binary file not shown.
Binary file removed convert/commands/hello.pyc
Binary file not shown.
13 changes: 10 additions & 3 deletions convert/commands/video.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from .base import Base

from .utils import let_user_pick
from ..converter_utils import let_user_pick
from ..converter_utils import print_message as prmsg
from ..converter_utils import run_ffmpeg


class Video(Base):
"""Get user input to execute different video conversions"""

def __init__(self, options, *args, **kwargs):
super(Video, self).__init__(options, *args, **kwargs)
super().__init__(options, *args, **kwargs)
self.conversion_map = {
1: {
'option_text': 'Convert to .mp4',
Expand Down Expand Up @@ -60,4 +62,9 @@ def __init__(self, options, *args, **kwargs):

def run(self):
"""Run the Video command."""
self.convert(self.conversion_map[let_user_pick(self.conversion_map)])
chosen_option = let_user_pick(self.conversion_map)
source_path, output_paths, params = self.get_user_input(
self.conversion_map[chosen_option])
for output_path in output_paths:
run_ffmpeg(source_path, output_path, params, self.options)
prmsg('completed')
Binary file removed convert/commands/video.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions convert/converter_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .utils import *
Loading

0 comments on commit 0af9d39

Please sign in to comment.