#๐ how to i make my script faster! (moviepy)
8 messages ยท Page 1 of 1 (latest)
@candid turret
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
from faster_whisper import WhisperModel
from moviepy import CompositeVideoClip, VideoFileClip, AudioFileClip, TextClip
from multiprocessing import cpu_count
class Subtitlefy:
def __init__(self, video_path, audio_path):
self.model = WhisperModel(
"tiny", cpu_threads= cpu_count(), compute_type="int8"
)
self.audio_path = audio_path
self.video = VideoFileClip(video_path)
self.audio = AudioFileClip(audio_path)
def transcribe(self):
transcribed, _ = self.model.transcribe(self.audio_path, word_timestamps=True)
timestamps = []
for segment in transcribed:
if not segment.words:
continue
for word in segment.words:
timestamps.append((word.start, word.end, word.word))
return timestamps
def create_subtitles(self, timestamps: list[tuple]):
subtitles = []
for start, end, word in timestamps:
subtitle = TextClip(
font="Arial",
font_size=100,
text=word.strip(),
color="white",
stroke_color="black",
stroke_width=10,
method='label',
size=(900,200)
).with_start(start).with_duration(end-start).with_position(("center", "center"))
subtitles.append(subtitle)
return subtitles
def merge(self, subtitles: list):
vid = self.video.with_duration(self.audio.duration).with_audio(self.audio)
final = CompositeVideoClip([vid, *subtitles])
final.write_videofile(
'output.mp4',
fps=30,
preset='ultrafast',
threads=cpu_count(),
bitrate='2500k'
)
s = Subtitlefy('pathtovid', 'kairo/video/generated_speech (1).wav')
subtitles = s.create_subtitles(s.transcribe())
s.merge(subtitles)```
is there any way to make this faster?
its a lot slower with longer videos
i feel like the biggest slow up is in the creating_subtitles func, since it creates a text clip for every word?
I don't have any experience with Whisper or AI APIs in general, but this is what you should be trying to verify. Benchmark the code by timing each part (there are Benchmarking tools that can help with this), and seeing where the time is being spent.
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.