16

I have created a simple method that executes a command like you do in the terminal

from subprocess import PIPE, run

class Command_Line():

    @staticmethod
    def execute(command):
        result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
        print(result)
        return result.stdout

My problem with the code above is it does not wait until the task/process is done. Lets say i use ffmpeg to change the frame rate of a video via the following code

import Command_Line as cmd    
cmd.execute('ffmpeg -i "000000004.avi" -c copy -y -r 30 "000000004.avi"')

The problem is the output video because it does not complete the process. I've search how to wait like Is there a way to check if a subprocess is still running? but could not incorporate it with my code. Can you share your experience with this.

Thanks

jameshwart lopez
  • 2,993
  • 6
  • 35
  • 65
  • I tested your script on both Windows and Linux with `ffmpeg -i movie.avi movie.mp4` (to increase the processing time). On my machine `subprocess.run` waits until the `ffmpeg` process terminates as it should according to the [docs](https://docs.python.org/3/library/subprocess.html). Quote: `"Wait for command to complete, then return a CompletedProcess instance."` – Marius Mar 15 '18 at 08:01
  • What exactly do you mean by "The problem is the output video because it does not complete the process."? Is the output video not written at all when you run your script or is it too small? Are there any error messages? – Marius Mar 15 '18 at 08:07
  • When i execute my code the video became broken and if i remove that part of my code the video is okay. Having that with my code the video Length of the video become zero. So then i tried to manually execute the command above(via terminal) but its working so I was thinking that the process was terminated halfway that causes the video to be broken – jameshwart lopez Mar 15 '18 at 08:26
  • 2
    I tested again with exactly the same ffmpeg command that you use and I think the problem is that you overwrite your input file https://stackoverflow.com/questions/28877049/issue-with-overwriting-file-while-using-ffmpeg-for-converting The video also becomes invalid if I run this command directly in a terminal. Can you try to change the output file name? – Marius Mar 15 '18 at 09:23
  • @Marius you should put that one as the answer so that i can give you the bounty ;) thats the problem and i just need to change the file name before changing its frame rate. Thanks a lot – jameshwart lopez Mar 15 '18 at 09:34

2 Answers2

13

According to the python documentation subprocess.run waits for the process to end.

The problem is that ffmpeg overwrites the input file if the input and output files are the same and therefore the output video becomes unusable.

Marius
  • 1,529
  • 6
  • 21
  • Is there a way to wait in a non blocking manner using suprocess.run()? I want to add some wait animation while waiting for the process to to end. – sham1810 Mar 22 '23 at 10:11
5

subprocess.run() is synchronous which means that the system will wait till it finishes before moving on to the next command. subprocess.Popen() does the same thing but it is asynchronous (the system will not wait for it to finish). You can try reloading your file using importlib.reload command. It may find your generated file then.

Rafeed
  • 61
  • 1
  • 1