[英]Why the output of the ffmpeg-python doesn't match the image shape?
我使用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() 的 output 不是解碼的圖像數組,而是JPEG格式編碼的字節串。 要將圖像恢復為 numpy 數組,只需使用 cv2.imdecode() function。 例如,
im = cv2.imdecode(im, cv2.IMREAD_COLOR)
但是,我不能在我的嵌入式 Linux 系統上使用opencv
。 所以我現在的問題是,我可以直接從ffmpeg-python
獲得 numpy output,而不需要通過 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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.