如何使用 ffmpeg 提取一系列帧并使用 subprocess.Popen 获取它

How to extract a sequence of frames with ffmpeg and get it with subprocess.Popen

由于读取单帧有点慢,我尝试一次提取多帧。现在我得到的问题是 ffmpeg 不是 return 列表而是一堆字节。 os: win10 python 版本 3.7.4 ffmpeg 从 mp4

中提取帧
        command = [self.FFMPEG_BINARY,
                   '-loglevel', 'fatal',
                   '-ss', str(datetime.timedelta(seconds=frame_index / self._fps)),
                   '-i', os.path.join(self._path, self._filename),
                   '-threads', str(self.THREAD_NUM),
                   '-vf', 'scale=%d:%d' % (self._resolution[0], self._resolution[1]),
                   '-vframes', str(num_frames),
                   '-f', 'image2pipe',
                   '-pix_fmt', 'rgb24',
                   '-vcodec', 'rawvideo', '-']

        pipe = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        tmplist = []
        for _ in range(num_frames):
            output, err = pipe.communicate()
            tmplist.append(output)
            if err: print('error', err); return None;
            pygame.image.frombuffer(output, self._resolution, "RGB")

        return tmplist


预期:pygame 个曲面的列表 得到:ValueError:缓冲区长度不等于格式和分辨率大小

管道不能return列表,只能是流。您需要使用像 yuv4mpegpupe 这样的格式,然后将帧解析出来。或者使用预先计算每个帧的大小并读取该字节数。

例如 rgb24 是宽度乘以高度乘以 3 个字节。