-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_utils.py
More file actions
264 lines (218 loc) · 8.38 KB
/
export_utils.py
File metadata and controls
264 lines (218 loc) · 8.38 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
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
"""
Export utilities for custom resolution, GIF, and frame sequence export
"""
import subprocess
import logging
import os
from pathlib import Path
from typing import Optional, Tuple
import tempfile
try:
import cv2
CV2_AVAILABLE = True
except ImportError:
CV2_AVAILABLE = False
logging.warning("OpenCV not available for export processing")
def find_ffmpeg():
"""Find ffmpeg executable."""
import shutil
ffmpeg_path = shutil.which('ffmpeg')
if ffmpeg_path:
return ffmpeg_path
import platform
if platform.system() == 'Windows':
common_paths = [
'C:\\ffmpeg\\bin\\ffmpeg.exe',
'C:\\Program Files\\ffmpeg\\bin\\ffmpeg.exe',
]
else:
common_paths = [
'/usr/bin/ffmpeg',
'/usr/local/bin/ffmpeg',
]
for path in common_paths:
if os.path.exists(path):
return path
return 'ffmpeg'
FFMPEG_PATH = find_ffmpeg()
def export_custom_resolution(video_path: str, output_path: str, width: int, height: int,
maintain_aspect: bool = True, quality: str = 'high') -> bool:
"""
Export video at custom resolution.
Args:
video_path: Input video path
output_path: Output video path
width: Target width
height: Target height
maintain_aspect: Maintain aspect ratio (adds padding if True)
quality: Quality preset ('low', 'medium', 'high', 'ultra')
Returns:
True if successful, False otherwise
"""
try:
# Quality presets for encoding
quality_settings = {
'low': {'crf': '28', 'preset': 'fast'},
'medium': {'crf': '23', 'preset': 'medium'},
'high': {'crf': '18', 'preset': 'slow'},
'ultra': {'crf': '15', 'preset': 'veryslow'}
}
settings = quality_settings.get(quality, quality_settings['high'])
if maintain_aspect:
# Scale with padding to maintain aspect ratio
filter_complex = f"scale={width}:{height}:force_original_aspect_ratio=decrease,pad={width}:{height}:(ow-iw)/2:(oh-ih)/2:color=black"
else:
# Stretch to exact dimensions
filter_complex = f"scale={width}:{height}"
command = [
FFMPEG_PATH,
'-i', str(video_path),
'-vf', filter_complex,
'-c:v', 'libx264',
'-crf', settings['crf'],
'-preset', settings['preset'],
'-pix_fmt', 'yuv420p',
'-y',
str(output_path)
]
subprocess.run(command, capture_output=True, check=True)
if os.path.exists(output_path):
logging.info(f"Custom resolution export successful: {output_path}")
return True
else:
logging.error("Custom resolution export failed - output file not created")
return False
except subprocess.CalledProcessError as e:
logging.error(f"Error exporting custom resolution: {e.stderr.decode() if e.stderr else str(e)}")
return False
except Exception as e:
logging.error(f"Error in export_custom_resolution: {str(e)}")
return False
def export_gif(video_path: str, output_path: str, fps: int = 15,
width: int = 512, optimize: bool = True,
colors: int = 256) -> bool:
"""
Export video as animated GIF.
Args:
video_path: Input video path
output_path: Output GIF path
fps: Frame rate for GIF (lower = smaller file)
width: Width of GIF (height auto-calculated)
optimize: Use GIF optimization
colors: Number of colors (256 max, lower = smaller file)
Returns:
True if successful, False otherwise
"""
try:
# First, create palette for better quality
palette_path = str(output_path).replace('.gif', '_palette.png')
# Generate palette
palette_command = [
FFMPEG_PATH,
'-i', str(video_path),
'-vf', f'fps={fps},scale={width}:-1:flags=lanczos,palettegen=max_colors={colors}',
'-y',
palette_path
]
subprocess.run(palette_command, capture_output=True, check=True)
# Create GIF using palette
gif_command = [
FFMPEG_PATH,
'-i', str(video_path),
'-i', palette_path,
'-filter_complex', f'fps={fps},scale={width}:-1:flags=lanczos[x];[x][1:v]paletteuse',
'-y',
str(output_path)
]
subprocess.run(gif_command, capture_output=True, check=True)
# Cleanup palette file
if os.path.exists(palette_path):
os.remove(palette_path)
if os.path.exists(output_path):
logging.info(f"GIF export successful: {output_path}")
return True
else:
logging.error("GIF export failed - output file not created")
return False
except subprocess.CalledProcessError as e:
logging.error(f"Error exporting GIF: {e.stderr.decode() if e.stderr else str(e)}")
return False
except Exception as e:
logging.error(f"Error in export_gif: {str(e)}")
return False
def export_frame_sequence(video_path: str, output_dir: str,
format: str = 'png', prefix: str = 'frame',
start_frame: int = 0, end_frame: Optional[int] = None,
step: int = 1) -> Tuple[bool, int]:
"""
Export video frames as image sequence.
Args:
video_path: Input video path
output_dir: Output directory for frames
format: Image format ('png', 'jpg', 'webp')
prefix: Filename prefix
start_frame: Start frame number (0-indexed)
end_frame: End frame number (None = all frames)
step: Frame step (1 = all frames, 2 = every other frame, etc.)
Returns:
Tuple of (success, frame_count)
"""
try:
output_dir_path = Path(output_dir)
output_dir_path.mkdir(parents=True, exist_ok=True)
# Build ffmpeg command
command = [
FFMPEG_PATH,
'-i', str(video_path),
]
# Add frame selection filters
filters = []
if start_frame > 0:
filters.append(f'select=gte(n\\,{start_frame})')
if end_frame is not None:
filters.append(f'select=lte(n\\,{end_frame})')
if step > 1:
filters.append(f'select=not(mod(n\\,{step}))')
if filters:
filter_complex = ','.join(filters)
command.extend(['-vf', filter_complex])
# Output pattern
output_pattern = str(output_dir_path / f'{prefix}_%06d.{format}')
command.extend(['-y', output_pattern])
subprocess.run(command, capture_output=True, check=True)
# Count exported frames
frame_count = len(list(output_dir_path.glob(f'{prefix}_*.{format}')))
if frame_count > 0:
logging.info(f"Frame sequence export successful: {frame_count} frames to {output_dir}")
return True, frame_count
else:
logging.error("Frame sequence export failed - no frames exported")
return False, 0
except subprocess.CalledProcessError as e:
logging.error(f"Error exporting frame sequence: {e.stderr.decode() if e.stderr else str(e)}")
return False, 0
except Exception as e:
logging.error(f"Error in export_frame_sequence: {str(e)}")
return False, 0
def get_video_resolution(video_path: str) -> Tuple[int, int]:
"""Get video resolution (width, height)."""
try:
import subprocess
import json
command = [
'ffprobe',
'-v', 'error',
'-select_streams', 'v:0',
'-show_entries', 'stream=width,height',
'-of', 'json',
str(video_path)
]
result = subprocess.run(command, capture_output=True, text=True, check=True)
data = json.loads(result.stdout)
if 'streams' in data and len(data['streams']) > 0:
width = data['streams'][0].get('width', 0)
height = data['streams'][0].get('height', 0)
return width, height
return 0, 0
except Exception:
return 0, 0