[英]Piping input AND output of ffmpeg in python
我正在使用ffmpeg
從我輸入到ffmpeg
的base64編碼圖像列表中創建視頻。
輸出到文件(使用下面的附加代碼)可以完美地工作,但是我想要實現的是將輸出改為Python變量-意味着管道輸入和管道輸出,但我似乎無法使其正常工作
我當前的代碼:
output = os.path.join(screenshots_dir, 'video1.mp4')
cmd_out = ['ffmpeg',
'-y', # (optional) overwrite output file if it exists
'-f', 'image2pipe',
'-vcodec', 'png',
'-r', str(fps), # frames per second
'-i', '-', # The input comes from a pipe
'-vcodec', 'png',
'-qscale', '0',
output]
pipe = sp.Popen(cmd_out, stdin=sp.PIPE)
for screenshot in screenshot_list:
im = Image.open(BytesIO(base64.b64decode(screenshot)))
im.save(pipe.stdin, 'PNG')
pipe.stdin.close()
pipe.wait()
這會導致mp4正常工作,但我想避免保存到本地。
運行相同的代碼並將output
更改為'-'
或'pipe:1'
並添加stdout=sp.PIPE
導致錯誤
[NULL @ 0x2236000]無法為'pipe:'找到合適的輸出格式
FFmpeg通過檢查輸出擴展名猜測輸出復用器。 對於管道,該管道不存在,因此必須明確設置。 在輸出之前添加'-f', 'image2pipe'
。
嘗試在sp.Popen
之后添加以下命令:
output, _ = pipe.communicate()
我認為您需要與打開的流程進行溝通。 之后,您可以打印它以確保它起作用:
print(_)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.