fixed formating
This commit is contained in:
35
main.py
35
main.py
@@ -1,14 +1,15 @@
|
||||
import os
|
||||
|
||||
from openai import OpenAI
|
||||
from pytube import YouTube
|
||||
|
||||
def download_audio(video_url, output_filename='audio.mp4'):
|
||||
def download(video_url, output_filename='audio.mp4'):
|
||||
try:
|
||||
print("Starting audio download.")
|
||||
print("Downloading audio...")
|
||||
yt = YouTube(video_url)
|
||||
audio_stream = yt.streams.get_audio_only()
|
||||
audio_filename = audio_stream.download(filename=output_filename)
|
||||
print(f"Downloaded '{yt.title}' audio to {audio_filename}")
|
||||
print(f"Downloaded '{yt.title}' audio.")
|
||||
return audio_filename
|
||||
except Exception as e:
|
||||
print(f"Failed to download audio: {e}")
|
||||
@@ -16,7 +17,7 @@ def download_audio(video_url, output_filename='audio.mp4'):
|
||||
|
||||
def transcription(OPENAI_API_KEY, audio_filename):
|
||||
try:
|
||||
print("Transcription started.")
|
||||
print("Transcriptiting audio...")
|
||||
|
||||
client = OpenAI(api_key=OPENAI_API_KEY)
|
||||
audio_file = open(audio_filename, "rb")
|
||||
@@ -27,15 +28,14 @@ def transcription(OPENAI_API_KEY, audio_filename):
|
||||
)
|
||||
|
||||
print("Transcription finished.")
|
||||
|
||||
return transcript
|
||||
except Exception as e:
|
||||
print(f"Failed to transcript audio: {e}")
|
||||
return None
|
||||
|
||||
def summarize(OPENAI_API_KEY, transcript):
|
||||
def summary(OPENAI_API_KEY, transcript):
|
||||
try:
|
||||
print("Summarizing starting")
|
||||
print("Summarizing transcript...")
|
||||
client = OpenAI(api_key=OPENAI_API_KEY)
|
||||
stream = client.chat.completions.create(
|
||||
model="gpt-4",
|
||||
@@ -50,27 +50,30 @@ def summarize(OPENAI_API_KEY, transcript):
|
||||
if chunk.choices[0].delta.content is not None:
|
||||
print(chunk.choices[0].delta.content, end="", flush=True)
|
||||
|
||||
print("Summarizing finished")
|
||||
print("\nSummarizing finished.")
|
||||
except Exception as e:
|
||||
print(f"Failed to summarize transcript: {e}")
|
||||
return None
|
||||
|
||||
def cleanup(audio_filename):
|
||||
try:
|
||||
print("Deleting audio file")
|
||||
print("Deleting audio...")
|
||||
os.remove(audio_filename)
|
||||
print(f"Deleted audio: {audio_filename}")
|
||||
except Exception as e:
|
||||
print(f"Failed to delete audio: {audio_filename}: {e}")
|
||||
print(f"Failed to delete audio: {e}")
|
||||
return None
|
||||
|
||||
def main():
|
||||
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
|
||||
video_url = str(input("video url:"))
|
||||
audio_filename = download_audio(video_url)
|
||||
transcript = transcription(OPENAI_API_KEY, audio_filename)
|
||||
summarize(OPENAI_API_KEY, transcript)
|
||||
cleanup(audio_filename)
|
||||
try:
|
||||
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
|
||||
video_url = str(input("video url:"))
|
||||
audio_filename = download(video_url)
|
||||
transcript = transcription(OPENAI_API_KEY, audio_filename)
|
||||
summary(OPENAI_API_KEY, transcript)
|
||||
cleanup(audio_filename)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user