File size: 3,685 Bytes
c268cff
dd99ab1
c268cff
 
1dd182b
 
c268cff
7c4cb34
 
 
 
c268cff
dd99ab1
7c4cb34
c268cff
 
dd99ab1
c268cff
 
7c4cb34
 
 
6efa7dd
dd99ab1
 
7c4cb34
 
dd99ab1
1dd182b
dd99ab1
 
 
 
1dd182b
dd99ab1
5fec5f0
1dd182b
dd99ab1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1dd182b
 
 
 
 
dd99ab1
 
1dd182b
dd99ab1
 
1dd182b
dd99ab1
 
 
 
 
 
 
 
 
 
 
c268cff
6efa7dd
c268cff
45c57ac
dd99ab1
 
 
 
45c57ac
c268cff
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
import os
import subprocess
import gradio as gr
import whisper
# Connect directly to our separate color database file
import color_rules

try:
    model = whisper.load_model("tiny")
except Exception as e:
    model = None

def generate_viral_caption_pipeline(audio_input):
    if audio_input is None or model is None:
        return None
        
    print("[SYSTEM] Executing AI Speech Audio Transcription Matrix...")
    result = model.transcribe(audio_input, word_timestamps=True)
    
    duration = 10.0
    if result.get("segments"):
        duration = result["segments"][-1]["end"]
        
    ass_filename = "subtitles.ass"
    video_raw = "canvas_base.mp4"
    final_output = "viral_captions_pip.mp4"
    
    # ----------------------------------------------------
    # GENERATE DYNAMIC VECTOR SUBTITLE MODULE
    # ----------------------------------------------------
    with open(ass_filename, "w", encoding="utf-8") as f:
        f.write("[Script Info]\nScriptType: v4.00+\nPlayResX: 720\nPlayResY: 1280\n\n")
        f.write("[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n")
        f.write("Style: Hormozi,Arial Black,44,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,-1,0,0,0,100,100,2,0,1,5,0,5,10,10,10,1\n\n")
        f.write("[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n")
        
        word_count_index = 0
        for segment in result.get("segments", []):
            for word in segment.get("words", []):
                start_time = word["start"]
                end_time = word["end"]
                
                def format_ass_time(secs):
                    h = int(secs // 3600)
                    m = int((secs % 3600) // 60)
                    s = secs % 60
                    return f"{h}:{m:02d}:{s:05.2f}"
                
                t_start = format_ass_time(start_time)
                t_end = format_ass_time(end_time)
                
                raw_text = word["word"].strip().upper()
                
                # Fetch color dynamically from our separate rules file matrix
                target_hex_color = color_rules.get_word_color(raw_text, word_count_index)
                
                # Apply word text layout size scale pop animations
                formatted_text = f"{{\\1c{target_hex_color}\\fscx112\\fscy112}}{raw_text}{{\\r}}"
                    
                f.write(f"Dialogue: 0,{t_start},{t_end},Hormozi,,0,0,0,,{formatted_text}\n")
                word_count_index += 1

    # ----------------------------------------------------
    # HARDWARE PIPELINE RENDER ENGINE
    # ----------------------------------------------------
    subprocess.run([
        'ffmpeg', '-y', '-f', 'lavfi', '-i', f'color=c=0x00ff00:s=720x1280:d={duration}:r=30',
        '-c:v', 'libx264', '-pix_fmt', 'yuv420p', video_raw
    ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

    subprocess.run([
        'ffmpeg', '-y', '-i', video_raw, '-i', audio_input,
        '-vf', f'ass={ass_filename}', '-c:v', 'libx264', '-c:a', 'aac',
        '-map', '0:v:0', '-map', '1:a:0', '-shortest', final_output
    ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    
    return final_output

demo = gr.Interface(
    fn=generate_viral_caption_pipeline, 
    inputs=gr.Audio(type="filepath", label="Input Audio Node File"), 
    outputs=gr.Video(label="Output High-Speed Green Screen MP4 Node File"),
    title="Viral Caption Engine Pro"
)
demo.launch()