I want to use python to capture videos. I plan to use ffmpeng.exe to record the video and then end it automatically at a scheduled time. I am a novice and the command is not complicated. I just use subprocess.Popen to start the ffmpeng command to capture the video and save it as mp4. I have tested it under cmd. There is no problem with the command, and the test can also be recorded in python. The problem is that it cannot be ended. . I used subprocess.kill to end the process, but it only ended cmd. ffmpeg started by cmd still continued to work. I don't know how to end the process it generated. .
cmd = '''ffmpeg1.exe -i "rtmp://123.123.123.132/live/tv22 live=1" -acodec libmp3lame -vcodec libx264 -y 3.mkv'''
cc=subprocess.Popen(cmd2,shell=True)
print(cc.pid)
time.sleep(15)
cc.terminate()
You cannot end the process with terminat or kill. You can only end the cmd process generated by subprocess.Popen. The ffmpeg1.exe process generated by cmd execution command cannot be stopped.
If there is a termination method that imitates ctrl c, it will also work. , please help me,
I later tested ping 127.0.0.1, the same cmd can be terminated, but ping.exe cannot be terminated
女神的闺蜜爱上我2017-06-14 10:53:31
In fact, you have already obtained the result yourself. When you kill subprocess.Popen
, it can only kill cmd
, but not ffmpeg1
, so try to kill it directly using the windows
command
kill_command = 'taskkill -f ffmpeg1.exe'
cc=subprocess.Popen(kill_command ,shell=True)
....(自己补充)