Skip to main content
 首页 » 编程设计

python之将视频文件作为字节流式传输到标准输入

2025年01月19日10bonelee

我正在创建一个进程,其中 python 打开一个视频文件,并将其流式传输到 ffmpeg 命令的标准输入中。我想我的想法是对的,但是打开文件的方式不适用于 stdin。到目前为止,这是我的代码:

def create_pipe(): 
 
    return Popen([    'ffmpeg', 
                      '-loglevel', 'panic', 
                      '-s', '1920x1080', 
                      '-pix_fmt', 'yuvj420p', 
                      '-y', 
                      '-f', 'image2pipe', 
                      '-vcodec', 'mjpeg', 
                      '-r', self.fps, 
                      '-i', '-', 
                      '-r', self.fps, 
                      '-s', '1920x1080', 
                      '-f', 'mov', 
                      '-vcodec', 'prores', 
                      '-profile:v', '3', 
                      '-aspect', '16:9', '-y', 
                      'output_file_name' + '.mov'], stdin=PIPE) 
 
 
 
in_pipe = create_pipe() 
with open("~/Desktop/IMG_1973.mov", "rb") as f: 
    in_pipe.communicate(input=f) 

这会产生错误:

TypeError:需要一个类似字节的对象,而不是“_io.BufferedReader”

将视频文件打开/流式传输到此管道的正确方法是什么?我还需要成为一个流,而不是将整个事情读入内存。

附言。请忽略我可以在 ffmpeg 中本地打开文件......我正在创建一个包装器,如果我可以控制输入就更好了。

请您参考如下方法:

首先确保输入格式可以通过管道传输。对于 ISO BMFF,moov 原子必须位于文件的开头才能工作。

如果它是可管道的,那么使用 generator读取文件并将其通过管道传递给子进程:

def read_bytes(input_file, read_size=8192): 
    """ read 'read_size' bytes at once """ 
    while True: 
        bytes_ = input_file.read(read_size) 
        if not bytes_: 
            break 
        yield bytes_ 
 
 
def main(): 
    in_pipe = create_pipe() 
 
    with open("in.mov", "rb") as f: 
        for bytes_ in read_bytes(f): 
            in_pipe.stdin.write(bytes_) 
 
    in_pipe.stdin.close() 
    in_pipe.wait()