在python中输入ffmpeg的输入和输出 [英] Piping input AND output of ffmpeg in python

查看:831
本文介绍了在python中输入ffmpeg的输入和输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ffmpeg从我传送到ffmpeg的base64编码图像列表中创建视频.

I'm using ffmpeg to create a video, from a list of base64 encoded images that I pipe into ffmpeg.

输出到文件(使用下面的附加代码)可以完美地工作,但是我想要实现的是将输出改为Python变量-意味着管道输入和管道输出,但我似乎无法得到它工作

Outputting to a file (using the attached code below) works perfectly, but what I would like to achieve is to get the output to a Python variable instead - meaning piping input and piping output but I can't seem to get it to work

我当前的代码:

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正常工作,但我想避免保存到本地.

This results in a working mp4, but I would like to avoid saving to local.

在将output更改为'-''pipe:1'并添加stdout=sp.PIPE的情况下运行相同的代码会导致错误

Running the same code with changing output to '-' or 'pipe:1' and adding stdout=sp.PIPE results in an error

[NULL @ 0x2236000]无法为'pipe:'找到合适的输出格式

[NULL @ 0x2236000] Unable to find a suitable output format for 'pipe:'

推荐答案

FFmpeg通过检查输出扩展名猜测输出多路复用器.对于管道,该管道不存在,因此必须明确设置.在输出之前添加'-f', 'image2pipe'.

FFmpeg guesses the output muxer by checking the output extension. With a pipe, that doesn't exist, so it has to be expressly set. Add '-f', 'image2pipe' just before output.

这篇关于在python中输入ffmpeg的输入和输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆