-
Notifications
You must be signed in to change notification settings - Fork 448
/
Copy pathanalyze.py
401 lines (317 loc) · 12.4 KB
/
analyze.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
from __future__ import annotations
import os
import re
from dataclasses import dataclass
from fractions import Fraction
from hashlib import sha1
from math import ceil
from tempfile import gettempdir
from typing import TYPE_CHECKING
import av
import numpy as np
from av.audio.fifo import AudioFifo
from av.subtitles.subtitle import AssSubtitle
from auto_editor import __version__
if TYPE_CHECKING:
from collections.abc import Iterator, Sequence
from fractions import Fraction
from numpy.typing import NDArray
from auto_editor.ffwrapper import FileInfo
from auto_editor.utils.bar import Bar
from auto_editor.utils.log import Log
__all__ = ("LevelError", "initLevels", "iter_audio", "iter_motion")
class LevelError(Exception):
pass
def mut_remove_small(
arr: NDArray[np.bool_], lim: int, replace: int, with_: int
) -> None:
start_p = 0
active = False
for j, item in enumerate(arr):
if item == replace:
if not active:
start_p = j
active = True
if j == len(arr) - 1 and j - start_p < lim:
arr[start_p:] = with_
elif active:
if j - start_p < lim:
arr[start_p:j] = with_
active = False
def mut_remove_large(
arr: NDArray[np.bool_], lim: int, replace: int, with_: int
) -> None:
start_p = 0
active = False
for j, item in enumerate(arr):
if item == replace:
if not active:
start_p = j
active = True
if j == len(arr) - 1 and j - start_p >= lim:
arr[start_p:] = with_
elif active:
if j - start_p > lim:
arr[start_p:j] = with_
active = False
def iter_audio(audio_stream: av.AudioStream, tb: Fraction) -> Iterator[np.float32]:
fifo = AudioFifo()
sr = audio_stream.rate
exact_size = (1 / tb) * sr
accumulated_error = Fraction(0)
# Resample so that audio data is between [-1, 1]
resampler = av.AudioResampler(av.AudioFormat("flt"), audio_stream.layout, sr)
container = audio_stream.container
assert isinstance(container, av.container.InputContainer)
for frame in container.decode(audio_stream):
frame.pts = None # Skip time checks
for reframe in resampler.resample(frame):
fifo.write(reframe)
while fifo.samples >= ceil(exact_size):
size_with_error = exact_size + accumulated_error
current_size = round(size_with_error)
accumulated_error = size_with_error - current_size
audio_chunk = fifo.read(current_size)
assert audio_chunk is not None
arr = audio_chunk.to_ndarray().flatten()
yield np.max(np.abs(arr))
def iter_motion(
video: av.VideoStream, tb: Fraction, blur: int, width: int
) -> Iterator[np.float32]:
video.thread_type = "AUTO"
prev_frame = None
current_frame = None
total_pixels = None
index = 0
prev_index = -1
graph = av.filter.Graph()
graph.link_nodes(
graph.add_buffer(template=video),
graph.add("scale", f"{width}:-1"),
graph.add("format", "gray"),
graph.add("gblur", f"sigma={blur}"),
graph.add("buffersink"),
).configure()
container = video.container
assert isinstance(container, av.container.InputContainer)
for unframe in container.decode(video):
if unframe.pts is None:
continue
graph.push(unframe)
frame = graph.vpull()
assert frame.time is not None
index = round(frame.time * tb)
if total_pixels is None:
total_pixels = frame.width * frame.height
current_frame = frame.to_ndarray()
if prev_frame is None:
value = np.float32(0.0)
else:
# Use `int16` to avoid underflow with `uint8` datatype
diff = np.abs(prev_frame.astype(np.int16) - current_frame.astype(np.int16))
value = np.float32(np.count_nonzero(diff) / total_pixels)
for _ in range(index - prev_index):
yield value
prev_frame = current_frame
prev_index = index
@dataclass(slots=True)
class Levels:
container: av.container.InputContainer
name: str
mod_time: int
tb: Fraction
bar: Bar
no_cache: bool
log: Log
@property
def media_length(self) -> int:
container = self.container
if container.streams.audio:
if (arr := self.read_cache("audio", (0,))) is not None:
return len(arr)
audio_stream = container.streams.audio[0]
result = sum(1 for _ in iter_audio(audio_stream, self.tb))
container.seek(0)
self.log.debug(f"Audio Length: {result}")
return result
# If there's no audio, get length in video metadata.
if not container.streams.video:
self.log.error("Could not get media duration")
video = container.streams.video[0]
if video.duration is None or video.time_base is None:
dur = 0
else:
dur = int(video.duration * video.time_base * self.tb)
self.log.debug(f"Video duration: {dur}")
container.seek(0)
return dur
def obj_tag(self, kind: str, obj: Sequence[object]) -> str:
mod_time = self.mod_time
key = f"{self.name}:{mod_time:x}:{self.tb}:" + ",".join(f"{v}" for v in obj)
return f"{sha1(key.encode()).hexdigest()[:16]}{kind}"
def none(self) -> NDArray[np.bool_]:
return np.ones(self.media_length, dtype=np.bool_)
def all(self) -> NDArray[np.bool_]:
return np.zeros(self.media_length, dtype=np.bool_)
def read_cache(self, kind: str, obj: Sequence[object]) -> None | np.ndarray:
if self.no_cache:
return None
key = self.obj_tag(kind, obj)
cache_file = os.path.join(gettempdir(), f"ae-{__version__}", f"{key}.npz")
try:
with np.load(cache_file, allow_pickle=False) as npzfile:
return npzfile["data"]
except Exception as e:
self.log.debug(e)
return None
def cache(self, arr: np.ndarray, kind: str, obj: Sequence[object]) -> np.ndarray:
if self.no_cache:
return arr
workdir = os.path.join(gettempdir(), f"ae-{__version__}")
if not os.path.exists(workdir):
os.mkdir(workdir)
cache_file = os.path.join(workdir, f"{self.obj_tag(kind, obj)}.npz")
try:
np.savez(cache_file, data=arr)
except Exception as e:
self.log.warning(f"Cache write failed: {e}")
cache_entries = []
with os.scandir(workdir) as entries:
for entry in entries:
if entry.name.endswith(".npz"):
cache_entries.append((entry.path, entry.stat().st_mtime))
if len(cache_entries) > 10:
# Sort by modification time, oldest first
cache_entries.sort(key=lambda x: x[1])
# Remove oldest files until we're back to 10
for filepath, _ in cache_entries[:-10]:
try:
os.remove(filepath)
except OSError:
pass
return arr
def audio(self, stream: int) -> NDArray[np.float32]:
container = self.container
if stream >= len(container.streams.audio):
raise LevelError(f"audio: audio stream '{stream}' does not exist.")
if (arr := self.read_cache("audio", (stream,))) is not None:
return arr
audio = container.streams.audio[stream]
if audio.duration is not None and audio.time_base is not None:
inaccurate_dur = int(audio.duration * audio.time_base * self.tb)
elif container.duration is not None:
inaccurate_dur = int(container.duration / av.time_base * self.tb)
else:
inaccurate_dur = 1024
bar = self.bar
bar.start(inaccurate_dur, "Analyzing audio volume")
result: NDArray[np.float32] = np.zeros(inaccurate_dur, dtype=np.float32)
index = 0
for value in iter_audio(audio, self.tb):
if index > len(result) - 1:
result = np.concatenate(
(result, np.zeros(len(result), dtype=np.float32))
)
result[index] = value
bar.tick(index)
index += 1
bar.end()
container.seek(0)
assert len(result) > 0
return self.cache(result[:index], "audio", (stream,))
def motion(self, stream: int, blur: int, width: int) -> NDArray[np.float32]:
container = self.container
if stream >= len(container.streams.video):
raise LevelError(f"motion: video stream '{stream}' does not exist.")
mobj = (stream, width, blur)
if (arr := self.read_cache("motion", mobj)) is not None:
return arr
video = container.streams.video[stream]
inaccurate_dur = (
1024
if video.duration is None or video.time_base is None
else int(video.duration * video.time_base * self.tb)
)
bar = self.bar
bar.start(inaccurate_dur, "Analyzing motion")
result: NDArray[np.float32] = np.zeros(inaccurate_dur, dtype=np.float32)
index = 0
for value in iter_motion(video, self.tb, blur, width):
if index > len(result) - 1:
result = np.concatenate(
(result, np.zeros(len(result), dtype=np.float32))
)
result[index] = value
bar.tick(index)
index += 1
bar.end()
container.seek(0)
return self.cache(result[:index], "motion", mobj)
def subtitle(
self,
pattern: str,
stream: int,
ignore_case: bool,
max_count: int | None,
) -> NDArray[np.bool_]:
container = self.container
if stream >= len(container.streams.subtitles):
raise LevelError(f"subtitle: subtitle stream '{stream}' does not exist.")
try:
flags = re.IGNORECASE if ignore_case else 0
re_pattern = re.compile(pattern, flags)
except re.error as e:
self.log.error(e)
try:
subtitle_stream = container.streams.subtitles[stream]
assert isinstance(subtitle_stream.time_base, Fraction)
except Exception as e:
self.log.error(e)
# Get the length of the subtitle stream.
sub_length = 0
for packet in container.demux(subtitle_stream):
if packet.pts is None or packet.duration is None:
continue
for subset in packet.decode():
# See definition of `AVSubtitle`
# in: https://ffmpeg.org/doxygen/trunk/avcodec_8h_source.html
start = float(packet.pts * subtitle_stream.time_base)
dur = float(packet.duration * subtitle_stream.time_base)
end = round((start + dur) * self.tb)
sub_length = max(sub_length, end)
result = np.zeros((sub_length), dtype=np.bool_)
del sub_length
count = 0
early_exit = False
container.seek(0)
for packet in container.demux(subtitle_stream):
if packet.pts is None or packet.duration is None:
continue
if early_exit:
break
for subset in packet.decode():
if max_count is not None and count >= max_count:
early_exit = True
break
start = float(packet.pts * subtitle_stream.time_base)
dur = float(packet.duration * subtitle_stream.time_base)
san_start = round(start * self.tb)
san_end = round((start + dur) * self.tb)
for sub in subset:
if not isinstance(sub, AssSubtitle):
continue
line = sub.dialogue.decode(errors="ignore")
if line and re.search(re_pattern, line):
result[san_start:san_end] = 1
count += 1
container.seek(0)
return result
def initLevels(
src: FileInfo, tb: Fraction, bar: Bar, no_cache: bool, log: Log
) -> Levels:
try:
container = av.open(src.path)
except av.FFmpegError as e:
log.error(e)
mod_time = int(src.path.stat().st_mtime)
return Levels(container, src.path.name, mod_time, tb, bar, no_cache, log)