File size: 1,629 Bytes
45484a7
07e866e
45484a7
4a7de15
45484a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07e866e
 
 
 
5c2d008
07e866e
45484a7
 
03dace5
07e866e
 
 
5c2d008
07e866e
 
 
 
 
 
 
 
 
 
 
5c2d008
07e866e
 
 
 
 
2c3d30b
5c2d008
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
import streamlit as st
from faster_whisper import WhisperModel
import yt_dlp as youtube_dl
import os

def download_youtube_video(url: str, file_name: str) -> str:
    ydl_opts = {
        'format': 'bestaudio/best',
        'outtmpl': f'{file_name}.%(ext)s',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])
        return f"{file_name}.mp3"

def transcribe_audio(file_name: str):
    model = WhisperModel("medium", device="cpu", compute_type="float32")
    segments, _ = model.transcribe(file_name)
    return ''.join([segment.text for segment in segments])

st.title("YouTube Video Transcription with Whisper")

url = st.text_input("Enter YouTube URL:")

if st.button("Download Audio"):
    if url:
        st.write("Downloading audio...")
        try:
            audio_file = download_youtube_video(url, "podcast_audio")
            st.write("Audio downloaded successfully.")

            if os.path.exists(audio_file):
                with open(audio_file, "rb") as file:
                    st.download_button(
                        label="Download Audio",
                        data=file,
                        file_name="audio.mp3",
                        mime="audio/mpeg"
                    )
            else:
                st.error("Audio file was not saved correctly.")
        except Exception as e:
            st.error(f"An error occurred: {e}")
    else:
        st.error("Please enter a valid YouTube URL.")