为什么ffmpeg-python的输出与图像形状不匹配?

我使用了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进行转换吗?

shijy07 回答:为什么ffmpeg-python的输出与图像形状不匹配?

要直接制作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')
本文链接:https://www.f2er.com/3133270.html

大家都在问