user2408159
user2408159

Reputation: 51

Running ffmpeg command through python script

I am trying to execute ffmpeg commands in python. When the following command was executed from command line in windows cmd it worked:

C:\FFmpeg\bin\ffmpeg -rtbufsize 100000k -r 65535/2733 -f dshow -i audio="virtual-audio-capturer":video="screen-capture-recorder" output100.avi

However when I try to run the this command in python in this way:

cmd='C:\FFmpeg\bin\ffmpeg -rtbufsize 100000k -r 65535/2733 -f dshow -i audio="virtual-audio-capturer":video="screen-capture-recorder" output100.avi'
subprocess.call(cmd, shell=true)

it doesn't work

I also tried this way

cmd='C:\FFmpeg\bin\ffmpeg -rtbufsize 100000k -r 65535/2733 -f dshow -i audio="virtual-audio-capturer":video="screen-capture-recorder" output100.avi'
subprocess.check_call(cmd) 

But it didn't work as well

I would like to know what am I doing wrong.I using python 2.76.Thanks.

Upvotes: 1

Views: 8203

Answers (6)

Harry
Harry

Reputation: 1277

This is an old post but I still find it useful today. I got mine working and so I would like to share that with you.

My video file is more than 3 hour long (3:09:09), I just wanted to extract one frame specifically at 20 minutes and 17 seconds (20:17) to a picture. So here's the working code (tested on Windows 10, 64-bit, Python 3.7):

import os
#Input video file
in_video=r"C:\temp\tu\my-trip-on-the-great-wall.rmvb"
#Output image file
out_image=r"C:\Users\rich_dad\Documents\test2.jpg"

#ffmpeg installation path
appDir=r"c:\ffmpeg\bin"

#Change directory on the go
os.chdir(appDir)

#Execute command
os.system("ffmpeg -ss 20:17 -i "+in_video+" -vframes 1 -q:v 2 "+out_image)

I would definitively add a loop to this if I need to get more photos out of a video. I hope you will find this useful.

Upvotes: 1

rwg
rwg

Reputation: 31

I wanted to convert some movie files to audio files,but I couldn't get ffmpeg to execute in Python, until I explicitly included paths, something like:

import os

Executable = r'C:\Users\rrabcdef\Documents\p\apps\ffmpeg\ffmpeg.exe'
input = r'C:\Users\rrabcdef\Documents\p\myStuff\clip_1.mov'
output = r'C:\Users\rrabcdef\Documents\p\myStuff\clip_1.mp3'
myCommand = Executable + " -i " + input + " -f mp3 -ab 320000 -vn " + output
os.system(myCommand)

Upvotes: 3

zaxvo
zaxvo

Reputation: 186

user3 was almost right, you need to pass the command as a list of strings:

cmd=['C:\FFmpeg\bin\ffmpeg', '-rtbufsize', '100000k', '-r', '65535/2733', '-f', 'dshow', '-i', 'audio="virtual-audio-capturer":video="screen-capture-recorder"', 'output100.avi']
subprocess.check_call(cmd) 

Upvotes: 0

venpa
venpa

Reputation: 4318

Windowserror:[Error 2] is coming due to shell=False error.

If You are running the command giving cmd as string, then you must use shell=True:

cmd='C:\FFmpeg\bin\ffmpeg -rtbufsize 100000k -r 65535/2733 -f dshow -i audio="virtual-audio-capturer":video="screen-capture-recorder" output100.avi'
subprocess.check_call(cmd, shell=True)

If you are running without shell=True, you have to pass cmd as list:

cmd='C:\FFmpeg\bin\ffmpeg -rtbufsize 100000k -r 65535/2733 -f dshow -i audio="virtual-audio-capturer":video="screen-capture-recorder" output100.avi'
subprocess.check_call([cmd])

above statements are same for Popen and check_call functions.

Upvotes: 0

Jeff
Jeff

Reputation: 4433

Without the error message, I can't tell, but most documentation says to use "ffmpeg.exe" as the binary when calling the executable. Also, you could make the args a list and pass it in:

NOT TESTED

import subprocess as sp

def get_ffmpeg_bin():
    ffmpeg_dir = "C:\\FFmpeg\\bin\\ffmpeg"
    FFMPEG_BIN = os.path.join(ffmpeg_dir, "ffmpeg.exe")
    return FFMPEG_BIN


pipe = sp.Popen([ffmpeg_binary, "-rtbufsize", "100000k", "-r", "65535/2733", "-f", "dshow", "-i", 'audio="virtual-audio-capturer":video="screen-capture-recorder"', "output100.avi"])

pipe.wait()

Upvotes: 0

austin-schick
austin-schick

Reputation: 1245

Try this:

import os
os.system(cmd)

As far as I can tell this method isn't as advanced as subprocess, but it does what it's supposed to do.

Upvotes: 0

Related Questions