python調用ffmpeg,正確的退出方式


`
from subprocess import Popen

import subprocess

def run():
url = "rtmp://play.xxxx.cn/channel01"
while True:
filename = "ch1_"+str(datetime.datetime.now()).replace(" ", "_").split(".")[0] + ".mp4"
# cmd = "ffmpeg -i rtmp://play.atemelive.cn/cgtn/channel01 -c copy -f mp4 %s" % filename
res = Popen(["ffmpeg","-i",url,"-c","copy","-f","mp4",filename],stdin=subprocess.PIPE,stdout=subprocess.PIPE)
if calc_time():
res.stdin.write('q'.encode("GBK"))
res.communicate()
res.kill()
`
ffmpeg在調用鍵盤的“q”建退出時,會自動添加文件尾,否則強制退出會造成文件不能播放的情況,所以要使用pipe將“q”傳入到子進程中
如果子進程輸出了大量數據到stdout或者stderr的管道,並達到了系統pipe的緩存大小的話,子進程會等待父進程讀取管道,而父進程此時正wait着的話,將會產生傳說中的死鎖,后果是非常嚴重滴。建議使用communicate() 來避免這種情況的發生。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM