File size: 2,052 Bytes
7457d43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import whisper
import moviepy.editor as mp
import gradio as gr

# Load Whisper model for audio transcription
model_name = "base"
whisper_model = whisper.load_model(model_name)

def generate_srt(transcription_result):
    """Helper function to convert Whisper's transcription with timestamps to SRT format."""
    srt_content = ""
    for i, segment in enumerate(transcription_result['segments']):
        # Convert start and end times to SRT time format
        start = segment['start']
        end = segment['end']
        start_time = f"{int(start//3600):02}:{int((start%3600)//60):02}:{int(start%60):02},{int((start%1)*1000):03}"
        end_time = f"{int(end//3600):02}:{int((end%3600)//60):02}:{int(end%60):02},{int((end%1)*1000):03}"

        # Create the SRT entry
        srt_content += f"{i+1}\n{start_time} --> {end_time}\n{segment['text'].strip()}\n\n"

    return srt_content

def transcribe_and_generate_subtitles(video):
    # Step 1: Extract the audio from the video using moviepy
    video_clip = mp.VideoFileClip(video)
    audio_path = "temp_audio.wav"
    video_clip.audio.write_audiofile(audio_path, codec='pcm_s16le')

    # Step 2: Transcribe the audio with timestamps using Whisper
    transcription_result = whisper_model.transcribe(audio_path, task="transcribe", language="en", verbose=False)

    # Step 3: Generate SRT subtitles
    srt_content = generate_srt(transcription_result)

    # Save SRT file
    srt_file = "output_subtitles.srt"
    with open(srt_file, "w", encoding="utf-8") as f:
        f.write(srt_content)

    return transcription_result["text"], srt_file

# Set up Gradio interface
interface = gr.Interface(
    fn=transcribe_and_generate_subtitles,
    inputs=gr.Video(label="Upload Video File"),
    outputs=[
        gr.Textbox(label="English Transcription"),
        gr.File(label="Subtitle File (SRT)")
    ],
    title="Video Subtitle Generator",
    description="Upload a video file, and this will generate a transcription and subtitles in SRT format."
)

# Launch the interface
interface.launch()