FFMPEG + Python - 在ffmpeg端跳过不需要的帧。

问题描述 投票:0回答:1

我想处理一个实时的hlsv流,从中提取每100帧,然后用openCV处理。

目前我的代码看起来像这样。

pipe = subprocess.Popen([FFMPEG_BIN, "-i", src, 
                    "-loglevel", "quiet",
                    "-an", 
                    "-f", "image2pipe",
                    "-pix_fmt", "bgr24",
                    "-vcodec", "rawvideo", "-"],
                    stdin=subprocess.PIPE, 
                    stdout=subprocess.PIPE, 
                    stderr=subprocess.DEVNULL)

while True:
    raw_image = pipe.stdout.read(WIDTH * HEIGHT * 3)
    if frame_counter > 100:
        frame_counter = 0
        process_frame(raw_image)
    frame_counter += 1

读取所有的帧并转储99%的帧似乎效率很低 而且导致了管道缓冲区的问题(至少我怀疑是这样)。

有没有可能跳过FFMPEG中的帧,让每100帧都进入stdout?

python ffmpeg subprocess
1个回答
0
投票

使用选择过滤器来保留第100帧。

pipe = subprocess.Popen([FFMPEG_BIN, "-i", src, 
                    "-loglevel", "quiet",
                    "-vf", "select=not(mod(n\,100))",
                    "-vsync", "0",
                    "-an", 
                    "-f", "image2pipe",
                    "-pix_fmt", "bgr24",
                    "-vcodec", "rawvideo", "-"],
                    stdin=subprocess.PIPE, 
                    stdout=subprocess.PIPE, 
                    stderr=subprocess.DEVNULL)
© www.soinside.com 2019 - 2024. All rights reserved.