1
+ import os
2
+ import sys
3
+
4
+ import requests
5
+ from youtube_transcript_api import YouTubeTranscriptApi
6
+
7
+ # Read the video ID from command-line arguments
8
+ if len (sys .argv ) < 2 :
9
+ print ("Please provide the YouTube video ID as a command-line argument." )
10
+ sys .exit (1 )
11
+
12
+ VIDEO_ID = sys .argv [1 ]
13
+ API_KEY = os .getenv ('YOUTUBE_API_KEY' )
14
+
15
+ def download_transcript ():
16
+ try :
17
+ return YouTubeTranscriptApi .get_transcript (VIDEO_ID )
18
+ except Exception as e :
19
+ print (f"Error downloading transcript: { str (e )} " )
20
+ return None
21
+
22
+ def format_transcript (transcript ):
23
+ formatted = ""
24
+ for entry in transcript :
25
+ start_time = int (entry ['start' ])
26
+ minutes , seconds = divmod (start_time , 60 )
27
+ hours , minutes = divmod (minutes , 60 )
28
+ timestamp = f"{ hours :02d} :{ minutes :02d} :{ seconds :02d} "
29
+ formatted += f"[{ timestamp } ] { entry ['text' ]} \n "
30
+ return formatted
31
+
32
+ def get_video_title (video_id ):
33
+ url = f'https://www.googleapis.com/youtube/v3/videos?part=snippet&id={ video_id } &key={ API_KEY } '
34
+ response = requests .get (url )
35
+ if response .status_code == 200 :
36
+ data = response .json ()
37
+ return data ['items' ][0 ]['snippet' ]['title' ]
38
+ else :
39
+ print (f'Error fetching video title: { response .status_code } ' )
40
+ return None
41
+
42
+ def save_transcript (transcript , title ):
43
+ filename = f"{ title } .txt"
44
+ with open (filename , 'w' , encoding = 'utf-8' ) as f :
45
+ f .write (transcript )
46
+ print (f'Transcript saved to { filename } ' )
47
+
48
+ # Call the function to download
49
+ transcript = download_transcript ()
50
+
51
+ if transcript :
52
+ print ('Transcript downloaded successfully.' )
53
+ formatted_transcript = format_transcript (transcript )
54
+ video_title = get_video_title (VIDEO_ID )
55
+ if video_title :
56
+ save_transcript (formatted_transcript , video_title )
57
+ else :
58
+ print ('Failed to get video title. Saving with video ID.' )
59
+ save_transcript (formatted_transcript , VIDEO_ID )
60
+ else :
61
+ print ('Failed to download transcript.' )
0 commit comments