-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchange_video_duration.py
More file actions
376 lines (306 loc) · 11.2 KB
/
change_video_duration.py
File metadata and controls
376 lines (306 loc) · 11.2 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
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
import cv2
import os
import numpy as np
from pathlib import Path
from typing import Optional, List
from constants import (
VIDEO_EXTENSIONS,
DEFAULT_INPUT_FOLDER,
DEFAULT_OUTPUT_FOLDER,
DEFAULT_MAPPING_FILE,
DEFAULT_TARGET_DURATION,
DEFAULT_FADE_DURATION,
)
from messages import (
msg,
video_open_error,
video_no_frames_error,
video_processing_error,
folder_not_exist_error,
invalid_input_warning,
invalid_choice_error,
video_mapped_success,
video_mapping_error,
mapping_saved_success,
total_processed_info,
video_created_success,
processing_videos_info,
batch_complete_success,
output_saved_info,
folder_created_info,
folder_setup_instructions,
)
def ensure_folder_exists(folder: str, create: bool = True) -> bool:
"""
Ensure folder exists, optionally creating it.
Args:
folder: Folder path to check/create
create: Whether to create the folder if it doesn't exist
Returns:
True if folder exists or was created, False otherwise
"""
folder_path = Path(folder)
if not folder_path.exists():
if create:
folder_path.mkdir(parents=True, exist_ok=True)
msg.info(folder_created_info(folder))
return True
else:
msg.error(folder_not_exist_error(folder))
msg.info(folder_setup_instructions(folder))
return False
return True
def get_video_files(folder: str) -> List[Path]:
"""Get all video files from a folder"""
folder_path = Path(folder)
if not folder_path.exists():
msg.error(folder_not_exist_error(folder))
msg.info(folder_setup_instructions(folder))
return []
video_files = []
for ext in VIDEO_EXTENSIONS:
video_files.extend(folder_path.glob(f"*{ext}"))
return sorted(video_files)
def get_video_duration(video_path: str) -> Optional[float]:
"""Get the duration of a video file in seconds"""
try:
cap = cv2.VideoCapture(str(video_path))
if not cap.isOpened():
msg.error(video_open_error(video_path))
return None
fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
if fps > 0:
duration = frame_count / fps
else:
duration = 0
cap.release()
return duration
except Exception as e:
msg.error(video_processing_error(video_path, e))
return None
def apply_fade(frame: np.ndarray, alpha: float) -> np.ndarray:
"""Apply fade effect to a frame using alpha blending"""
return (frame * alpha).astype(np.uint8)
def create_video_mapping(
input_folder: str = DEFAULT_INPUT_FOLDER,
output_file: str = DEFAULT_MAPPING_FILE,
target_duration: float = DEFAULT_TARGET_DURATION,
) -> None:
"""
Create a mapping of all video files in a folder with their durations.
Works with any video filenames, not just numbered files.
"""
video_files = get_video_files(input_folder)
if not video_files:
msg.error(folder_not_exist_error(input_folder))
return
# Create mapping
mapping = []
for video_path in video_files:
video_filename = video_path.name
duration = get_video_duration(str(video_path))
if duration is not None:
mapping.append(
f"{video_filename} - Original: {duration:.2f}s, Target: {target_duration:.2f}s"
)
msg.success(video_mapped_success(video_filename, duration, target_duration))
else:
mapping.append(f"{video_filename} - ERROR")
msg.error(video_mapping_error(video_filename))
# Write mapping to file
with open(output_file, "w") as f:
f.write("Video Mapping (Filename - Durations)\n")
f.write(f"Target Duration: {target_duration:.2f}s\n")
f.write("=" * 50 + "\n\n")
for line in mapping:
f.write(line + "\n")
msg.success(mapping_saved_success(output_file))
msg.info(total_processed_info(len([m for m in mapping if "ERROR" not in m])))
def create_video_with_duration(
input_video: str,
output_video: str,
target_duration: float = DEFAULT_TARGET_DURATION,
fade_duration: float = DEFAULT_FADE_DURATION,
apply_fades: bool = False,
) -> bool:
"""
Create a new video from input video with specified duration.
If input is longer, it will be trimmed. If shorter, it will loop.
Optionally applies fade in and fade out effects.
"""
cap = cv2.VideoCapture(input_video)
if not cap.isOpened():
msg.error(video_open_error(input_video))
return False
# Get video properties
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Calculate target frame count
target_frames = int(target_duration * fps)
fade_frames = int(fade_duration * fps) if apply_fades else 0
# Define codec and create VideoWriter
fourcc = cv2.VideoWriter_fourcc(*"mp4v") # type: ignore[attr-defined]
out = cv2.VideoWriter(output_video, fourcc, fps, (width, height))
frames_written = 0
all_frames = []
# Read all frames from input video
while True:
ret, frame = cap.read()
if not ret:
break
all_frames.append(frame)
cap.release()
if len(all_frames) == 0:
msg.error(video_no_frames_error(input_video))
return False
# Write frames until we reach target duration
while frames_written < target_frames:
frame_idx = frames_written % len(all_frames)
frame = all_frames[frame_idx].copy()
# Apply fade effects if enabled
if apply_fades:
# Fade in at the beginning
if frames_written < fade_frames:
alpha = frames_written / fade_frames
frame = apply_fade(frame, alpha)
# Fade out at the end
elif frames_written >= target_frames - fade_frames:
alpha = (target_frames - frames_written) / fade_frames
frame = apply_fade(frame, alpha)
out.write(frame)
frames_written += 1
out.release()
msg.success(video_created_success(output_video, target_duration))
return True
def batch_adjust_durations(
input_folder: str = DEFAULT_INPUT_FOLDER,
output_folder: str = DEFAULT_OUTPUT_FOLDER,
target_duration: float = DEFAULT_TARGET_DURATION,
fade_duration: float = DEFAULT_FADE_DURATION,
apply_fades: bool = False,
) -> None:
"""
Batch process all video files in a folder to have the specified duration.
Works with any video filenames, not just numbered files.
"""
# Ensure output folder exists (create it)
ensure_folder_exists(output_folder, create=True)
# Check input folder exists
if not ensure_folder_exists(input_folder, create=False):
return
video_files = get_video_files(input_folder)
if not video_files:
msg.warning(f"No video files found in '{input_folder}'")
msg.info(folder_setup_instructions(input_folder, output_folder))
return
msg.info(processing_videos_info(target_duration))
success_count = 0
for video_path in video_files:
input_file = str(video_path)
output_file = os.path.join(output_folder, video_path.name)
if create_video_with_duration(
input_file, output_file, target_duration, fade_duration, apply_fades
):
success_count += 1
msg.success(batch_complete_success(success_count, target_duration))
msg.info(output_saved_info(output_folder))
def get_float_input(prompt: str, default: float) -> float:
"""Helper function to get float input with default value"""
user_input = input(prompt).strip()
if not user_input:
return default
try:
return float(user_input)
except ValueError:
msg.warning(invalid_input_warning(default))
return default
def get_yes_no_input(prompt: str, default: bool = False) -> bool:
"""Helper function to get yes/no input"""
user_input = input(prompt).strip().lower()
if not user_input:
return default
return user_input in ["y", "yes", "true", "1"]
def main():
"""
Main function - choose what you want to do:
1. Create mapping of existing videos
2. Adjust all videos to a specified duration with optional fade effects
"""
print("Video Mapping & Duration Adjuster")
print("=" * 50)
print("\nDefault folders:")
print(f" Input: {DEFAULT_INPUT_FOLDER}/")
print(f" Output: {DEFAULT_OUTPUT_FOLDER}/")
print("\nOptions:")
print("1. Create mapping of all videos in folder")
print("2. Adjust all videos to specified duration")
print("3. Both (mapping + adjustment)")
choice = input("\nEnter choice (1/2/3): ").strip()
input_folder = (
input(f"Enter input folder name (default: '{DEFAULT_INPUT_FOLDER}'): ").strip()
or DEFAULT_INPUT_FOLDER
)
# Get target duration for all operations
target_duration = get_float_input(
f"Enter target duration in seconds (default: {DEFAULT_TARGET_DURATION}): ",
DEFAULT_TARGET_DURATION,
)
if choice == "1":
mapping_file = (
input(
f"Enter mapping file name (default: '{DEFAULT_MAPPING_FILE}'): "
).strip()
or DEFAULT_MAPPING_FILE
)
create_video_mapping(input_folder, mapping_file, target_duration)
elif choice == "2":
output_folder = (
input(
f"Enter output folder name (default: '{DEFAULT_OUTPUT_FOLDER}'): "
).strip()
or DEFAULT_OUTPUT_FOLDER
)
apply_fades = get_yes_no_input(
"Apply fade in/out effects? (y/n, default: n): ", default=False
)
fade_duration = DEFAULT_FADE_DURATION
if apply_fades:
fade_duration = get_float_input(
f"Enter fade duration in seconds (default: {DEFAULT_FADE_DURATION}): ",
DEFAULT_FADE_DURATION,
)
batch_adjust_durations(
input_folder, output_folder, target_duration, fade_duration, apply_fades
)
elif choice == "3":
mapping_file = (
input(
f"Enter mapping file name (default: '{DEFAULT_MAPPING_FILE}'): "
).strip()
or DEFAULT_MAPPING_FILE
)
create_video_mapping(input_folder, mapping_file, target_duration)
output_folder = (
input(
f"Enter output folder name (default: '{DEFAULT_OUTPUT_FOLDER}'): "
).strip()
or DEFAULT_OUTPUT_FOLDER
)
apply_fades = get_yes_no_input(
"Apply fade in/out effects? (y/n, default: n): ", default=False
)
fade_duration = DEFAULT_FADE_DURATION
if apply_fades:
fade_duration = get_float_input(
f"Enter fade duration in seconds (default: {DEFAULT_FADE_DURATION}): ",
DEFAULT_FADE_DURATION,
)
batch_adjust_durations(
input_folder, output_folder, target_duration, fade_duration, apply_fades
)
else:
msg.error(invalid_choice_error())
if __name__ == "__main__":
main()