-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateFinal.py
More file actions
185 lines (151 loc) · 6.45 KB
/
Copy pathcreateFinal.py
File metadata and controls
185 lines (151 loc) · 6.45 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
from moviepy import VideoFileClip, AudioFileClip, TextClip, CompositeVideoClip
from moviepy.video.tools.subtitles import SubtitlesClip
import random
import numpy as np
import torch
import time
import whisper_timestamped as whisper
########### Allows file to save in specific directory ######################################
from pathlib import Path
from datetime import datetime
# -------- CONFIG --------
OUTPUT_DIR = Path("HorrorVideos")
COUNTER_FILE = Path("HorrorStories/counter.txt")
# Ensure output directory exists
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
# Read counter
if COUNTER_FILE.exists():
story_number = (int(COUNTER_FILE.read_text().strip()))
# Get today's date
date_str = datetime.now().strftime("%Y-%m-%d")
# Build filename
output_filename = f"HorrorStory{story_number}_{date_str}.mp4"
output_path = OUTPUT_DIR / output_filename
print(f"Saving video to: {output_path}")
########################################################################
# ----------------- helpers -----------------
def format_time(seconds: float) -> str:
minutes = int(seconds // 60)
remaining_seconds = seconds % 60
return f"{minutes}m {remaining_seconds:.2f}s"
# -------------------------------------------
video_clip = VideoFileClip("MCParkour.mp4") # for videos
video_clip = video_clip.without_audio()
# video file clips already have fps and duration
print("Clip duration: {}".format(video_clip.duration))
print("Clip fps: {}".format(video_clip.fps))
audio_clip = AudioFileClip("finalOutput.mp3")
# Select random segment of video
start_time = random.uniform(0, video_clip.duration - audio_clip.duration)
# New Video with New Duration
video_segment = video_clip.subclipped(start_time, start_time + audio_clip.duration)
print(f"Using video segment from {start_time:.2f}s to {start_time + audio_clip.duration:.2f}s")
print("Clip duration: {}".format(video_segment.duration)) # Cuting will update duration
print("Clip fps: {}".format(video_segment.fps)) # and keep fps
# And finally we can write the result into a file
# Here we just save as MP4, inheriting FPS, etc. from final_clip
# Get .srt file from video
def get_transcribed_text(filename):
audio = whisper.load_audio(filename)
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Whisper device:", device)
model = whisper.load_model("small", device=device)
results = whisper.transcribe(model, audio, language="en")
return results["segments"]
def get_text_clips(text, max_chars_per_clip=35):
text_clips = []
for segment in text:
words = segment["words"]
current_text = ""
current_start = words[0]["start"]
current_end = words[0]["end"]
for word in words:
# Check if adding the next word exceeds the limit
if len(current_text) + len(word["text"]) + 1 <= max_chars_per_clip:
if current_text:
current_text += " "
current_text += word["text"]
current_end = word["end"]
else:
# Create the clip for the current group
text_clips.append(
TextClip(
text=current_text,
method="caption", # ✅ wrapping
font_size=34,
size=(1400, 260), # ✅ wrap to 1400px wide; NOT full frame
stroke_width=5,
stroke_color="black",
font="font/use.ttf",
color="white",
text_align="center",
interline=6, # ✅ more line spacing
margin=(20, 30) # ✅ (x, y) padding INSIDE box
)
.with_position(("center", "center"))
.with_start(current_start)
.with_end(current_end)
)
# Start a new group
current_text = word["text"]
current_start = word["start"]
current_end = word["end"]
# Add the last clip if any text remains
if current_text:
text_clips.append(
TextClip(
text=current_text,
method="caption", # ✅ wrapping
font_size=34,
size=(1400, 260), # ✅ wrap to 1400px wide; NOT full frame
stroke_width=5,
stroke_color="black",
font="font/use.ttf",
color="white",
text_align="center",
interline=6, # ✅ more line spacing
margin=(20, 30) # ✅ (x, y) padding INSIDE box
)
.with_position(("center", "center"))
.with_start(current_start)
.with_end(current_end)
)
return text_clips
# ===================== SUBTITLES TIMER =====================
subtitle_start = time.perf_counter()
# Loading the video as a VideoFileClip
transcribed_text = get_transcribed_text("finalOutput.mp3")
# Generate text elements for video using transcribed text
text_clip_list = get_text_clips(text=transcribed_text)
subtitle_end = time.perf_counter()
subtitle_time = subtitle_end - subtitle_start
# ===========================================================
# Create a CompositeVideoClip that we write to a file
final_clip = CompositeVideoClip([video_segment] + text_clip_list)
final_clip = final_clip.with_audio(audio_clip)
# ===================== VIDEO TIMER =========================
video_start = time.perf_counter()
# Write the final video once (video with subtitles + audio)
final_clip.write_videofile(
str(output_path),
codec="h264_nvenc",
audio_codec="aac",
fps=video_segment.fps,
preset="p4",
ffmpeg_params=["-pix_fmt", "yuv420p"],
temp_audiofile="temp-audio.m4a",
remove_temp=True
)
video_end = time.perf_counter()
video_time = video_end - video_start
# ===========================================================
final_clip.close()
video_segment.close()
video_clip.close()
audio_clip.close()
total_time = subtitle_time + video_time
print("\n====== PERFORMANCE SUMMARY ======")
print(f"Subtitles (Whisper + TextClips): {format_time(subtitle_time)}")
print(f"Video render (NVENC): {format_time(video_time)}")
print(f"Total pipeline time: {format_time(total_time)}")
print("================================\n")