Reputation: 6370
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)?
Upvotes: 1
Views: 1316
Reputation: 1
PyTrochVideo uses decord as its video decoding backend, and provides timestamp reading:
from pytorchvideo.data.encoded_video import EncodedVideo
start_sec = 0
end_sec = start_sec + clip_duration
video = EncodedVideo.from_path(video_path)
video_data = video.get_clip(start_sec=start_sec, end_sec=end_sec)
The tutorial is at https://pytorchvideo.org/docs/tutorial_torchhub_inference
Upvotes: 0
Reputation: 6370
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.
Upvotes: 1