File size: 1,840 Bytes
6c575ba
3f4d8ed
 
041cb5b
4927d22
 
6c575ba
3f4d8ed
 
 
 
 
 
 
 
 
 
 
 
4927d22
3f4d8ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aa9031
3f4d8ed
4927d22
 
 
 
 
 
 
3f4d8ed
 
 
4ea4e2b
3f4d8ed
 
 
0aa9031
 
 
3f4d8ed
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 gradio as gr
from moviepy.editor import VideoFileClip
import os
import whisper
import srt
from datetime import timedelta

def extract_audio(video_path):
    try:
        video = VideoFileClip(video_path)
        audio_path = 'audio.mp3'
        audio = video.audio
        audio.write_audiofile(audio_path)
        audio.close()
        video.close()
        return audio_path
    except Exception as e:
        return str(e)

def transcribe_audio_to_srt(audio_path, srt_file="output.srt"):
    model = whisper.load_model("base.en")
    result = model.transcribe(audio_path)
    subtitles = []
    for i, segment in enumerate(result['segments']):
        start_time = segment['start']
        end_time = segment['end']
        content = segment['text'].strip()
        subtitle = srt.Subtitle(index=i+1,
                                start=timedelta(seconds=start_time),
                                end=timedelta(seconds=end_time),
                                content=content)
        subtitles.append(subtitle)
    with open(srt_file, 'w', encoding='utf-8') as f:
        f.write(srt.compose(subtitles))
    return srt_file

def process_video(video):
    video_path = video
    audio_path = extract_audio(video_path)
    if audio_path.endswith('.mp3'):
        processed_audio_path = transcribe_audio_to_srt(audio_path)
        with open(processed_audio_path, "r") as f:
            srt_content = f.read()
        return srt_content
    else:
        return "Failed to extract audio."

iface = gr.Interface(
    fn=process_video,
    inputs=gr.Video(),
    outputs=gr.Textbox(label="Generated SRT File Content"),
    title="Extract and Process Audio from Video",
    description="Upload a video file to extract and process the audio, and view the generated SRT file content.",
    allow_flagging="never"
)

iface.launch()