Spaces:
Sleeping
Sleeping
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.") | |