[英]How to use Python decord library to seek by timestamp instead of by frame index?
Decord允許使用索引從文件中查找視頻幀,例如:
video_reader = decord.VideoReader(video_path)
frames = video_reader.get_batch(indices)
如果我有時間戳(以秒為單位),我該怎么做?
您可以獲得每一幀的時間戳(平均其開始和結束時間),然后找到最接近的:
from typing import Sequence, Union
import decord
import numpy as np
def time_to_indices(video_reader: decord.VideoReader, time: Union[float, Sequence[float]]) -> np.ndarray:
times = video_reader.get_frame_timestamp(range(len(video_reader))).mean(-1)
indices = np.searchsorted(times, time)
# Use `np.bitwise_or` so it works both with scalars and numpy arrays.
return np.where(np.bitwise_or(indices == 0, times[indices] - time <= time - times[indices - 1]), indices,
indices - 1)
frames = video_reader.get_batch(time_to_indices(indices))
注意 VideoReader C 對象(不是 Python 對象)已經加載了初始化時的所有幀時間戳。 我們利用了它們應該被排序的事實。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.