Untitled

 avatar
unknown
plain_text
10 months ago
2.3 kB
10
Indexable
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
import math

# ==== INPUT ====
input_video = "bimar.mp4"
output_video = "bimar_output.mp4"

# Teks ucapan (bagi per kata agar bisa di-highlight)
speech_text = "kami, MTB Bimar Kalimantan Barat. siap mensukseskan kalimantan barat MTB Festival 2025. mantap mantap mantap mantaaap"
words = speech_text.split()

# === SETTING ===
font = "Courier-New"   # pastikan font tersedia, kalau error coba ganti ke "Courier"
fontsize = 40          # ukuran teks
base_color = "white"   # warna dasar teks
highlight_color = "red" # warna highlight untuk kata aktif
stroke_color = "black" # outline hitam
stroke_width = 2

# Load video
video = VideoFileClip(input_video)
duration = video.duration

# Hitung durasi rata-rata per kata
word_duration = duration / len(words)

# Fungsi untuk bikin frame dengan efek ketik + highlight
def make_frame(t):
    # Tentukan kata ke berapa yang sedang aktif
    current_word_index = min(int(t // word_duration), len(words)-1)
    
    # Hitung jumlah karakter yang sudah muncul
    total_chars = int((t / duration) * len(speech_text))
    visible_text = speech_text[:total_chars]

    # Rekonstruksi kalimat dengan highlight kata aktif
    highlighted_words = []
    char_count = 0
    for i, w in enumerate(words):
        if char_count <= total_chars:
            if i == current_word_index:
                highlighted_words.append(f"<span foreground='{highlight_color}'>{w}</span>")
            else:
                highlighted_words.append(w)
        char_count += len(w) + 1  # +1 untuk spasi

    display_text = " ".join(highlighted_words)

    # Buat TextClip dengan pango markup agar bisa ada warna berbeda
    txt_clip = TextClip(
        display_text,
        fontsize=fontsize,
        font=font,
        color=base_color,
        stroke_color=stroke_color,
        stroke_width=stroke_width,
        method="pango"
    ).set_position(("center", "bottom"))

    return txt_clip.get_frame(t)

# Buat clip teks
text_clip = video.fl(make_frame)

# Gabungkan
final = CompositeVideoClip([video, text_clip])

# Export
final.write_videofile(output_video, codec="libx264", audio_codec="aac")
Editor is loading...
Leave a Comment