TMD,尝试无数种方法杀死ffmpeg进程,但是发现结束后录制还在继续,把 我的腾讯云服务器全部塞满了,话不多说,主要参考下面这位大佬的代码:https://segmentfault.com/a/1190000015409826
核心思想是在ffmpeg进程中按下“q”,才能够正常结束录制,代码如下:
import time
import os
import datetime
import subprocess
flv_url = "视频地址"
def live_video():
file_path = "D/zhibo"
while True:
file_name = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S.mp4')
live_video = subprocess.Popen('ffmpeg -user-agent "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" -i {} -c copy {}'.format('"%s"' % flv_url, os.path.join(file_path,file_name)),shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE)
time.sleep(10)
live_video.stdin.write('q'.encode("GBK"))
live_video.communicate()
live_video()