I've been using Python to run a video processing program on a large collection of .mp4 files. The video processing program (which I did not write and can't alter) does not exit once it reaches the final frame of the video, so using os.system(cmd)
in a loop going through all the .mp4 files didn't work for me unless I sat there killing the processing program after each video ended.
I tried to solve this using a subprocess that got terminated after the video ended (a predetermined amount of time):
for file in os.listdir(myPath):
if file.endswith(".mp4"):
vidfile = os.path.join(myPath, file)
command = "./Tracking " + vidfile
p = subprocess.Popen(command, shell=True)
sleep(840)
p.terminate()
However, the Tracking
program still doesn't exit, so I end up with tons of videos open at the same time. I can only get rid of them by force quitting each separate frame or by using kill -9 id
for the id of that particular instance of the program. I've read that using shell=True
isn't recommended, but I'm not sure if that would cause this behavior.
How can I kill the Tracking
program after a certain amount of time? I'm extremely new to Python and am not sure how to do this. I was considering doing something like os.system("kill -9 id")
after the sleep()
, but I don't know how to get the id of the program either.