Spaces:
Sleeping
Sleeping
File size: 7,626 Bytes
91d209c |
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 |
"""
Video Processing Utilities
Handles video operations like frame extraction, trimming, and concatenation
"""
import subprocess
import os
import base64
from pathlib import Path
from typing import List, Tuple, Optional
import json
import tempfile
def get_video_info(video_path: str) -> dict:
"""
Get video metadata using ffprobe
Args:
video_path: Path to video file
Returns:
Dictionary containing video metadata
"""
try:
cmd = [
'ffprobe',
'-v', 'quiet',
'-print_format', 'json',
'-show_format',
'-show_streams',
video_path
]
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
return json.loads(result.stdout)
except subprocess.CalledProcessError as e:
raise Exception(f"Failed to get video info: {e}")
except FileNotFoundError:
raise Exception("ffprobe not found. Please install ffmpeg.")
def extract_frame(
video_path: str,
timestamp: float,
output_path: Optional[str] = None,
return_base64: bool = False,
compress: bool = True
) -> str:
"""
Extract a single frame from video at specified timestamp
Args:
video_path: Path to video file
timestamp: Time in seconds to extract frame
output_path: Output path for frame (optional)
return_base64: Return base64-encoded image instead of path
compress: If False, use lossless PNG format (no compression)
Returns:
Path to extracted frame or base64 data URL
"""
try:
if compress:
# Compressed JPEG (default behavior)
if not output_path:
output_path = tempfile.mktemp(suffix='.jpg')
cmd = [
'ffmpeg',
'-ss', str(timestamp),
'-i', video_path,
'-vframes', '1',
'-q:v', '2', # JPEG quality (2 = high quality)
'-y',
output_path
]
mime_type = 'image/jpeg'
else:
# Uncompressed PNG (lossless, for continuity frames)
if not output_path:
output_path = tempfile.mktemp(suffix='.png')
cmd = [
'ffmpeg',
'-ss', str(timestamp),
'-i', video_path,
'-vframes', '1',
'-f', 'image2', # Force image format
'-pix_fmt', 'rgba', # Preserve full color information
'-y',
output_path
]
mime_type = 'image/png'
subprocess.run(cmd, capture_output=True, check=True)
if return_base64:
with open(output_path, 'rb') as f:
image_data = base64.b64encode(f.read()).decode('utf-8')
# Clean up temp file if we created it
if not output_path:
os.remove(output_path)
return f"data:{mime_type};base64,{image_data}"
return output_path
except subprocess.CalledProcessError as e:
raise Exception(f"Failed to extract frame: {e}")
except FileNotFoundError:
raise Exception("ffmpeg not found. Please install ffmpeg.")
def extract_transition_frames(
video_path: str,
transition_window: float = 1.5
) -> List[Tuple[float, str]]:
"""
Extract transition frames from the end of a video
Args:
video_path: Path to video file
transition_window: Time window before end (in seconds)
Returns:
List of (timestamp, base64_data_url) tuples
"""
try:
# Get video duration
info = get_video_info(video_path)
duration = float(info['format']['duration'])
# Calculate transition zone
transition_start = max(0, duration - transition_window)
# Extract 3 frames
timestamps = [
transition_start, # Mid-point
duration - 0.5, # Near end
duration - 0.1 # Final frame
]
frames = []
for timestamp in timestamps:
frame_data = extract_frame(video_path, timestamp, return_base64=True)
frames.append((timestamp, frame_data))
return frames
except Exception as e:
raise Exception(f"Failed to extract transition frames: {e}")
def trim_video(
input_path: str,
output_path: str,
start_time: float,
end_time: float
) -> str:
"""
Trim video to specified time range
Args:
input_path: Input video path
output_path: Output video path
start_time: Start time in seconds
end_time: End time in seconds
Returns:
Path to trimmed video
"""
try:
duration = end_time - start_time
cmd = [
'ffmpeg',
'-ss', str(start_time),
'-i', input_path,
'-t', str(duration),
'-c:v', 'libx264',
'-c:a', 'aac',
'-y',
output_path
]
subprocess.run(cmd, capture_output=True, check=True)
return output_path
except subprocess.CalledProcessError as e:
raise Exception(f"Failed to trim video: {e}")
except FileNotFoundError:
raise Exception("ffmpeg not found. Please install ffmpeg.")
def concatenate_videos(
video_paths: List[str],
output_path: str
) -> str:
"""
Concatenate multiple videos into one
Args:
video_paths: List of video file paths
output_path: Output path for concatenated video
Returns:
Path to concatenated video
"""
try:
# Create temporary file list
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as f:
for path in video_paths:
f.write(f"file '{os.path.abspath(path)}'\n")
list_file = f.name
try:
cmd = [
'ffmpeg',
'-f', 'concat',
'-safe', '0',
'-i', list_file,
'-c', 'copy',
'-y',
output_path
]
subprocess.run(cmd, capture_output=True, check=True)
return output_path
finally:
os.remove(list_file)
except subprocess.CalledProcessError as e:
raise Exception(f"Failed to concatenate videos: {e}")
except FileNotFoundError:
raise Exception("ffmpeg not found. Please install ffmpeg.")
def generate_thumbnail(
video_path: str,
output_path: str,
width: int = 160,
height: int = 90
) -> str:
"""
Generate thumbnail from video
Args:
video_path: Path to video file
output_path: Output path for thumbnail
width: Thumbnail width
height: Thumbnail height
Returns:
Path to thumbnail
"""
try:
cmd = [
'ffmpeg',
'-i', video_path,
'-vf', f'scale={width}:{height}',
'-frames:v', '1',
'-q:v', '2',
'-y',
output_path
]
subprocess.run(cmd, capture_output=True, check=True)
return output_path
except subprocess.CalledProcessError as e:
raise Exception(f"Failed to generate thumbnail: {e}")
except FileNotFoundError:
raise Exception("ffmpeg not found. Please install ffmpeg.")
|