Created
March 8, 2024 04:47
-
-
Save angel1254mc/94b0a3f4fcbe19772f45c06b89b91b29 to your computer and use it in GitHub Desktop.
Transcribe Video and Generate Captions with Whisper-Timestamped + MoviePy π
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip | |
import whisper_timestamped as whisper | |
filename = "example.mp4" | |
screen_width = 1080 | |
screen_height = 1920 | |
def get_transcribed_text(filename): | |
audio = whisper.load_audio(filename) | |
model = whisper.load_model("small", device="cpu") | |
results = whisper.transcribe(model, audio, language="en") | |
return results["segments"] | |
def get_text_clips(text, fontsize): | |
text_clips = [] | |
for segment in text: | |
for word in segment["words"]: | |
text_clips.append( | |
TextClip(word["text"], | |
fontsize=fontsize, | |
method='caption', | |
stroke_width=5, | |
stroke_color="white", | |
font="Arial-Bold", | |
color="white") | |
.set_start(word["start"]) | |
.set_end(word["end"]) | |
.set_position("center") | |
) | |
return text_clips | |
# Loading the video as a VideoFileClip | |
original_clip = VideoFileClip(filename) | |
# Load the audio in the video to transcribe it and get transcribed text | |
transcribed_text = get_transcribed_text(filename) | |
# Generate text elements for video using transcribed text | |
text_clip_list = get_text_clips(text=transcribed_text, fontsize=90) | |
# Create a CompositeVideoClip that we write to a file | |
final_clip = CompositeVideoClip([original_clip] + text_clip_list) | |
final_clip.write_videofile("final.mp4", codec="libx264") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment