Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,59 @@
|
|
1 |
-
from pytubefix import YouTube
|
2 |
-
from pytubefix.cli import on_progress
|
3 |
import gradio as gr
|
|
|
|
|
4 |
|
5 |
-
def
|
6 |
-
try:
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
return
|
14 |
-
except Exception as e:
|
15 |
-
return
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from moviepy.editor import VideoFileClip
|
3 |
+
import os
|
4 |
|
5 |
+
def extract_audio(video_path):
|
6 |
+
try:
|
7 |
+
video = VideoFileClip(video_path)
|
8 |
+
audio_path = 'audio.mp3'
|
9 |
+
audio = video.audio
|
10 |
+
audio.write_audiofile(audio_path)
|
11 |
+
audio.close()
|
12 |
+
video.close()
|
13 |
+
return audio_path
|
14 |
+
except Exception as e:
|
15 |
+
return str(e)
|
16 |
+
|
17 |
+
def transcribe_audio_to_srt(audio_path, srt_file=out_put.srt):
|
18 |
+
|
19 |
+
|
20 |
+
model = whisper.load_model("base.en")
|
21 |
+
result = model.transcribe(audio_path)
|
22 |
+
# Create a list to hold subtitle entries
|
23 |
+
subtitles = []
|
24 |
+
start_time = 0.0
|
25 |
+
|
26 |
+
for i, segment in enumerate(result['segments']):
|
27 |
+
start_time = segment['start']
|
28 |
+
end_time = segment['end']
|
29 |
+
content = segment['text'].strip()
|
30 |
+
|
31 |
+
# Create a subtitle entry
|
32 |
+
subtitle = srt.Subtitle(index=i+1,
|
33 |
+
start=timedelta(seconds=start_time),
|
34 |
+
end=timedelta(seconds=end_time),
|
35 |
+
content=content)
|
36 |
+
subtitles.append(subtitle)
|
37 |
+
|
38 |
+
# Write the subtitles to an SRT file
|
39 |
+
with open(srt_file, 'w', encoding='utf-8') as f:
|
40 |
+
f.write(srt.compose(subtitles))
|
41 |
+
return srt_file
|
42 |
+
|
43 |
+
def process_video(video):
|
44 |
+
video_path = os.path.join("uploads", video.name)
|
45 |
+
video.save(video_path)
|
46 |
+
audio_path = extract_audio(video_path)
|
47 |
+
processed_audio_path = process_audio(audio_path)
|
48 |
+
with open(processed_audio_path, "r") as f:
|
49 |
+
srt_content = f.read()
|
50 |
+
return srt_content
|
51 |
+
|
52 |
+
iface = gr.Interface(
|
53 |
+
fn=process_video,
|
54 |
+
inputs=gr.Video(type="file"),
|
55 |
+
outputs=gr.Textbox(label="Generated SRT File Content"),
|
56 |
+
title="Extract and Process Audio from Video",
|
57 |
+
description="Upload a video file to extract and process the audio, and view the generated SRT file content.",
|
58 |
+
allow_flagging="never"
|
59 |
+
iface.launch()
|