Spaces:
Runtime error
Runtime error
File size: 1,756 Bytes
b133d5b fb5dc64 b133d5b c529989 bfd1883 b133d5b a1147bd 550e352 b133d5b | 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 | from youtube_transcript_api import YouTubeTranscriptApi
from googleapiclient.discovery import build
from docx import Document
import re
import os
def get_youtube_transcript(video_id):
try:
transcript = YouTubeTranscriptApi.get_transcript(video_id)
return transcript
except Exception as e:
print(f"An error occurred: {e}")
return None
def get_video_title(api_key, video_id):
youtube = build('youtube', 'v3', developerKey=api_key)
request = youtube.videos().list(part='snippet', id=video_id)
response = request.execute()
if 'items' in response and len(response['items']) > 0:
return response['items'][0]['snippet']['title']
return None
def save_transcript_to_doc(transcript, filename):
doc = Document()
doc.add_heading('YouTube Video Transcript', 0)
for entry in transcript:
start_time = entry['start']
duration = entry['duration']
text = entry['text']
doc.add_paragraph(f"{start_time:.2f} - {duration:.2f}: {text}")
doc.save(filename)
def sanitize_filename(filename):
return re.sub(r'[\\/*?:"<>|]', "_", filename)
if __name__ == "__main__":
api_key = os.getenv('YOUR_YOUTUBE_DATA_API_KEY') # Replace this with your API key
video_id = 'nTQIYWgn-lQ' # Replace this with your video ID
transcript = get_youtube_transcript(video_id)
video_title = get_video_title(api_key, video_id)
if transcript and video_title:
sanitized_title = sanitize_filename(video_title)
output_filename = f"./{sanitized_title}.txt"
#save_transcript_to_doc(transcript, output_filename)
print(f"Transcript texty is {transcript}.")
else:
print("Transcript or video title not available.") |