1

I am trying to read from a POE streaming camera and write to file. I have it working from the command line with the following command:

ffmpeg -y -i rtsp://admin:admin@192.168.1.200 -acodec copy -vcodec copy out.mp4

When I hit Ctrl+C, it exits with:

Exiting normally, received signal 2.

and I am able to view the saved file.

I need to wrap this in Python. From reading online, it appears that using the subprocess library would be best. This is the bare bones code:

import ffmpeg
from subprocess import Popen
import signal
import time


handle = Popen("ffmpeg -y -i rtsp://admin:ambi1234@192.168.1.200 -acodec copy -vcodec copy out.mp4",shell=True)
time.sleep(5)
handle.kill()
# handle.send_signal(signal.SIGINT) # This doesn't work either

The behavior I see is that after 5s, the script exits, but ffmpeg is still working. No matter how many times I send ctrl+C, it continues. Closing the Terminal stops it, but the file is unplayable.

How do I terminate the process cleanly?

trycatch22
  • 307
  • 4
  • 14
  • https://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true/4791612#4791612 – jackl May 14 '21 at 17:48

1 Answers1

0

This is what worked:

import os
os.killpg(os.getpgid(handle.pid), signal.SIGINT)```
trycatch22
  • 307
  • 4
  • 14
  • I had a question.. This is an example of manual termination of the sub-process after a specified timeout period. But what would happen if ffmpeg encountered a connection loss before the timeout out period? would the sub-process terminate automatically or just freeze? – Bhaskar Dey Jul 08 '21 at 05:12
  • Thank you. This restarted my PC – Frankie Drake Apr 22 '22 at 12:45