-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
73 lines (53 loc) · 1.75 KB
/
parser.py
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
import os
import subprocess
import av
import av.datasets
import time
# We want an H.264 stream in the Annex B byte-stream format.
# We haven't exposed bitstream filters yet, so we're gonna use the `ffmpeg` CLI.
h264_path = 'nightsky.h264'
if not os.path.exists(h264_path):
subprocess.check_call([
'ffmpeg',
'-i', av.datasets.curated('pexels/time-lapse-video-of-night-sky-857195.mp4'),
'-vcodec', 'copy',
'-an',
'-bsf:v', 'h264_mp4toannexb',
h264_path,
])
fh = open(h264_path, 'rb')
print ("running h264 decoder on cpu..")
codec = av.CodecContext.create('h264', 'r')
t1 = time.time()
while True:
chunk = fh.read(1 << 16)
packets = codec.parse(chunk)
print("Parsed {} packets from {} bytes:".format(len(packets), len(chunk)))
for packet in packets:
print(' ', packet)
frames = codec.decode(packet)
for frame in frames:
print(' ', frame)
# We wait until the end to bail so that the last empty `buf` flushes
# the parser.
if not chunk:
break
print ("h264 decoder on cpu success in %s!!" %(time.time() - t1))
fh = open(h264_path, 'rb')
print ("running h264 decoder on gpu..")
codec = av.CodecContext.create('h264_cuvid', 'r')
t1 = time.time()
while True:
chunk = fh.read(1 << 16)
packets = codec.parse(chunk)
print("Parsed {} packets from {} bytes:".format(len(packets), len(chunk)))
for packet in packets:
print(' ', packet)
frames = codec.decode(packet)
for frame in frames:
print(' ', frame)
# We wait until the end to bail so that the last empty `buf` flushes
# the parser.
if not chunk:
break
print ("h264 decoder on gpu success in %s!!"%(time.time() - t1))