SPACERUNNER99 commited on
Commit
3f4d8ed
·
verified ·
1 Parent(s): 4724b71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -25
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 download(url):
6
- try:
7
- yt = YouTube(url, on_progress_callback = on_progress)
8
- print(yt.title)
9
- video_path = f"{yt.title}.mp4"
10
- ys = yt.streams.get_highest_resolution()
11
- print(ys)
12
- ys.download()
13
- return f"Downloaded: {video_title}.mp4"
14
- except Exception as e:
15
- return f"Error: {str(e)}"
16
-
17
- # Create Gradio Interface
18
- iface = gr.Interface(
19
- fn=download,
20
- inputs=gr.Textbox(label="YouTube Video URL", placeholder="Enter YouTube video URL..."),
21
- outputs=gr.Textbox(label="Download Status"),
22
- title="YouTube Video Downloader",
23
- description="Enter the URL of a YouTube video to download it in the highest resolution."
24
- )
25
-
26
- # Launch the web app
27
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()