File size: 9,125 Bytes
86e3fda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Video Processor - Handles video I/O using FFmpeg
Supports frame extraction, video assembly, and audio preservation
"""
import os
import cv2
import json
import shutil
import subprocess
import numpy as np
from pathlib import Path
from typing import Generator, Tuple, Optional, Dict
from dataclasses import dataclass


@dataclass
class VideoInfo:
    """Video metadata"""
    width: int
    height: int
    fps: float
    total_frames: int
    duration: float
    has_audio: bool
    codec: str
    filepath: str


class VideoProcessor:
    """Handles all video I/O operations"""
    
    def __init__(self, config):
        self.config = config
        self._verify_ffmpeg()
    
    def _verify_ffmpeg(self):
        """Verify FFmpeg is installed"""
        try:
            result = subprocess.run(
                ["ffmpeg", "-version"], 
                capture_output=True, text=True, timeout=5
            )
            if result.returncode != 0:
                raise RuntimeError("FFmpeg not working properly")
        except FileNotFoundError:
            raise RuntimeError(
                "FFmpeg not found! Install it:\n"
                "  Ubuntu: sudo apt install ffmpeg\n"
                "  Mac: brew install ffmpeg\n"
                "  Windows: choco install ffmpeg"
            )
    
    def get_video_info(self, video_path: str) -> VideoInfo:
        """Extract video metadata using ffprobe"""
        cmd = [
            "ffprobe", "-v", "quiet",
            "-print_format", "json",
            "-show_format", "-show_streams",
            video_path
        ]
        
        result = subprocess.run(cmd, capture_output=True, text=True)
        if result.returncode != 0:
            raise ValueError(f"Cannot read video: {video_path}")
        
        probe = json.loads(result.stdout)
        
        # Find video stream
        video_stream = None
        has_audio = False
        for stream in probe.get("streams", []):
            if stream["codec_type"] == "video" and video_stream is None:
                video_stream = stream
            elif stream["codec_type"] == "audio":
                has_audio = True
        
        if not video_stream:
            raise ValueError("No video stream found")
        
        # Parse FPS
        fps_parts = video_stream.get("r_frame_rate", "30/1").split("/")
        fps = float(fps_parts[0]) / float(fps_parts[1]) if len(fps_parts) == 2 else 30.0
        
        # Parse frame count
        nb_frames = int(video_stream.get("nb_frames", 0))
        duration = float(probe.get("format", {}).get("duration", 0))
        
        if nb_frames == 0 and duration > 0:
            nb_frames = int(duration * fps)
        
        return VideoInfo(
            width=int(video_stream["width"]),
            height=int(video_stream["height"]),
            fps=fps,
            total_frames=nb_frames,
            duration=duration,
            has_audio=has_audio,
            codec=video_stream.get("codec_name", "unknown"),
            filepath=video_path
        )
    
    def extract_frames_to_dir(
        self, 
        video_path: str, 
        output_dir: str,
        max_height: Optional[int] = None,
        progress_callback=None
    ) -> Tuple[str, VideoInfo]:
        """
        Extract all frames as numbered JPEGs (required by SAM 2)
        
        Args:
            video_path: Path to input video
            output_dir: Directory to save frames
            max_height: Optional max height for resizing
            progress_callback: Optional callback(current, total)
        
        Returns:
            (frames_dir, video_info)
        """
        os.makedirs(output_dir, exist_ok=True)
        info = self.get_video_info(video_path)
        
        # Build FFmpeg command
        cmd = ["ffmpeg", "-y", "-i", video_path]
        
        # Add scaling if needed
        if max_height and info.height > max_height:
            cmd.extend(["-vf", f"scale=-2:{max_height}"])
        
        # Output as numbered JPEGs
        cmd.extend([
            "-qscale:v", "2",  # High quality JPEG
            os.path.join(output_dir, "%06d.jpg")
        ])
        
        process = subprocess.Popen(
            cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
        )
        _, stderr = process.communicate()
        
        if process.returncode != 0:
            raise RuntimeError(f"Frame extraction failed: {stderr.decode()}")
        
        # Count extracted frames
        frame_files = sorted([
            f for f in os.listdir(output_dir) 
            if f.endswith('.jpg')
        ])
        info.total_frames = len(frame_files)
        
        # Update dimensions if resized
        if frame_files:
            sample = cv2.imread(os.path.join(output_dir, frame_files[0]))
            if sample is not None:
                info.height, info.width = sample.shape[:2]
        
        print(f"📹 Extracted {info.total_frames} frames ({info.width}x{info.height} @ {info.fps:.1f} FPS)")
        return output_dir, info
    
    def read_frames_generator(
        self, 
        video_path: str,
        max_height: Optional[int] = None
    ) -> Generator[Tuple[int, np.ndarray], None, None]:
        """
        Stream frames from video using OpenCV (memory efficient)
        
        Yields:
            (frame_index, frame_bgr)
        """
        cap = cv2.VideoCapture(video_path)
        if not cap.isOpened():
            raise ValueError(f"Cannot open video: {video_path}")
        
        frame_idx = 0
        while True:
            ret, frame = cap.read()
            if not ret:
                break
            
            # Resize if needed
            if max_height and frame.shape[0] > max_height:
                scale = max_height / frame.shape[0]
                new_w = int(frame.shape[1] * scale)
                frame = cv2.resize(frame, (new_w, max_height))
            
            yield frame_idx, frame
            frame_idx += 1
        
        cap.release()
    
    def assemble_video(
        self, 
        frames_dir: str,
        output_path: str,
        fps: float,
        original_video: Optional[str] = None,
        progress_callback=None
    ) -> str:
        """
        Assemble processed frames back into video with optional audio
        
        Args:
            frames_dir: Directory with numbered JPEG frames
            output_path: Output video path
            fps: Frame rate
            original_video: Original video to copy audio from
            progress_callback: Optional callback
        
        Returns:
            Path to output video
        """
        temp_video = output_path + ".temp.mp4"
        
        # Step 1: Encode frames to video
        cmd = [
            "ffmpeg", "-y",
            "-framerate", str(fps),
            "-i", os.path.join(frames_dir, "%06d.jpg"),
            "-c:v", self.config.video.output_codec,
            "-crf", str(self.config.video.output_crf),
            "-preset", self.config.video.output_preset,
            "-pix_fmt", self.config.video.pixel_format,
            "-movflags", "+faststart",
        ]
        
        if original_video:
            cmd.append(temp_video)
        else:
            cmd.append(output_path)
        
        process = subprocess.Popen(
            cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
        )
        _, stderr = process.communicate()
        
        if process.returncode != 0:
            raise RuntimeError(f"Video encoding failed: {stderr.decode()}")
        
        # Step 2: Mux audio from original video
        if original_video:
            info = self.get_video_info(original_video)
            
            if info.has_audio:
                mux_cmd = [
                    "ffmpeg", "-y",
                    "-i", temp_video,
                    "-i", original_video,
                    "-c:v", "copy",
                    "-c:a", "aac",
                    "-map", "0:v:0",
                    "-map", "1:a:0?",
                    "-shortest",
                    output_path
                ]
                
                process = subprocess.Popen(
                    mux_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
                )
                _, stderr = process.communicate()
                
                if process.returncode != 0:
                    # Fallback: just use video without audio
                    shutil.move(temp_video, output_path)
                    print("⚠️  Audio muxing failed, output has no audio")
                else:
                    os.remove(temp_video)
                    print("🔊 Audio preserved from original video")
            else:
                shutil.move(temp_video, output_path)
        
        file_size_mb = os.path.getsize(output_path) / (1024 * 1024)
        print(f"✅ Output video: {output_path} ({file_size_mb:.1f} MB)")
        
        return output_path
    
    def cleanup_temp(self, temp_dir: str):
        """Remove temporary files"""
        if os.path.exists(temp_dir):
            shutil.rmtree(temp_dir, ignore_errors=True)