Spaces:
Runtime error
Runtime error
Upload caption_generator.py
Browse files- caption_generator.py +83 -0
caption_generator.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Helper functions for the video and audio extraction
|
2 |
+
from pytube import YouTube
|
3 |
+
from moviepy.editor import VideoFileClip
|
4 |
+
import os
|
5 |
+
import stable_whisper
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
def download_video(youtube_url, save_path="./"):
|
9 |
+
try:
|
10 |
+
# Create a YouTube object with the link
|
11 |
+
yt = YouTube(youtube_url)
|
12 |
+
|
13 |
+
# Get the highest resolution stream
|
14 |
+
stream = yt.streams.get_highest_resolution()
|
15 |
+
|
16 |
+
# Generate a unique filename for the downloaded video
|
17 |
+
video_filename = "video.mp4"
|
18 |
+
|
19 |
+
# Download the video
|
20 |
+
video_path = stream.download(output_path=save_path, filename=video_filename)
|
21 |
+
print("Download complete!")
|
22 |
+
|
23 |
+
# Extract audio
|
24 |
+
audio_path = extract_audio(video_path, save_path)
|
25 |
+
if audio_path:
|
26 |
+
print("Audio extracted successfully at:", audio_path)
|
27 |
+
return [video_path, audio_path]
|
28 |
+
except Exception as e:
|
29 |
+
print("An error occurred:", str(e))
|
30 |
+
|
31 |
+
|
32 |
+
def extract_audio(video_path, save_path):
|
33 |
+
try:
|
34 |
+
# Load the video clip using moviepy
|
35 |
+
video_clip = VideoFileClip(video_path)
|
36 |
+
|
37 |
+
# Extract audio
|
38 |
+
audio_clip = video_clip.audio
|
39 |
+
|
40 |
+
# Create path for saving audio
|
41 |
+
audio_path = video_path[:-4] + ".mp3"
|
42 |
+
|
43 |
+
# Save audio
|
44 |
+
audio_clip.write_audiofile(audio_path)
|
45 |
+
|
46 |
+
return audio_path
|
47 |
+
|
48 |
+
except Exception as e:
|
49 |
+
print("An error occurred while extracting audio:", str(e))
|
50 |
+
|
51 |
+
# # Example usage
|
52 |
+
# youtube_link = input("Enter the YouTube link: ")
|
53 |
+
# download_video(youtube_link)
|
54 |
+
# BONUS 1: This cell contains the function for the gradio app
|
55 |
+
|
56 |
+
|
57 |
+
def vid_to_subs(link):
|
58 |
+
try:
|
59 |
+
# Download video and extract audio
|
60 |
+
|
61 |
+
|
62 |
+
# Construct the full path to the downloaded video
|
63 |
+
video_path = download_video(link)[0]
|
64 |
+
|
65 |
+
# Transcribe the audio
|
66 |
+
model = stable_whisper.load_model('medium')
|
67 |
+
result = model.transcribe(video_path)
|
68 |
+
|
69 |
+
# Store transcription result in a variable
|
70 |
+
|
71 |
+
transcription = result.to_txt()
|
72 |
+
|
73 |
+
return transcription
|
74 |
+
|
75 |
+
except Exception as e:
|
76 |
+
print("An error occurred:", str(e))
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
demo = gr.Interface(fn=vid_to_subs, inputs="textbox", outputs="textbox")
|
82 |
+
|
83 |
+
demo.launch(share=True, debug=True)
|