簡體   English   中英

使用 PyAV 將視頻直接讀入 Numpy(無迭代)

[英]Reading a video directly into Numpy with PyAV (no iterations)

是否可以使用 PyAV 將視頻直接讀入 3D Numpy? 目前,我正在遍歷每一幀:

i = 0
container = av.open('myvideo.avi')
for frame in container.decode(video=0):
    if i == 0: V = np.array(frame.to_ndarray(format='gray'))
    else: V = np.dstack((V, np.array(frame.to_ndarray(format='gray'))))
    i += 1

第一幀定義了一個2D Numpy數組(i=0); 每個后續幀 (i>0) 使用np.dstack堆疊到第一個數組上。 理想情況下,我想一次將整個視頻讀入灰度幀的 3D Numpy 數組。

我找不到使用 PyAV 的解決方案,而是使用ffmpeg-python

ffmpeg-pythonFFmpeg的 Pythonic 綁定,如PyAV

該代碼一次性將整個視頻讀入灰度幀的 3D Numpy 數組。

該解決方案執行以下步驟:

  • 創建輸入視頻文件(用於測試)。
  • 使用“探針”獲取視頻文件的分辨率。
  • 將視頻流式傳輸到字節數組中。
  • 將字節數組重塑為nx height x width numpy 數組。
  • 顯示第一幀(用於測試)。

這是代碼(請閱讀評論):

import ffmpeg
import numpy as np
from PIL import Image

in_filename = 'in.avi'

"""Build synthetic video, for testing begins:"""
# ffmpeg -y -r 10 -f lavfi -i testsrc=size=160x120:rate=1 -c:v libx264 -t 5 in.mp4
width, height = 160, 120

(
    ffmpeg
    .input('testsrc=size={}x{}:rate=1'.format(width, height), r=10, f='lavfi')
    .output(in_filename, vcodec='libx264', t=5)
    .overwrite_output()
    .run()
)
"""Build synthetic video ends"""


# Use ffprobe to get video frames resolution
p = ffmpeg.probe(in_filename, select_streams='v');
width = p['streams'][0]['width']
height = p['streams'][0]['height']

# https://github.com/kkroening/ffmpeg-python/blob/master/examples/README.md
# Stream the entire video as one large array of bytes
in_bytes, _ = (
    ffmpeg
    .input(in_filename)
    .video # Video only (no audio).
    .output('pipe:', format='rawvideo', pix_fmt='gray')  # Set the output format to raw video in 8 bit grayscale
    .run(capture_stdout=True)
)

n_frames = len(in_bytes) // (height*width)  # Compute the number of frames.
frames = np.frombuffer(in_bytes, np.uint8).reshape(n_frames, height, width) # Reshape buffer to array of n_frames frames (shape of each frame is (height, width)).

im = Image.fromarray(frames[0, :, :])  # Convert first frame to image object
im.show()  # Display the image

輸出:
在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM