-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_youtube_audio.py
76 lines (59 loc) · 2.21 KB
/
get_youtube_audio.py
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
from pytube import YouTube
from openai import OpenAI
from os import environ
import vectara
import google_translate
import streamlit as st
OpenAI_API_KEY = st.secrets["OPENAI_API_KEY"]
client = OpenAI(api_key=OpenAI_API_KEY)
def prepForVectara():
vectara.ResetCorpus()
vectara.AddVideoTranscription()
def get_english_transcription_from_english_youtube(url, language):
video = YouTube(url)
try:
stream = video.streams.filter(only_audio=True).first()
stream.download(filename=f"input_video.mp3")
print("The video is downloaded in MP3")
video_length = video.length
print(f"The video length is {video_length} seconds")
if video_length > 600:
print("The video is too long. Please provide a video of 10 minutes or less.")
return
except KeyError:
print(
"Unable to fetch video information. Please check the video URL or your network connection."
)
audio_file = open("input_video.mp3", "rb")
transcription = client.audio.transcriptions.create(
model="whisper-1", file=audio_file
)
english_transcription = transcription.text
yoruba_transcription = google_translate.translate_english_to_yoruba(
english_transcription
)
french_transcription = google_translate.translate_to_french(
english_transcription
)
# Write transcription to file
with open("video_transcription.txt", "w", encoding="utf-8") as file:
file.write(yoruba_transcription)
print("Text has been written to video_transcription.txt")
prepForVectara()
if language == "English":
return english_transcription
elif language == "Yoruba":
return yoruba_transcription
elif language == "French":
return french_transcription
def askQuestionAboutVideo(prompt, language):
english_answer = vectara.askQuestion(prompt)
yoruba_answer = google_translate.translate_english_to_yoruba(english_answer)
french_answer = google_translate.translate_to_french(english_answer)
print(language)
if language == "English":
return english_answer
elif language == "Yoruba":
return yoruba_answer
elif language == "French":
return french_answer