-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_processor.py
65 lines (54 loc) · 1.97 KB
/
video_processor.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
import cv2
import cupy as cp
from tqdm import tqdm
from video_stream import VideoStream
from video_writer import VideoWriter
from utils import VideoMetadata, VideoProcessingState, extract_video_metadata
class VideoProcessor:
def __init__(
self,
input_file,
output_file,
batch_size=32,
):
self.input_file = input_file
self.output_file = output_file
self.batch_size = batch_size
self.metadata = extract_video_metadata(input_file)
self.video_stream = VideoStream(input_file, batch_size).start()
self.video_writer = VideoWriter(
output_file,
cv2.VideoWriter_fourcc(*"mp4v"),
self.metadata.fps,
self.metadata.width,
self.metadata.height,
)
def remove_static_frames(self, frame_processor):
state = VideoProcessingState(
total_duration=0,
skipped_duration=0,
previous_frame=None,
)
with tqdm(total=self.metadata.frame_count) as pbar:
while True:
batch = self.video_stream.read()
if batch is None:
break
if not batch: # Skip empty batches
continue
state, include_frames, processed_batch_gpu = (
frame_processor.process_batch(batch, state, self.metadata.fps)
)
# Filter the batch on GPU
filtered_batch_gpu = processed_batch_gpu[include_frames]
# Convert filtered batch back to CPU only when writing
filtered_batch_cpu = cp.asnumpy(filtered_batch_gpu)
self.video_writer.write_batch(filtered_batch_cpu)
pbar.update(len(batch))
self.video_stream.stop()
self.video_writer.stop()
return (
state.total_duration,
state.skipped_duration,
state.total_duration - state.skipped_duration,
)