from moviepy.editor import VideoFileClip, concatenate_videoclips, ImageClip import numpy as np import random from scipy.ndimage import zoom as scizoom # For zooming def cut_and_sync_videos(file_paths, beat_times, image_duration_seconds=1.0): """ Cuts and syncs video/image clips to the beat times with user-specified image duration. """ clips = [] num_files = len(file_paths) num_beats = len(beat_times) for i, beat_time in enumerate(beat_times): file_index = i % num_files file_path = file_paths[file_index] try: if file_path.lower().endswith(('.jpg', '.jpeg', '.png')): clip = ImageClip(file_path) clip = clip.set_duration(image_duration_seconds) else: clip = VideoFileClip(file_path) if i < num_beats - 1: next_beat_time = beat_times[i + 1] duration = next_beat_time - beat_time else: duration = 1.0 cut_clip = clip.subclip(0, min(clip.duration, duration)) clips.append(cut_clip) clip.close() except Exception as e: print(f"Error processing file {file_path}: {e}") return None, [] if len(clips) == 0: return None, [] try: final_clip = concatenate_videoclips(clips, method="compose") return final_clip, clips except Exception as e: print(f"Error concatenating clips: {e}") return None, [] def apply_video_effects(clip, beat_times=None, selected_effect="None"): """ Applies a beat-synchronized video effect based on user selection. """ def effect(gf, t): frame = gf(t) if frame is None: return previous_frame is_near_beat = False beat_proximity_threshold = 0.05 if beat_times is not None: beat_proximity = np.abs(t - np.array(beat_times)) is_near_beat = np.any(beat_proximity < beat_proximity_threshold) processed_frame = frame if is_near_beat: if selected_effect == "Flash": # Create a white flash processed_frame = np.full_like(frame, 255, dtype='uint8') elif selected_effect == "Zoom": zoom_factor = 1.1 # Adjust for intensity h, w = frame.shape[:2] zh = int(h / zoom_factor) zw = int(w / zoom_factor) top = (h - zh) // 2 left = (w - zw) // 2 zoomed_frame = scizoom(frame[top:top+zh, left:left+zw], (zoom_factor, zoom_factor, 1)) # Pad back to original size ph, pw = zoomed_frame.shape[:2] padded_frame = np.zeros_like(frame) padded_frame[(h - ph) // 2 : (h + ph) // 2, (w - pw) // 2 : (w + pw) // 2] = zoomed_frame processed_frame = padded_frame.astype('uint8') elif selected_effect == "Rotate": angle = 5 # Degrees to rotate, adjust for intensity rotated_frame = np.array(Image.fromarray(frame).rotate(angle)) processed_frame = rotated_frame elif selected_effect == "Color Shift": color_factor = np.array([1.2, 0.8, 1.0]) # R, G, B multipliers processed_frame = (frame * color_factor).clip(0, 255).astype('uint8') previous_frame = processed_frame return processed_frame previous_frame = None return clip.fl(effect, apply_to=['mask', 'video']) # Import Pillow if needed for Rotate try: from PIL import Image except ImportError: print("Pillow (PIL) not found. Install it for the Rotate effect: pip install Pillow") # Import scipy for Zoom try: from scipy.ndimage import zoom as scizoom except ImportError: print("SciPy not found. Install it for the Zoom effect: pip install scipy")