forked from meta-pytorch/torchcodec
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_video_decode.py
More file actions
219 lines (180 loc) · 6.64 KB
/
test_video_decode.py
File metadata and controls
219 lines (180 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import paddle
paddle.enable_compat(scope={"torchcodec"})
import pytest
import subprocess
from dataclasses import dataclass, fields
from io import BytesIO
from typing import Callable, Mapping, Optional, Union
import os
import httpx
import numpy as np
def ffmpeg_rgb_sum(video_path_or_url: str) -> int:
# Use the local FFmpeg build as the oracle because YUV->RGB conversion is
# architecture- and FFmpeg-build-dependent.
proc = subprocess.Popen(
[
"ffmpeg",
"-v",
"error",
"-vsync",
"0",
"-i",
video_path_or_url,
"-f",
"rawvideo",
"-pix_fmt",
"rgb24",
"-",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
assert proc.stdout is not None
assert proc.stderr is not None
total = 0
while chunk := proc.stdout.read(16 * 1024 * 1024):
total += sum(chunk)
stderr = proc.stderr.read().decode("utf-8", "ignore")
if proc.wait() != 0:
raise RuntimeError(f"ffmpeg failed to decode video: {stderr}")
return total
@dataclass
class VideoMetadata(Mapping):
total_num_frames: int
fps: Optional[float] = None
width: Optional[int] = None
height: Optional[int] = None
duration: Optional[float] = None
video_backend: Optional[str] = None
frames_indices: Optional[list[int]] = None
def __iter__(self):
return (f.name for f in fields(self))
def __len__(self):
return len(fields(self))
def __getitem__(self, item):
return getattr(self, item)
def __setitem__(self, key, value):
return setattr(self, key, value)
@property
def timestamps(self) -> list[float]:
"Timestamps of the sampled frames in seconds."
if self.fps is None or self.frames_indices is None:
raise ValueError("Cannot infer video `timestamps` when `fps` or `frames_indices` is None.")
return [frame_idx / self.fps for frame_idx in self.frames_indices]
def update(self, dictionary):
for key, value in dictionary.items():
if hasattr(self, key):
setattr(self, key, value)
def default_sample_indices_fn(metadata: VideoMetadata, num_frames=None, fps=None, **kwargs):
total_num_frames = metadata.total_num_frames
video_fps = metadata.fps
if num_frames is None and fps is not None:
num_frames = int(total_num_frames / video_fps * fps)
if num_frames > total_num_frames:
raise ValueError(
f"When loading the video with fps={fps}, we computed num_frames={num_frames} "
f"which exceeds total_num_frames={total_num_frames}. Check fps or video metadata."
)
if num_frames is not None:
indices = np.arange(0, total_num_frames, total_num_frames / num_frames, dtype=int)
else:
indices = np.arange(0, total_num_frames, dtype=int)
return indices
def read_video_decord(
video_path: Union["URL", "Path"],
sample_indices_fn: Callable,
**kwargs,
):
from decord import VideoReader, cpu
vr = VideoReader(uri=video_path, ctx=cpu(0)) # decord has problems with gpu
video_fps = vr.get_avg_fps()
total_num_frames = len(vr)
duration = total_num_frames / video_fps if video_fps else 0
metadata = VideoMetadata(
total_num_frames=int(total_num_frames),
fps=float(video_fps),
duration=float(duration),
video_backend="decord",
)
indices = sample_indices_fn(metadata=metadata, **kwargs)
video = vr.get_batch(indices).asnumpy()
metadata.update(
{
"frames_indices": indices,
"height": video.shape[1],
"width": video.shape[2],
}
)
return video, metadata
def read_video_torchcodec(
video_path: Union["URL", "Path"],
sample_indices_fn: Callable,
**kwargs,
):
from torchcodec.decoders import VideoDecoder # import torchcodec
decoder = VideoDecoder(
video_path,
seek_mode="exact",
num_ffmpeg_threads=0,
)
metadata = VideoMetadata(
total_num_frames=decoder.metadata.num_frames,
fps=decoder.metadata.average_fps,
duration=decoder.metadata.duration_seconds,
video_backend="torchcodec",
height=decoder.metadata.height,
width=decoder.metadata.width,
)
indices = sample_indices_fn(metadata=metadata, **kwargs)
video = decoder.get_frames_at(indices=indices).data
video = video.contiguous()
metadata.frames_indices = indices
return video, metadata
VIDEO_DECODERS = {
"decord": read_video_decord,
"torchcodec": read_video_torchcodec,
}
def load_video(
video,
num_frames: Optional[int] = None,
fps: Optional[Union[int, float]] = None,
backend: str = "decord",
sample_indices_fn: Optional[Callable] = None,
**kwargs,
) -> np.ndarray:
if fps is not None and num_frames is not None and sample_indices_fn is None:
raise ValueError(
"`num_frames`, `fps`, and `sample_indices_fn` are mutually exclusive arguments, please use only one!"
)
# If user didn't pass a sampling function, create one on the fly with default logic
if sample_indices_fn is None:
def sample_indices_fn_func(metadata, **fn_kwargs):
return default_sample_indices_fn(metadata, num_frames=num_frames, fps=fps, **fn_kwargs)
sample_indices_fn = sample_indices_fn_func
# Early exit if provided an array or `PIL` frames
if not isinstance(video, str):
metadata = [None] * len(video)
return video, metadata
if video.startswith("http://") or video.startswith("https://"):
file_obj = BytesIO(httpx.get(video, follow_redirects=True).content)
elif os.path.isfile(video):
file_obj = video
else:
raise TypeError("Incorrect format used for video. Should be an url linking to an video or a local path.")
video_decoder = VIDEO_DECODERS[backend]
video, metadata = video_decoder(file_obj, sample_indices_fn, **kwargs)
return video, metadata
def test_video_decode():
video_path = os.getenv(
"PADDLECODEC_TEST_VIDEO",
"https://paddlenlp.bj.bcebos.com/datasets/paddlemix/demo_video/example_video.mp4",
)
video, metadata = load_video(video_path, backend="torchcodec")
assert video.to(paddle.int64).sum().item() == ffmpeg_rgb_sum(video_path)
assert metadata.total_num_frames == 263
assert metadata.fps == pytest.approx(29.99418249715141)
assert metadata.width == 1920
assert metadata.height == 1080
assert metadata.duration == pytest.approx(8.768367)
for i, idx in enumerate(metadata.frames_indices):
assert idx == i