forked from JunMagic88/TLDR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.GetTranscripts.py
38 lines (34 loc) · 1.19 KB
/
01.GetTranscripts.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
import os
from pytube import Playlist, YouTube
from youtube_transcript_api import YouTubeTranscriptApi
# Create the "Texts" folder if it does not already exist
if not os.path.exists("Texts"):
os.makedirs("Texts")
# Open the "YoutubeURLs.txt" file and read the URLs
with open("YoutubeURLs.txt", "r") as file:
urls = file.readlines()
# Iterate through the URLs
for url in urls:
url = url.strip()
if "playlist?list=" in url:
try:
# Try creating a Playlist object
playlist = Playlist(url)
videos = playlist.videos
except Exception as e:
print(f"Error: {e}")
elif "watch?v=" in url:
video = YouTube(url)
videos = [video]
else:
print("Invalid URL")
continue
# Save the SRT transcript from each video into separate files
for video in videos:
with open('Texts/'+video.title +'.txt', "w") as file:
# Iterate through the videos and write their titles to the file
srt = YouTubeTranscriptApi.get_transcript(video.video_id)
text=""
for item in srt:
text = text+(item['text'])+" "
file.write(video.title + "\n\n" +text)