-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.py
More file actions
289 lines (214 loc) · 8.61 KB
/
decode.py
File metadata and controls
289 lines (214 loc) · 8.61 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import ffmpeg
import numpy as np
import time
import re
def detect_video_format(video_path):
probe = ffmpeg.probe(video_path)
video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
if not video_stream:
raise ValueError("Не удалось найти видеопоток в файле")
width = int(video_stream['width'])
height = int(video_stream['height'])
pix_fmt = video_stream.get('pix_fmt', 'yuv420p')
if 'avg_frame_rate' in video_stream:
fps_str = video_stream['avg_frame_rate']
if '/' in fps_str:
num, den = map(int, fps_str.split('/'))
fps = num / den if den != 0 else 0
else:
fps = float(fps_str)
else:
fps = 0
return {
'width': width,
'height': height,
'pix_fmt': pix_fmt,
'fps': fps
}
def decode_yuv420p(video_path,hwaccel_t,decoder_t ,only_keyframes=False):
info = detect_video_format(video_path)
width, height = info['width'], info['height']
input_args = {
'hwaccel': hwaccel_t,
'c:v': decoder_t
}
if only_keyframes:
input_args['skip_frame'] = 'nokey'
output_args = {
'format': 'null'
}
stream = ffmpeg.input(video_path, **input_args)
stream = ffmpeg.output(stream, 'pipe:', **output_args)
process = ffmpeg.run_async(stream, pipe_stdout=True, pipe_stderr=True)
y_size = width * height
uv_size = (width // 2) * (height // 2)
frame_size = y_size + uv_size * 2
frame_count = 0
try:
while True:
in_bytes = process.stdout.read(frame_size)
if not in_bytes or len(in_bytes) < frame_size:
break
y_data = in_bytes[:y_size]
u_data = in_bytes[y_size:y_size + uv_size]
v_data = in_bytes[y_size + uv_size:]
y_plane = np.frombuffer(y_data, np.uint8).reshape((height, width))
u_plane = np.frombuffer(u_data, np.uint8).reshape((height // 2, width // 2))
v_plane = np.frombuffer(v_data, np.uint8).reshape((height // 2, width // 2))
yuv_frame = {
'y': y_plane,
'u': u_plane,
'v': v_plane,
'format': 'yuv420p'
}
frame_count += 1
yield yuv_frame
finally:
_finish_process(process, frame_count)
def decode_rgb24(video_path,hwaccel_t,decoder_t, only_keyframes=False):
info = detect_video_format(video_path)
width, height = info['width'], info['height']
input_args = {
'hwaccel': hwaccel_t,
'c:v': decoder_t
}
if only_keyframes:
input_args['skip_frame'] = 'nokey'
output_args = {
'format': 'rawvideo',
'pix_fmt': 'rgb24'
}
stream = ffmpeg.input(video_path, **input_args)
stream = ffmpeg.output(stream, 'pipe:', **output_args)
process = ffmpeg.run_async(stream, pipe_stdout=True, pipe_stderr=True)
bytes_per_pixel = 3
frame_size = width * height * bytes_per_pixel
frame_count = 0
try:
while True:
in_bytes = process.stdout.read(frame_size)
if not in_bytes or len(in_bytes) < frame_size:
break
frame = np.frombuffer(in_bytes, np.uint8).reshape([height, width, 3])
frame_count += 1
yield {'data': frame, 'format': 'rgb24'}
finally:
_finish_process(process, frame_count)
def decode_rgba(video_path,hwaccel_t,decoder_t, only_keyframes=False):
info = detect_video_format(video_path)
width, height = info['width'], info['height']
input_args = {
'hwaccel': hwaccel_t,
'c:v': decoder_t
}
if only_keyframes:
input_args['skip_frame'] = 'nokey'
output_args = {
'format': 'rawvideo',
'pix_fmt': 'rgba'
}
stream = ffmpeg.input(video_path, **input_args)
stream = ffmpeg.output(stream, 'pipe:', **output_args)
process = ffmpeg.run_async(stream, pipe_stdout=True, pipe_stderr=True)
bytes_per_pixel = 4
frame_size = width * height * bytes_per_pixel
frame_count = 0
try:
while True:
in_bytes = process.stdout.read(frame_size)
if not in_bytes or len(in_bytes) < frame_size:
break
frame = np.frombuffer(in_bytes, np.uint8).reshape([height, width, 4])
frame_count += 1
yield {'data': frame, 'format': 'rgba'}
finally:
_finish_process(process, frame_count)
def decode_gray(video_path,hwaccel_t,decoder_t, only_keyframes=False):
info = detect_video_format(video_path)
width, height = info['width'], info['height']
input_args = {
'hwaccel': hwaccel_t,
'c:v': decoder_t
}
if only_keyframes:
input_args['skip_frame'] = 'nokey'
output_args = {
'format': 'rawvideo',
'pix_fmt': 'gray'
}
stream = ffmpeg.input(video_path, **input_args)
stream = ffmpeg.output(stream, 'pipe:', **output_args)
process = ffmpeg.run_async(stream, pipe_stdout=True, pipe_stderr=True)
bytes_per_pixel = 1
frame_size = width * height * bytes_per_pixel
frame_count = 0
try:
while True:
in_bytes = process.stdout.read(frame_size)
if not in_bytes or len(in_bytes) < frame_size:
break
frame = np.frombuffer(in_bytes, np.uint8).reshape([height, width])
frame_count += 1
yield {'data': frame, 'format': 'gray'}
finally:
_finish_process(process, frame_count)
def _finish_process(process, frame_count):
t_start = time.time()
process.stdout.close()
stderr = process.stderr.read()
process.wait()
duration_ms = (time.time() - t_start) * 1000
stderr_output = stderr.decode('utf-8', errors='ignore')
fps = 0
speed = None
progress_lines = []
for line in stderr_output.split('\n'):
if "frame=" in line and "fps=" in line:
progress_lines.append(line)
if progress_lines:
last_line = progress_lines[-1]
all_fps = re.findall(r'fps=([\d.]+)', last_line)
all_speeds = re.findall(r'speed=\s*([\d.]+)x', last_line)
fps = float(all_fps[-1]) if all_fps else None
speed = float(all_speeds[-1]) if all_speeds else None
print(f"Processed {frame_count} frames in {duration_ms:.2f}ms")
print(f"FPS: {fps}, Speed: {speed}x")
def decode_video(video_path,hwaccel_t,decoder_t, target_format=None, only_keyframes=False):
info = detect_video_format(video_path)
if target_format is None:
native_format = info['pix_fmt']
if native_format == 'yuv420p':
return decode_yuv420p(video_path,hwaccel_t,decoder_t, only_keyframes)
elif native_format in ['rgb24', 'bgr24']:
return decode_rgb24(video_path,hwaccel_t,decoder_t, only_keyframes)
elif native_format in ['rgba', 'bgra']:
return decode_rgba(video_path,hwaccel_t,decoder_t, only_keyframes)
elif native_format == 'gray':
return decode_gray(video_path,hwaccel_t,decoder_t, only_keyframes)
else:
return decode_rgb24(video_path,hwaccel_t,decoder_t, only_keyframes)
else:
if target_format == 'yuv420p':
return decode_yuv420p(video_path,hwaccel_t,decoder_t, only_keyframes)
elif target_format == 'rgb24':
return decode_rgb24(video_path,hwaccel_t,decoder_t, only_keyframes)
elif target_format == 'rgba':
return decode_rgba(video_path,hwaccel_t,decoder_t, only_keyframes)
elif target_format == 'gray':
return decode_gray(video_path,hwaccel_t,decoder_t, only_keyframes)
else:
raise ValueError(f"Неподдерживаемый формат: {target_format}")
def main():
video_path = "/home/mehroj/Coding/hardware-decoding/video/input.mp4"
format_type = ""
print("CUDA декодирование")
for frame in decode_video(video_path,'cuda','h264_cuvid'):
format_type = frame.get('format')
print("\nIntel GPU декодирование")
for frame in decode_video(video_path,'vaapi','h264'):
format_type = frame.get('format')
print("\nCPU декодирование")
for frame in decode_video(video_path,'none','h264'):
format_type = frame.get('format')
print(f"format_type: {format_type}")
main()