Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature error cmdline #751

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/get_video_thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def generate_thumbnail(in_filename, out_filename, time, width):
.run(capture_stdout=True, capture_stderr=True)
)
except ffmpeg.Error as e:
print(e.stderr.decode(), file=sys.stderr)
print(f"$ {e.cmdline}\n{e.stderr.decode()}", file=sys.stderr)
sys.exit(1)


Expand Down
2 changes: 1 addition & 1 deletion examples/show_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,6 @@ def handler(key, value):
.run(capture_stdout=True, capture_stderr=True)
)
except ffmpeg.Error as e:
print(e.stderr, file=sys.stderr)
print(f"$ {e.cmdline}\n{e.stderr.decode()}", file=sys.stderr)
sys.exit(1)

2 changes: 1 addition & 1 deletion examples/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def decode_audio(in_filename, **input_kwargs):
.run(capture_stdout=True, capture_stderr=True)
)
except ffmpeg.Error as e:
print(e.stderr, file=sys.stderr)
print(f"$ {e.cmdline}\n{e.stderr.decode()}", file=sys.stderr)
sys.exit(1)
return out

Expand Down
2 changes: 1 addition & 1 deletion examples/video_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
try:
probe = ffmpeg.probe(args.in_filename)
except ffmpeg.Error as e:
print(e.stderr, file=sys.stderr)
print(f"$ {e.cmdline}\n{e.stderr.decode()}", file=sys.stderr)
sys.exit(1)

video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
Expand Down
5 changes: 3 additions & 2 deletions ffmpeg/_probe.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import subprocess
from ._run import Error
from ._utils import convert_kwargs_to_cmd_line_args
from ._utils import convert_kwargs_to_cmd_line_args, convert_cmd_line_args_to_cmd_line_str


def probe(filename, cmd='ffprobe', timeout=None, **kwargs):
Expand All @@ -23,7 +23,8 @@ def probe(filename, cmd='ffprobe', timeout=None, **kwargs):
communicate_kwargs['timeout'] = timeout
out, err = p.communicate(**communicate_kwargs)
if p.returncode != 0:
raise Error('ffprobe', out, err)
cmdline = convert_cmd_line_args_to_cmd_line_str(args)
raise Error('ffprobe', out, err, cmdline)
return json.loads(out.decode('utf-8'))


Expand Down
9 changes: 6 additions & 3 deletions ffmpeg/_run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import unicode_literals
from .dag import get_outgoing_edges, topo_sort
from ._utils import basestring, convert_kwargs_to_cmd_line_args
from ._utils import basestring, convert_kwargs_to_cmd_line_args, convert_cmd_line_args_to_cmd_line_str
from builtins import str
from functools import reduce
import copy
Expand All @@ -24,12 +24,13 @@


class Error(Exception):
def __init__(self, cmd, stdout, stderr):
def __init__(self, cmd, stdout, stderr, cmdline):
super(Error, self).__init__(
'{} error (see stderr output for detail)'.format(cmd)
)
self.stdout = stdout
self.stderr = stderr
self.cmdline = cmdline


def _get_input_args(input_node):
Expand Down Expand Up @@ -334,7 +335,9 @@ def run(
out, err = process.communicate(input)
retcode = process.poll()
if retcode:
raise Error('ffmpeg', out, err)
args = compile(stream_spec, cmd, overwrite_output=overwrite_output)
cmdline = convert_cmd_line_args_to_cmd_line_str(args)
raise Error('ffmpeg', out, err, cmdline)
return out, err


Expand Down
13 changes: 11 additions & 2 deletions ffmpeg/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from builtins import str
from past.builtins import basestring
import hashlib
import sys

import sys, os
import subprocess
import shlex

if sys.version_info.major == 2:
# noinspection PyUnresolvedReferences,PyShadowingBuiltins
Expand Down Expand Up @@ -106,3 +107,11 @@ def convert_kwargs_to_cmd_line_args(kwargs):
if v is not None:
args.append('{}'.format(v))
return args


def convert_cmd_line_args_to_cmd_line_str(args):
"""Helper function to build command line string from command line args."""
if os.name == "nt": # Windows system
return subprocess.list2cmdline(args)
# Non-windows systems
return shlex.join(args)