JaMugen commited on
Commit
b0314f2
·
1 Parent(s): 381c36c

Update functions.py

Browse files
Files changed (1) hide show
  1. functions.py +35 -20
functions.py CHANGED
@@ -1,33 +1,48 @@
1
  from pytube import YouTube
2
  from youtube_transcript_api import YouTubeTranscriptApi
3
  import re
 
4
 
5
  def download_video_transcript(video_url):
6
  try:
7
- video_id = re.search(r"(?<=v=)[\w-]+", video_url).group()
8
- yt = YouTube(video_url)
9
- stream = yt.streams.get_highest_resolution()
 
 
10
 
11
- print(f'Downloading {yt.title}...')
12
- modified_title = yt.title.replace(" ", "_")
13
- download_path = 'Milestone1/videos'
14
- stream.download(output_path=download_path, filename=modified_title + '.mp4')
15
- print('Download completed!')
16
 
17
- transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
18
- transcript = transcript_list.find_generated_transcript(['en']).fetch()
 
19
 
20
- captions = ""
21
- for i, line in enumerate(transcript):
22
- start_time = line['start']
23
- formatted_time = f"{int(start_time // 60):02d}:{int(start_time % 60):02d}"
24
- captions += f"{formatted_time} {line['text']}\n"
25
 
26
- with open(f'Milestone1/captions/{modified_title}.txt', 'w', encoding='utf-8') as file:
27
- file.write(captions)
28
-
29
- return captions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  except Exception as e:
31
  print("Error:", e)
32
  return None
33
-
 
1
  from pytube import YouTube
2
  from youtube_transcript_api import YouTubeTranscriptApi
3
  import re
4
+ import os
5
 
6
  def download_video_transcript(video_url):
7
  try:
8
+ video_id = re.search(r"(?<=v=)[\w-]+", video_url)
9
+ if video_id:
10
+ video_id = video_id.group()
11
+ yt = YouTube(video_url)
12
+ stream = yt.streams.get_highest_resolution()
13
 
14
+ print(f'Downloading {yt.title}...')
15
+ modified_title = yt.title.replace(" ", "_")
16
+ download_path = 'CS370_Milestone5/videos' # Updated path to CS370_Milestone5 repository
17
+ captions_path = 'CS370_Milestone5/captions' # Updated path to CS370_Milestone5 repository
 
18
 
19
+ # Create directories if they don't exist
20
+ os.makedirs(download_path, exist_ok=True)
21
+ os.makedirs(captions_path, exist_ok=True)
22
 
23
+ video_file = f'{download_path}/{modified_title}.mp4'
24
+ stream.download(output_path=download_path, filename=modified_title + '.mp4')
25
+ print('Download completed!')
 
 
26
 
27
+ transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
28
+ transcript = transcript_list.find_generated_transcript(['en']).fetch()
29
+
30
+ captions = ""
31
+ for i, line in enumerate(transcript):
32
+ start_time = line['start']
33
+ formatted_time = f"{int(start_time // 60):02d}:{int(start_time % 60):02d}"
34
+ captions += f"{formatted_time} {line['text']}\n"
35
+
36
+ with open(f'{captions_path}/{modified_title}.txt', 'w', encoding='utf-8') as file:
37
+ file.write(captions)
38
+
39
+ # Delete video file after captions are saved
40
+ os.remove(video_file)
41
+
42
+ return captions
43
+ else:
44
+ print("Video ID not found in URL.")
45
+ return None
46
  except Exception as e:
47
  print("Error:", e)
48
  return None