alghul96
alghul96

Reputation: 75

Trying to send commands via Subprocess to operate with ffmpeg

I am trying to build a script that converts video files via ffmpeg inside Python 3. Via Windows PowerShell I successfully obtained the desired result via the following command:

ffmpeg -i test.webm -c:v libx264 converted.mp4

However, if I try to repeat the same operation inside python via the following code:

import subprocess
from os import getcwd

print(getcwd()) # current directory
subprocess.call(["ffmpeg", " -f test.webm -c libx264 converted.mp4"])

I get the following error:

Output #0, mp4, to ' -f test.webm -c libx264 converted.mp4':
Output file #0 does not contain any stream

I am in the correct folder where the files are. Do you have better methods to execute commands in shell via Python? That should preferably work on different platforms.

Upvotes: 0

Views: 331

Answers (1)

J. Anderson
J. Anderson

Reputation: 309

try this:

import shlex

cmd = shlex.split("ffmpeg -f test.webm -c libx264 converted.mp4")
subprocess.call(cmd)

you need pass each argument as a single element in a list, that's how argv works, or let shell do the split:

subprocess.call("ffmpeg -f test.webm -c libx264 converted.mp4", shell=True)

Upvotes: 1

Related Questions