-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_streams_piped.py
More file actions
221 lines (170 loc) · 6.46 KB
/
test_streams_piped.py
File metadata and controls
221 lines (170 loc) · 6.46 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
import logging
import numpy as np
import ffmpegio as ff
from ffmpegio import streams
logging.basicConfig(level=logging.DEBUG)
mult_url = "tests/assets/testmulti-1m.mp4"
video_url = "tests/assets/testvideo-1m.mp4"
audio_url = "tests/assets/testaudio-1m.mp3"
outext = ".mp4"
def test_MediaReader():
with streams.PipedFFmpegRunner.open_media_reader(
[(mult_url, {})], None, options={"t_in": 1}, squeeze=False
) as reader:
nframes = [0] * reader.num_output_streams
for i, data in enumerate(reader):
nframes = [n0 + v["shape"][0] for n0, v in zip(nframes, data)]
assert nframes == [30, 44100, 25, 44100]
def test_MediaWriter_audio():
ff.use("read_numpy")
rates, data = ff.media.read(audio_url, t=1, ar=8000, sample_fmt="s16")
with streams.PipedFFmpegRunner.open_media_encoder(
[{"ar": rates["0:a:0"]}],
[{"f": "matroska"}],
show_log=True,
) as writer:
for i, frame in enumerate(data.values()):
writer.write(frame, i)
# read the encoded bytes if any available
b = writer.read_encoded_nowait(0)
# close the input and wait for FFmpeg to finish encoding and terminate
writer.wait(timeout=10)
# read the rest
b = writer.read_encoded(0)
def test_MediaWriter():
ff.use("read_numpy")
rates, data = ff.media.read(mult_url, t=1)
stream_types = [spec.split(":", 2)[1] for spec in data]
rate_opt_name = {"a": "ar", "v": "r"}
stream_opts = [
{rate_opt_name[mtype]: r} for mtype, r in zip(stream_types, rates.values())
]
with streams.PipedFFmpegRunner.open_media_encoder(
stream_opts,
[{"f": "matroska", "map": range(len(stream_types))}],
show_log=True,
) as writer:
# write full audio streams
video_frames = {}
for i, (mtype, frame) in enumerate(zip(stream_types, data.values())):
if mtype == "a":
writer.write(frame, i)
else:
video_frames[i] = frame.shape[0]
# write video stream one frame at a time
frame_count = {k: 0 for k in video_frames}
while any(
n < nall for n, nall in zip(frame_count.values(), video_frames.values())
):
for i, (mtype, frame) in enumerate(zip(stream_types, data.values())):
if i in frame_count:
j = frame_count[i]
print(j)
try:
writer.write(frame[j], i)
except IndexError:
pass
else:
b = writer.read_encoded_nowait(0)
frame_count[i] = j + 1
writer.wait(10)
b = writer.read_encoded(-1)
assert isinstance(b, bytes) and len(b) > 0
def test_SimpleMediaFilter():
ff.use("read_numpy")
fs, x = ff.audio.read("tests/assets/testaudio-1m.mp3", to=0.1)
nin = 1024
nblocks = len(x) // nin
X = x[: nin * nblocks, ...].reshape(nblocks, nin, -1)
with ff.streams.SISOFFmpegFilter.create_and_open(
{"ar": fs},
{"map": "[out]"},
options={"filter_complex": "[0:a:0]showcqt=s=vga[out]"},
show_log=True,
squeeze=False,
) as f:
# write the first frame (so the output rate is resolved)
f.write(X[0])
dt = nin / f.rate_in
ntotal = int(nin * nblocks * f.rate / f.rate_in) # total # of frames
cumnout = np.astype(np.arange(1, nblocks) * dt * f.rate, int)
nread = 0
for i, (n, Xn) in enumerate(zip(cumnout, X[1:])):
assert bool(f)
ntry = n - nread
if ntry > 0:
out = f.read_nowait(n - nread)
nread += out.shape[0]
print(f"[{i:2}] expects {ntry} new frames, {out.shape[0]} frames read")
f.write(Xn, last=i == nblocks - 2)
ntry = ntotal - nread
if ntry > 0:
print(f"[last] reading the remaining {ntry} frames")
out = f.read(ntry)
nread += out.shape[0]
print(f"[last] final read obtained {out.shape[0]} frames")
assert nread == ntotal
def test_MediaFilter():
ff.use("read_bytes")
fs, x = ff.audio.read("tests/assets/testaudio-1m.mp3", to=1)
fps, F = ff.video.read("tests/assets/testvideo-1m.mp4", to=1)
print(f"video: {len(F['buffer'])} bytes | audio: {len(x['buffer'])} bytes")
with ff.streams.PipedFFmpegRunner.open_media_filter(
[{"r": fps}, {"r": fps}, {"ar": fs}, {"ar": fs}],
output_streams=["[out0]", {"map": "[out1]"}],
options={"filter_complex": ["[0:V:0][1:V:0]vstack", "[2:a:0][3:a:0]amerge"]},
show_log=True,
# loglevel="debug",
# queuesize=4,
) as f:
# f.write([F, F])
for i, frame in enumerate([F, F, x, x]):
f.write(frame, i, last=True)
# sleep(1)
assert ["out0", "out1"] == f.output_labels
assert f.num_output_streams == 2
frames_per_read = f.output_frames()
nnext = list(frames_per_read)
for i in range(F["shape"][0]):
for st in range(2):
n = int(nnext[st])
Fout = f.read(n, st)
print(Fout["shape"], n)
# assert Fout["shape"][0] == n
nnext[st] = nnext[st] - n + frames_per_read[st]
# just in case
f.wait(1)
def test_MediaTranscoder():
url = "tests/assets/sample.mp4"
data = b""
# 1. transcode from a file to pipe
with streams.PipedFFmpegRunner.open_media_transcoder(
[],
[{"f": "matroska", "to": 1}],
extra_inputs=[(url, {})],
show_log=True,
# loglevel="debug",
) as f:
while f:
b = f.read_encoded_nowait(-1)
data += b
b = f.read_encoded_nowait(-1)
data += b
assert len(data) > 0
print(f"FIRST TRANCODING YIELDED {len(data)} bytes")
with streams.PipedFFmpegRunner.open_media_transcoder(
[{}],
[{"f": "flac", "vn": None}, {"f": "matroska", "codec": "copy"}],
show_log=True,
# loglevel="debug",
) as f:
f.write_encoded(data, last=True)
out = [b"", b""]
while f:
for st in range(2):
out[st] += f.read_encoded_nowait(-1, stream=st)
assert len(out[0]) > 0
assert len(out[1]) > 0
print(
f"SECOND TRANCODING YIELDED {len(out[0])} bytes for flac and {len(out[1])} bytes for matroska"
)