Decord allows to seek a video frame from a file using indices, like:
video_reader = decord.VideoReader(video_path)
frames = video_reader.get_batch(indices)
How can I do the same if I have timestamps (with the unit second)?
You can obtain the timestamp of each frame (averaging its start and end times), then find the closest one:
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))
Note the VideoReader C object (not the Python one) is loaded already with all the frame timestamps from the initialization. And we take advantage of the fact that they should be sorted.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.