Spaces:
Sleeping
Sleeping
Download app.py from cevajosh3-cpu/caption-engine: direct link, hf CLI and curl.
- Browser
- Download file 3.69 kB
-
https://huggingface.co/spaces/cevajosh3-cpu/caption-engine/resolve/main/app.py
- Command line
-
hf download hf://spaces/cevajosh3-cpu/caption-engine/app.py
-
curl -L -o app.py https://huggingface.co/spaces/cevajosh3-cpu/caption-engine/resolve/main/app.py
3.69 kB
| 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() | |