【问题标题】:How to extract a sequence of frames with ffmpeg and get it with subprocess.Popen如何使用 ffmpeg 提取帧序列并使用 subprocess.Popen 获取
【发布时间】:2019-11-03 09:32:15
【问题描述】:

由于读取单个帧有点慢,我尝试一次提取多个帧。现在我得到了 ffmpeg 不返回列表而是返回一堆字节的问题。 操作系统:win10 蟒蛇版本 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 Surfaces 列表 得到:ValueError:缓冲区长度不等于格式和分辨率大小

【问题讨论】:

  • 解决方案:raw = pipe.stdout.read(resolution[0]*resolution[1]*3) 这计算缓冲区大小

标签: python-3.x ffmpeg extract frames


【解决方案1】:

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

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

【讨论】:

  • 谢谢,有道理。
猜你喜欢
  • 1970-01-01
  • 2011-10-12
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
  • 2016-10-24
  • 1970-01-01
  • 2012-08-06
  • 2020-05-18
相关资源
最近更新 更多