-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_editor.py
111 lines (89 loc) · 3.68 KB
/
video_editor.py
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
import cv2
import numpy as np
from moviepy.editor import VideoFileClip, AudioFileClip, concatenate_videoclips
from pathlib import Path
class VideoEditor:
def __init__(self):
"""Initialize the Video Editor"""
self.output_dir = Path(__file__).parent / "output"
self.output_dir.mkdir(exist_ok=True)
def reverse_video(self, video_path, output_path):
"""
Reverse a video
Args:
video_path (str): Path to input video
output_path (str): Path for reversed video
"""
try:
# Open the original video
cap = cv2.VideoCapture(video_path)
# Get properties
fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Read frames into a list
frames = []
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frames.append(frame)
cap.release()
# Write reversed frames
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
for frame in reversed(frames):
out.write(frame)
out.release()
except Exception as e:
raise Exception(f"Failed to reverse video: {str(e)}")
def create_video_with_audio(self, video_path, audio_path, output_path=None):
"""
Create a video with audio, using reversed video effect
Args:
video_path (str): Path to input video
audio_path (str): Path to audio file
output_path (str, optional): Path for output video
Returns:
str: Path to the created video
"""
try:
if output_path is None:
output_path = str(self.output_dir / "final_video.mp4")
# Create reversed video
reversed_path = str(self.output_dir / "temp_reversed.mp4")
self.reverse_video(video_path, reversed_path)
# Load videos and audio
original_video = VideoFileClip(video_path)
reversed_video = VideoFileClip(reversed_path)
audio = AudioFileClip(audio_path)
# Combine videos
combined_video = concatenate_videoclips([original_video, reversed_video])
# Create final video matching audio duration
clips = []
current_duration = 0
while current_duration < audio.duration:
clips.append(combined_video)
current_duration += combined_video.duration
final_video = concatenate_videoclips(clips).set_duration(audio.duration)
final_video = final_video.set_audio(audio)
# Export
print("Exporting final video...")
final_video.write_videofile(
output_path,
codec="libx264",
audio_codec="aac",
verbose=False,
logger=None
)
# Cleanup
original_video.close()
reversed_video.close()
audio.close()
final_video.close()
if Path(reversed_path).exists():
Path(reversed_path).unlink()
return output_path
except Exception as e:
raise Exception(f"Failed to create video with audio: {str(e)}")