| import os
|
| import tempfile
|
| import gradio as gr
|
| import moviepy.editor as mp
|
| import subprocess
|
| import assemblyai as aai
|
| import yt_dlp
|
| import shutil
|
| import time
|
| from pathlib import Path
|
| from dotenv import load_dotenv
|
|
|
|
|
| load_dotenv()
|
|
|
|
|
| API_KEY = os.getenv("ASSEMBLYAI_API_KEY", "335c7fb7f8544ec58299700d43d8a592")
|
| aai.settings.api_key = API_KEY
|
|
|
|
|
| def download_youtube(youtube_url, output_path):
|
| ydl_opts = {
|
| 'format': 'best[ext=mp4]',
|
| 'outtmpl': output_path,
|
| }
|
|
|
| with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| ydl.download([youtube_url])
|
|
|
| return output_path
|
|
|
|
|
| def extract_audio(video_path, audio_path):
|
| video = mp.VideoFileClip(video_path)
|
| video.audio.write_audiofile(audio_path, verbose=False, logger=None)
|
| return audio_path
|
|
|
|
|
| def transcribe_audio(audio_path):
|
| transcriber = aai.Transcriber()
|
| transcript = transcriber.transcribe(audio_path)
|
|
|
| if transcript.status == aai.TranscriptStatus.error:
|
| return None, f"Transcription Error: {transcript.error}"
|
|
|
| subtitle_path = os.path.join(tempfile.gettempdir(), "subtitle.srt")
|
| with open(subtitle_path, 'w', encoding='utf-8') as f:
|
| f.write(transcript.export_subtitles_srt())
|
|
|
| return subtitle_path, "Transcription completed successfully"
|
|
|
|
|
| def add_subtitles(video_path, subtitle_path, output_path):
|
|
|
| output_dir = os.path.dirname(output_path)
|
| temp_subtitle = os.path.join(output_dir, "temp_subtitle.srt")
|
|
|
|
|
| shutil.copy(subtitle_path, temp_subtitle)
|
|
|
|
|
| subtitle_filename = os.path.basename(temp_subtitle)
|
|
|
|
|
| subtitle_filename = subtitle_filename.replace("'", "'\\''")
|
|
|
|
|
| command = [
|
| "ffmpeg",
|
| "-i", video_path,
|
| "-vf", f"subtitles={subtitle_filename}:force_style='Fontname=Arial,Fontsize=30,PrimaryColour=&HFFFFFF,OutlineColour=&H000000,BorderStyle=3,Outline=1'",
|
| "-c:a", "copy",
|
| "-y",
|
| output_path
|
| ]
|
|
|
|
|
| current_dir = os.getcwd()
|
| os.chdir(output_dir)
|
|
|
| try:
|
| result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
|
|
| if result.returncode == 0:
|
| return output_path, "Subtitles added successfully!"
|
| else:
|
| return None, f"Error adding subtitles: {result.stderr}"
|
| finally:
|
|
|
| os.chdir(current_dir)
|
| try:
|
| os.remove(temp_subtitle)
|
| except:
|
| pass
|
|
|
|
|
| def process_video(video_input, youtube_url, progress=gr.Progress()):
|
|
|
| temp_dir = tempfile.mkdtemp()
|
|
|
|
|
| progress(0, desc="Starting process...")
|
|
|
|
|
| if youtube_url:
|
| input_video = os.path.join(temp_dir, "youtube_video.mp4")
|
| status_msg = f"Downloading YouTube video: {youtube_url}"
|
| progress(5, desc="Downloading YouTube video...")
|
| yield None, status_msg
|
| try:
|
| download_youtube(youtube_url, input_video)
|
| except Exception as e:
|
| yield None, f"Error downloading YouTube video: {str(e)}"
|
| return
|
| else:
|
| input_video = video_input
|
| if not input_video:
|
| yield None, "Please provide either a video file or a YouTube URL"
|
| return
|
|
|
|
|
| audio_path = os.path.join(temp_dir, "extracted_audio.mp3")
|
| status_msg = "Extracting audio from video..."
|
| progress(20, desc="Extracting audio...")
|
| yield None, status_msg
|
| try:
|
| extract_audio(input_video, audio_path)
|
| except Exception as e:
|
| yield None, f"Error extracting audio: {str(e)}"
|
| return
|
|
|
|
|
| status_msg = "Transcribing audio..."
|
| progress(40, desc="Transcribing audio...")
|
| yield None, status_msg
|
| try:
|
| subtitle_path, msg = transcribe_audio(audio_path)
|
| if not subtitle_path:
|
| yield None, msg
|
| return
|
| except Exception as e:
|
| yield None, f"Error during transcription: {str(e)}"
|
| return
|
|
|
|
|
| output_path = os.path.join(temp_dir, "output_with_subtitles.mp4")
|
| status_msg = "Adding subtitles to video..."
|
| progress(70, desc="Adding subtitles...")
|
| yield None, status_msg
|
| try:
|
| result_path, msg = add_subtitles(input_video, subtitle_path, output_path)
|
| if not result_path:
|
| yield None, msg
|
| return
|
|
|
| progress(95, desc="Finalizing...")
|
| time.sleep(1)
|
| progress(100, desc="Complete!")
|
| yield result_path, "Process completed successfully! Your video with subtitles is ready."
|
| except Exception as e:
|
| yield None, f"Error adding subtitles: {str(e)}"
|
|
|
|
|
| with gr.Blocks(title="Video Subtitle Generator") as app:
|
| gr.Markdown(
|
| """
|
| # 🎬 Video Subtitle Generator
|
|
|
| Upload a video file or provide a YouTube URL to generate subtitles.
|
| """
|
| )
|
|
|
| with gr.Row():
|
| with gr.Column(scale=1):
|
| video_input = gr.Video(label="Upload Video")
|
| youtube_url = gr.Textbox(label="Or Enter YouTube URL")
|
| submit_btn = gr.Button("Generate Subtitles", variant="primary")
|
|
|
| with gr.Column(scale=1):
|
| output_video = gr.Video(label="Video with Subtitles")
|
| status = gr.Textbox(label="Status", value="Ready to process")
|
|
|
| submit_btn.click(
|
| fn=process_video,
|
| inputs=[video_input, youtube_url],
|
| outputs=[output_video, status],
|
| show_progress=True
|
| )
|
|
|
|
|
| if __name__ == "__main__":
|
| app.launch(debug=True) |