Spaces:
Sleeping
Sleeping
Robby commited on
Commit ·
ca56ff2
1
Parent(s): 7a0c34e
feat: simplify UI and add subtitle style options
Browse files8 unmodified lines9101112131415161718192021222312 unmodified lines36373830394032414243442 unmodified lines4748494150515253454654555648495758596052536162636457586566676869706271727365666768697074757673777875798081823 unmodified lines86878885899091888992939495969798959699100101102103104105106107991001011081091101041051061111121131141151161171111121131141151161171181191201211228 unmodified linesprint("Loading Whisper model (base)...")model = whisper.load_model("base")
# Subtitle Style Presets (FFmpeg force_style strings)STYLE_PRESETS = { "Classic White": "Fontname=Arial,Fontsize=20,PrimaryColour=&H00FFFFFF,OutlineColour=&H00000000,BorderStyle=1,Outline=2,Shadow=1,Alignment=2", "Social Yellow": "Fontname=Arial,Fontsize=22,PrimaryColour=&H0000FFFF,OutlineColour=&H00000000,BorderStyle=1,Outline=2,Shadow=1,Alignment=2", "Large Bold": "Fontname=Arial,Fontsize=26,Bold=1,PrimaryColour=&H00FFFFFF,OutlineColour=&H00000000,BorderStyle=1,Outline=3,Shadow=0,Alignment=2", "Clean Netflix": "Fontname=Arial,Fontsize=20,PrimaryColour=&H00FFFFFF,OutlineColour=&H00000000,BorderStyle=1,Outline=0,Shadow=2,Alignment=2", "Boxed Text": "Fontname=Arial,Fontsize=18,PrimaryColour=&H00FFFFFF,BackColour=&H80000000,BorderStyle=3,Outline=0,Shadow=0,Alignment=2"}
def format_timestamp(seconds: float): td = datetime.timedelta(seconds=seconds) total_seconds = int(td.total_seconds())12 unmodified lines output += f"{i + 1}\n{start} --> {end}\n{text}\n\n" return output
def transcribe_video(video_path):def process_auto_caption(video_path, style_name): if not video_path: return "Please upload a video file.", None, None return None try: # 1. Transcribe with Whisper2 unmodified lines # 2. Create SRT file temp_dir = tempfile.gettempdir() srt_path = os.path.join(temp_dir, "captions.srt") srt_path = os.path.join(temp_dir, f"captions_{os.getpid()}.srt") with open(srt_path, "w", encoding="utf-8") as f: f.write(create_srt(result['segments'])) # 3. Burn Subtitles into Video using FFmpeg output_video_path = os.path.join(temp_dir, "output_with_captions.mp4") # 3. Burn Subtitles with Style output_video_path = os.path.join(temp_dir, f"output_{os.getpid()}.mp4") # Escape path for FFmpeg (Windows vs Linux handling) # On Hugging Face (Linux), we need to be careful with the subtitle path formatting # FFmpeg escaping for Linux/HF escaped_srt_path = srt_path.replace("\\", "/").replace(":", "\\:") style_string = STYLE_PRESETS.get(style_name, STYLE_PRESETS["Classic White"]) # FFmpeg command to "burn" subtitles # -vf "subtitles=filename" is the standard filter # FFmpeg command command = [ "ffmpeg", "-y", "-i", video_path, "-vf", f"subtitles='{escaped_srt_path}'", "-c:a", "copy", # Copy audio without re-encoding for speed "-vf", f"subtitles='{escaped_srt_path}':force_style='{style_string}'", "-c:a", "copy", "-preset", "veryfast", # Faster encoding for HF CPU output_video_path ] print(f"Running FFmpeg: {' '.join(command)}") print(f"Running FFmpeg Style: {style_name}") subprocess.run(command, check=True) # 4. Format transcript for display display_transcript = "" for s in result['segments']: display_transcript += f"[{format_timestamp(s['start'])}] {s['text']}\n" return display_transcript, output_video_path, srt_path return output_video_path except Exception as e: print(f"Error: {e}") return f"Error: {str(e)}", None, None return None
# Design theme# UI Implementationtheme = gr.themes.Soft( primary_hue="emerald", secondary_hue="blue",3 unmodified lines button_primary_background_fill_hover="linear-gradient(135deg, #059669 0%, #2563eb 100%)",)
with gr.Blocks(theme=theme, title="AutoCap AI") as demo:with gr.Blocks(theme=theme, title="AutoCap AI Pro") as demo: gr.Markdown( """ # 🎬 AutoCap AI (Video Subtitle Burner) Upload video dan AI akan otomatis menambahkan subtitle langsung ke dalam videonya. # 🎬 AutoCap AI Pro Tempel subtitle otomatis ke video Anda dengan berbagai pilihan gaya menarik. """ ) with gr.Row(): with gr.Column(): video_input = gr.Video(label="Upload Video") submit_btn = gr.Button("Mulai Proses AI", variant="primary") video_input = gr.Video(label="1. Unggah Video") style_opt = gr.Dropdown( choices=list(STYLE_PRESETS.keys()), value="Classic White", label="2. Pilih Gaya Subtitle" ) submit_btn = gr.Button("⚡ Proses Video Sekarang", variant="primary") with gr.Column(): video_output = gr.Video(label="Hasil Video + Subtitle") srt_file_output = gr.File(label="Download File .SRT (Opsional)") transcript_output = gr.Textbox(label="Preview Teks", lines=5, interactive=False) video_output = gr.Video(label="3. Hasil Video + Subtitle")
submit_btn.click( fn=transcribe_video, inputs=[video_input], outputs=[transcript_output, video_output, srt_file_output] fn=process_auto_caption, inputs=[video_input, style_opt], outputs=[video_output] ) gr.Markdown( """ ### Cara Kerja: 1. **Upload**: Masukkan video Anda. 2. **AI Transcribe**: Whisper AI akan membaca suara di video tersebut. 3. **Burning**: FFmpeg akan menempelkan teks subtitle tersebut secara permanen ke video Anda. 4. **Download**: Ambil hasilnya dalam format MP4. *Note: Proses 'burning' mungkin memakan waktu tambahan tergantung durasi video.* --- **Tips:** Proses transkripsi dan penempelan video dilakukan sepenuhnya oleh AI. Harap tunggu beberapa saat hingga video hasil muncul di kolom kanan. """ )
app.py+40-38