2

I have this ffmpeg command:

ffmpeg -y -i 6.mp4 -vf scale=1280:-2,setsar=1:1 -c:v libx264 -c:a copy 720p.mp4

and I want to implement it via python code. for this I used subprocess function and I use the following code for this:

subprocess.call(['ffmpeg.exe','-y','-i', pname1,'-vf','scale=','1280:-2','setsar=','1:1','-c:v', 'libx264', '-c:a', 'copy', pname2])

pname1 and pname2 are the names of input and output files. this program is running without any error, but it produces nothing. do you know what is the problem?

2
  • 1
    "Running without any error" -- how do you know that? I would also strongly suggest passing check=True if you aren't going to store and inspect the returned object. Also, if you don't set stdout=subprocess.PIPE and stderr=subprocess.PIPE you can't see what output was written from Python and thus need to inspect whatever stdout and stderr was inherited from the Python process itself. Commented Nov 2, 2021 at 12:04
  • Did you verify that your ffmpeg command worked before attempting to put in into Python code? Commented Nov 2, 2021 at 17:48

2 Answers 2

1

The correct syntax is:

subprocess.call(['ffmpeg.exe', '-y', '-i', pname1, '-vf', 'scale=1280:-2,setsar=1:1', '-c:v', 'libx264', '-c:a', 'copy', pname2])

Each element in the list (after the first one) represents an argument.
The sperate arguments to FFmpeg may be identified by the spaces between them (when executing FFmpeg in command line).

When executing you original command, I am getting an error message:

[NULL @ 0000020fb89bc2c0] Unable to find a suitable output format for '1280:-2' 1280:-2: Invalid argument

The error message is printed to stderr (usually printed to the console).

Sign up to request clarification or add additional context in comments.

Comments

0

To see what the process is doing you can capture the output by replacing:

subprocess.call(["command"])

with:

subprocess.check_output(["command"], stderr=subprocess.STDOUT)

See https://docs.python.org/3.9/library/subprocess.html#subprocess.check_output

So your call would become:

ffmpeg_output = subprocess.check_output(
    ['ffmpeg.exe','-y','-i', pname1,'-vf','scale=','1280:-2','setsar=','1:1','-c:v', 'libx264', '-c:a', 'copy', pname2],
    stderr=subprocess.STDOUT
)
print(ffmpeg_output)

2 Comments

(I'd argue that this is more fit to be a comment describing a debugging hint; an answer should be a proper fix to the problem; a question that isn't answerable should be pushed back on until the OP adds enough information to allow it to be answered).
Beyond that: You don't need to coalesce stderr and stdout to check them -- one can (and, I'd argue should) keep them separate and capture both, as done by default with subprocess.run(..., capture_output=True). Also, add check=True and you'll get an exception when exit status is nonzero without needing to use one of the check_* functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.