Spaces:
Build error
Build error
Create utils/video_processor.py
Browse files- utils/video_processor.py +129 -0
utils/video_processor.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import subprocess
|
| 3 |
+
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip, AudioFileClip
|
| 4 |
+
from moviepy.video.fx import all as vfx
|
| 5 |
+
import numpy as np
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
import tempfile
|
| 8 |
+
|
| 9 |
+
class VideoProcessor:
|
| 10 |
+
def __init__(self):
|
| 11 |
+
self.temp_dir = tempfile.mkdtemp()
|
| 12 |
+
|
| 13 |
+
def extract_audio(self, video_path):
|
| 14 |
+
"""Extract audio from video file"""
|
| 15 |
+
try:
|
| 16 |
+
video = VideoFileClip(video_path)
|
| 17 |
+
audio_path = Path(self.temp_dir) / "audio.wav"
|
| 18 |
+
video.audio.write_audiofile(str(audio_path), logger=None)
|
| 19 |
+
video.close()
|
| 20 |
+
return str(audio_path)
|
| 21 |
+
except Exception as e:
|
| 22 |
+
raise Exception(f"Audio extraction failed: {str(e)}")
|
| 23 |
+
|
| 24 |
+
def extract_clip(self, video_path, start_time, end_time):
|
| 25 |
+
"""Extract a segment from video"""
|
| 26 |
+
try:
|
| 27 |
+
video = VideoFileClip(video_path)
|
| 28 |
+
clip = video.subclip(start_time, end_time)
|
| 29 |
+
|
| 30 |
+
# Resize to 9:16 for shorts
|
| 31 |
+
target_width = 1080
|
| 32 |
+
target_height = 1920
|
| 33 |
+
|
| 34 |
+
# Calculate scaling
|
| 35 |
+
scale = max(target_width / clip.w, target_height / clip.h)
|
| 36 |
+
new_w = int(clip.w * scale)
|
| 37 |
+
new_h = int(clip.h * scale)
|
| 38 |
+
|
| 39 |
+
# Resize and crop
|
| 40 |
+
clip_resized = clip.resize((new_w, new_h))
|
| 41 |
+
|
| 42 |
+
# Center crop
|
| 43 |
+
x_center = new_w / 2
|
| 44 |
+
y_center = new_h / 2
|
| 45 |
+
x1 = int(x_center - target_width / 2)
|
| 46 |
+
y1 = int(y_center - target_height / 2)
|
| 47 |
+
|
| 48 |
+
clip_cropped = clip_resized.crop(
|
| 49 |
+
x1=x1, y1=y1,
|
| 50 |
+
x2=x1 + target_width,
|
| 51 |
+
y2=y1 + target_height
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
output_path = Path(self.temp_dir) / f"clip_{start_time}_{end_time}.mp4"
|
| 55 |
+
clip_cropped.write_videofile(
|
| 56 |
+
str(output_path),
|
| 57 |
+
codec='libx264',
|
| 58 |
+
audio_codec='aac',
|
| 59 |
+
logger=None
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
video.close()
|
| 63 |
+
clip.close()
|
| 64 |
+
clip_resized.close()
|
| 65 |
+
clip_cropped.close()
|
| 66 |
+
|
| 67 |
+
return str(output_path)
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
raise Exception(f"Clip extraction failed: {str(e)}")
|
| 71 |
+
|
| 72 |
+
def add_captions(self, video_path, captions_data):
|
| 73 |
+
"""Add animated captions to video"""
|
| 74 |
+
try:
|
| 75 |
+
video = VideoFileClip(video_path)
|
| 76 |
+
|
| 77 |
+
clips = [video]
|
| 78 |
+
|
| 79 |
+
for caption in captions_data['captions']:
|
| 80 |
+
# Create text clip
|
| 81 |
+
txt_clip = TextClip(
|
| 82 |
+
caption['text'],
|
| 83 |
+
fontsize=captions_data['fontsize'],
|
| 84 |
+
color=captions_data['color'],
|
| 85 |
+
stroke_color=captions_data['stroke_color'],
|
| 86 |
+
stroke_width=captions_data['stroke_width'],
|
| 87 |
+
font='Arial-Bold',
|
| 88 |
+
method='caption',
|
| 89 |
+
size=(video.w * 0.9, None)
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# Position and timing
|
| 93 |
+
txt_clip = txt_clip.set_position(('center', 'center'))
|
| 94 |
+
txt_clip = txt_clip.set_start(caption['start'])
|
| 95 |
+
txt_clip = txt_clip.set_duration(caption['duration'])
|
| 96 |
+
|
| 97 |
+
# Add animation
|
| 98 |
+
if captions_data['animation'] == 'pop':
|
| 99 |
+
txt_clip = txt_clip.crossfadein(0.2)
|
| 100 |
+
|
| 101 |
+
clips.append(txt_clip)
|
| 102 |
+
|
| 103 |
+
# Compose final video
|
| 104 |
+
final_video = CompositeVideoClip(clips)
|
| 105 |
+
|
| 106 |
+
output_path = video_path.replace('.mp4', '_captioned.mp4')
|
| 107 |
+
final_video.write_videofile(
|
| 108 |
+
output_path,
|
| 109 |
+
codec='libx264',
|
| 110 |
+
audio_codec='aac',
|
| 111 |
+
logger=None
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
video.close()
|
| 115 |
+
final_video.close()
|
| 116 |
+
|
| 117 |
+
return output_path
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
raise Exception(f"Caption addition failed: {str(e)}")
|
| 121 |
+
|
| 122 |
+
def add_sound_effects(self, video_path, sound_effects):
|
| 123 |
+
"""Add sound effects to video at specific timestamps"""
|
| 124 |
+
try:
|
| 125 |
+
# This would integrate with sound effect library
|
| 126 |
+
# For now, return original video
|
| 127 |
+
return video_path
|
| 128 |
+
except Exception as e:
|
| 129 |
+
raise Exception(f"Sound effect addition failed: {str(e)}")
|