我使用ffmpeg-python
模块将视频转换为图像。具体来说,我使用了ffmpeg-python
官方git repo提供的代码,如下
out, _ = (
ffmpeg
.input(in_filename)
.filter('select', 'gte(n,{})'.format(frame_num))
.output('pipe:', vframes=1, format='image2', vcodec='mjpeg')
.run(capture_stdout=True)
)
im = np.frombuffer(out, 'uint8')
print(im.shape[0]/3/1080)
# 924.907098765432
原始视频的大小为(1920, 1080),pix_fmt'yuv420p',但上述代码的输出不是1920。
我自己发现ffmpeg.run()的输出不是解码后的图像数组,而是JPEG格式编码的字节字符串。要将图像恢复到 numpy 数组中,只需使用 cv2.imdecode() 函数即可。例如,
im = cv2.imdecode(im, cv2.IMREAD_COLOR)
但是,我无法在嵌入式 Linux 系统上使用 opencv
。所以我现在的问题是,我可以直接从 ffmpeg-python 获取 numpy 输出,而不需要通过 opencv 进行转换吗?
最佳答案
要使ffmpeg-python
直接输出原始图像数组,请使用以下命令:
out, _ = (
ffmpeg
.input(in_filename)
.filter('select', 'gte(n,{})'.format(frame_num))
.output('pipe:', vframes=1, format='rawvideo', pix_fmt='rgb24')
.run(capture_stdout=True)
)
im = np.frombuffer(out, 'uint8')
关于python - 为什么 ffmpeg-python 的输出与图像形状不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58778321/