Skip to content

Commit

Permalink
new way to specify extra options
Browse files Browse the repository at this point in the history
  • Loading branch information
slhck committed May 5, 2018
1 parent acf0654 commit 20ec9da
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ You can (if you really need to!) also overwrite your input file. Warning, this w

If you need some fancy extra options, such as setting `vbr` for the `libfdk_aac` encoder, pass them to the `-e`/`--extra-options` argument:

ffmpeg-normalize input.m4a -c:a libfdk_aac -e '["-vbr": "3"]' -o output.m4a
ffmpeg-normalize input.m4a -c:a libfdk_aac -e='-vbr 3' -o output.m4a

Further examples? Please submit a PR so I can collect them.

Expand Down Expand Up @@ -175,9 +175,15 @@ Output Format:

- `-e EXTRA_OUTPUT_OPTIONS, --extra-output-options EXTRA_OUTPUT_OPTIONS`: Extra output options list.

A JSON-formatted list of ffmpeg command line arguments. Wrap in quotes to prevent shell expansion and to preserve literal quotes inside string.
A list of extra ffmpeg command line arguments.

Example: `-e '[ "-vbr", "3" ]'`
You can either use a JSON-formatted list, or a simple string of space-
separated arguments. If JSON is used, you need to wrap the argument in
quotes to prevent shell expansion and to preserve literal quotes
inside the string. If a simple string is used, you need to specify the
argument with `-e=`.

Examples: `-e '[ "-vbr", "3" ]'` or `-e="-vbr 3"`

- `-ofmt OUTPUT_FORMAT, --output-format OUTPUT_FORMAT`: Media format to use for output file(s).

Expand Down
12 changes: 8 additions & 4 deletions ffmpeg_normalize/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,15 @@ def create_parser():
help=textwrap.dedent("""\
Extra output options list.
A JSON-formatted list of ffmpeg command line arguments. Wrap in quotes
to prevent shell expansion and to preserve literal quotes inside
string.
A list of extra ffmpeg command line arguments.
Example: `-e '[ "-vbr", "3" ]'`
You can either use a JSON-formatted list, or a simple string of space-
separated arguments. If JSON is used, you need to wrap the argument in
quotes to prevent shell expansion and to preserve literal quotes
inside the string. If a simple string is used, you need to specify the
argument with `-e=`.
Examples: `-e '[ "-vbr", "3" ]'` or `-e="-vbr 3"`
""")
)
group_format.add_argument(
Expand Down
14 changes: 13 additions & 1 deletion ffmpeg_normalize/_ffmpeg_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
import json
from numbers import Number
from tqdm import tqdm
import shlex

try:
from json.decoder import JSONDecodeError
except ImportError:
JSONDecodeError = ValueError

from ._cmd_utils import get_ffmpeg_exe, ffmpeg_has_loudnorm
from ._media_file import MediaFile
Expand Down Expand Up @@ -107,7 +113,13 @@ def __init__(
if extra_output_options:
try:
if isinstance(extra_output_options, str):
self.extra_output_options = [str(s) for s in json.loads(extra_output_options)]
if extra_output_options.startswith('['):
try:
self.extra_output_options = [str(s) for s in json.loads(extra_output_options)]
except JSONDecodeError as e:
self.extra_output_options = shlex.split(extra_output_options)
else:
self.extra_output_options = shlex.split(extra_output_options)
elif isinstance(extra_output_options, list):
self.extra_output_options = extra_output_options
except Exception as e:
Expand Down
23 changes: 16 additions & 7 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@

sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/../'))

def ffmpeg_normalize_call(args, env=None):
cmd = [sys.executable, '-m', 'ffmpeg_normalize']
cmd.extend(args)
print()
print(" ".join(cmd))
def ffmpeg_normalize_call(args, env=None, raw=False):
if not raw:
cmd = [sys.executable, '-m', 'ffmpeg_normalize']
cmd.extend(args)
print()
print(" ".join(cmd))
else:
cmd = sys.executable + ' -m ffmpeg_normalize ' + args
print(cmd)
p = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
env=env
env=env,
shell=raw
)
stdout, stderr = p.communicate()

Expand Down Expand Up @@ -87,10 +92,14 @@ def test_vcodec(self):
ffmpeg_normalize_call(['test/test.mp4', '-c:v', 'libx264'])
self.assertTrue(os.path.isfile('normalized/test.mkv'))

def test_extra_options(self):
def test_extra_options_json(self):
ffmpeg_normalize_call(['test/test.mp4', '-c:a', 'aac', '-e', '[ "-vbr", "3" ]'])
self.assertTrue(os.path.isfile('normalized/test.mkv'))

def test_extra_options_normal(self):
output, _ = ffmpeg_normalize_call('test/test.mp4 -c:a aac -e="-vbr 3"', raw=True)
self.assertTrue(os.path.isfile('normalized/test.mkv'))

def test_ofmt_fail(self):
Path('normalized').mkdir(exist_ok=True)
output, _ = ffmpeg_normalize_call(['test/test.mp4', '-ofmt', 'mp3', '-o', 'normalized/test.mp3', '-vn', '-sn'])
Expand Down

0 comments on commit 20ec9da

Please sign in to comment.