-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_streams_simple.py
More file actions
160 lines (132 loc) · 4.59 KB
/
test_streams_simple.py
File metadata and controls
160 lines (132 loc) · 4.59 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
import logging
logging.basicConfig(level=logging.DEBUG)
import re
import tempfile
from os import path
import ffmpegio
from ffmpegio import utils
from ffmpegio.streams import StdFFmpegRunner
url = "tests/assets/testmulti-1m.mp4"
outext = ".mp4"
def test_read_video():
w = 420
h = 360
with StdFFmpegRunner.open_simple_reader(
[(url, {})],
{"map": "0:V:0", "vf": "transpose", "pix_fmt": "gray", "s": (w, h), "r": 30},
show_log=True,
) as f:
F = f.read(10)
assert f.output_rates[0] == 30
assert f.output_shapes[0] == (h, w, 1)
assert F["shape"] == (10, h, w)
assert F["dtype"] == f.output_dtypes[0]
def test_read_write_video():
fs, F = ffmpegio.video.read(url, t=1)
bps = utils.get_samplesize(F["shape"][-3:], F["dtype"])
F0 = {
"buffer": F["buffer"][:bps],
"shape": (1, *F["shape"][1:]),
"dtype": F["dtype"],
}
F1 = {
"buffer": F["buffer"][bps:],
"shape": (F["shape"][0] - 1, *F["shape"][1:]),
"dtype": F["dtype"],
}
with tempfile.TemporaryDirectory() as tmpdirname:
out_url = path.join(tmpdirname, re.sub(r"\..*?$", outext, path.basename(url)))
with StdFFmpegRunner.open_simple_writer([(out_url, {})], {"r": fs}) as f:
f.write(F0)
f.write(F1)
f.wait()
fs, F = ffmpegio.video.read(out_url)
assert len(F["buffer"])
def test_read_audio():
fs, x = ffmpegio.audio.read(url)
bps = utils.get_samplesize(x["shape"][-1:], x["dtype"])
# validate read iterator obtains all the samples
with StdFFmpegRunner.open_simple_reader(
[(url, {})], {"map": "0:a:0"}, show_log=True, blocksize=1024**2
) as f:
# x = f.read(1024)
# assert x['shape'] == (1024, f.ac)
blks = [blk["buffer"] for blk in f]
x1 = b"".join(blks)
assert x["buffer"] == x1
# validate starting
n0 = int(0.5 * fs)
n1 = int(1.2 * fs)
t0 = n0 / fs
t1 = n1 / fs
with StdFFmpegRunner.open_simple_reader(
[(url, {})],
{"map": "0:a:0"},
show_log=True,
blocksize=1024**2,
options={"ss_in": t0, "to_in": t1},
) as f:
blks, shapes = zip(*[(blk["buffer"], blk["shape"][0]) for blk in f])
shape = sum(shapes)
x2 = b"".join(blks)
# # print("# of blks: ", len(blks), x1['shape'])
# for i, xi in enumerate(x2):
# print(i, xi-x[n0 + i])
# assert np.array_equal(xi, x[n0 + i])
assert shape == n1 - n0
assert x["buffer"][n0 * bps : n1 * bps] == x2
def test_read_write_audio():
outext = ".flac"
with StdFFmpegRunner.open_simple_reader([(url, {})], {"map": "0:a:0"}) as f:
F = b"".join((f.read(100)["buffer"], f.read(-1)["buffer"]))
fs = f.output_rates[0]
shape = f.output_shapes[0]
dtype = f.output_dtypes[0]
bps = f.output_itemsizes[0]
out = {"dtype": dtype, "shape": shape}
print(len(F[: 100 * bps]))
with tempfile.TemporaryDirectory() as tmpdirname:
out_url = path.join(tmpdirname, re.sub(r"\..*?$", outext, path.basename(url)))
with StdFFmpegRunner.open_simple_writer(
[(out_url, {})], {"ar": fs}, show_log=True
) as f:
f.write({**out, "buffer": F[: 100 * bps]})
f.write({**out, "buffer": F[100 * bps :]})
f.wait()
assert path.exists(out_url)
def test_write_extra_inputs():
url_aud = "tests/assets/testaudio-1m.mp3"
fs, F = ffmpegio.video.read(url, t=1)
F = {
"buffer": F["buffer"],
"shape": F["shape"],
"dtype": F["dtype"],
}
print(len(F["buffer"]))
with tempfile.TemporaryDirectory() as tmpdirname:
out_url = path.join(tmpdirname, re.sub(r"\..*?$", outext, path.basename(url)))
with StdFFmpegRunner.open_simple_writer(
[(out_url, {})],
{"r": fs},
extra_inputs=[(url_aud, {})],
show_log=True,
options={"map": ["0:v", "1:a"], "loglevel": "debug"},
) as f:
f.write(F)
f.wait()
print(f.readlog())
info = ffmpegio.probe.streams_basic(out_url)
assert len(info) == 2
with StdFFmpegRunner.open_simple_writer(
[(out_url, {})],
{"r": fs},
extra_inputs=[("anoisesrc", {"f": "lavfi"})],
show_log=True,
overwrite=True,
options={"map": ["0:v", "1:a"], "shortest": None},
) as f:
f.write(F)
f.wait()
print(f.readlog())
info = ffmpegio.probe.streams_basic(out_url)
assert len(info) == 2