File size: 2,214 Bytes
4513217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Helper functions for the video and audio extraction
from pytube import YouTube
from moviepy.editor import VideoFileClip
import os
import stable_whisper
import gradio as gr

def download_video(youtube_url, save_path="./"):
    try:
        # Create a YouTube object with the link
        yt = YouTube(youtube_url)

        # Get the highest resolution stream
        stream = yt.streams.get_highest_resolution()

        # Generate a unique filename for the downloaded video
        video_filename = "video.mp4"

        # Download the video
        video_path = stream.download(output_path=save_path, filename=video_filename)
        print("Download complete!")

        # Extract audio
        audio_path = extract_audio(video_path, save_path)
        if audio_path:
            print("Audio extracted successfully at:", audio_path)
        return [video_path, audio_path]
    except Exception as e:
        print("An error occurred:", str(e))


def extract_audio(video_path, save_path):
    try:
        # Load the video clip using moviepy
        video_clip = VideoFileClip(video_path)

        # Extract audio
        audio_clip = video_clip.audio

        # Create path for saving audio
        audio_path = video_path[:-4] + ".mp3"

        # Save audio
        audio_clip.write_audiofile(audio_path)

        return audio_path

    except Exception as e:
        print("An error occurred while extracting audio:", str(e))

# # Example usage
# youtube_link = input("Enter the YouTube link: ")
# download_video(youtube_link)
# BONUS 1: This cell contains the function for the gradio app


def vid_to_subs(link):
    try:
        # Download video and extract audio


        # Construct the full path to the downloaded video
        video_path = download_video(link)[0]

        # Transcribe the audio
        model = stable_whisper.load_model('medium')
        result = model.transcribe(video_path)

        # Store transcription result in a variable

        transcription = result.to_txt()

        return transcription

    except Exception as e:
        print("An error occurred:", str(e))




demo = gr.Interface(fn=vid_to_subs, inputs="textbox", outputs="textbox")

demo.launch(share=True, debug=True)