Fam
Fam

Reputation: 15

Python running a cmd command(ffmpeg) with subprocess and wait for cmd to close before executing some code

So im using ffmpeg to resize a video, and this takes a little while so before the code continues i have to wait for the cmd to close. I havent been able to find a solution and i hope someone that actually knows their stuff can teach me a way. im probably just doing something retarded... Anyway i have tried multiple things like using subprocess.Propen() and then using pope() to see when it doesnt return "None" also tried stuff like subprocess.run() with subprocess.CompletedProcess() but i just cant get it to work could someone please care to explain if there is a way to do this and that im just doing it wrong? :)

Here is an example of what I tried

p = subprocess.Popen("start ffmpeg -y -i "+DIR+"/post.mp4 -vf scale="+str(nW)+":"+str(nH)+" "+DIR+"/post_r.mp4", shell=True)
    while p.poll() is None:
        time.sleep(1)
        print("alive")
    else:
        print("cmd exited")

Also whenever i put shell=False it just breaks instantly and I get this error: FileNotFoundError: [WinError 2] The system cannot find the file specified

Upvotes: 1

Views: 1458

Answers (1)

tripleee
tripleee

Reputation: 189297

Don't use subprocess.Popen() if you simply need the process to run while you wait. Legacy subprocess.call() or the modern subprocess.run() are designed exactly for this use case.

With shell=False you need to split the command line into a list yourself. You can call on shlex.split() for convenience, but it's not hard to do yourself.

subprocess.run(['ffmpeg', '-y', '-i', DIR+"/post.mp4",
    '-vf', 'scale=' + str(nW) + ":" + str(nH), DIR+"/post_r.mp4"])

The start looks like you are on Windows and of course then start is a feature of the shell as well; if you don't have a shell, start is not available (but also not useful).

Upvotes: 1

Related Questions