forked from Rakly3/EasyScripts
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoutubeTranscriptDownloader.py
More file actions
658 lines (578 loc) · 25 KB
/
YoutubeTranscriptDownloader.py
File metadata and controls
658 lines (578 loc) · 25 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# ==================================================================
# =========================== IMPORTS =============================
# Standard library imports
import os
import re
import csv
import json
import hashlib
import logging
import datetime
# Third party imports
from tqdm import tqdm
import isodate
from googleapiclient.discovery import build
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api._errors import TranscriptsDisabled, NoTranscriptFound
# Local imports
from prettyPrint import *
# =========================== CONFIGURATION =======================
CONFIG_FILE = ".ytdConfig.json"
API_KEY_FILE = ".yttApiKeys.json"
DEFAULT_CONFIG = {
"LOGFILE_PATH": ".",
"ENABLE_LOGGING": True,
"TRANSCRIPT_FILENAME_LENGTH": 50,
"REGEX_PATTERNS": {
"sanitize_filename": r"[^\w\-\s]",
"youtube_video_id": r"(?:v=|\/)([0-9A-Za-z_-]{11})(?:[&?].*)?",
},
}
class YouTubeTranscriptDownloader:
def __init__(self):
self.config = self.load_config()
self.logfile_name = f"ytd-{datetime.date.today().strftime('%Y-%m-%d')}.log"
self.logfile_path = self.config["LOGFILE_PATH"]
self.logfile = os.path.join(self.logfile_path, self.logfile_name)
self.enable_logging = self.config["ENABLE_LOGGING"]
self.transcript_filename_length = self.config["TRANSCRIPT_FILENAME_LENGTH"]
self.regex_patterns = self.config["REGEX_PATTERNS"]
self.api_key = self.config.get("API_KEY")
if not self.api_key:
raise ValueError(
"API key is missing. Please provide it in '.yttApiKeys.json'"
)
# =========================== SETUP LOGGING ===========================
log_dir = os.path.dirname(self.logfile)
if log_dir and not os.path.exists(log_dir):
os.makedirs(log_dir, exist_ok=True)
if self.enable_logging:
logging.basicConfig(
filename=self.logfile,
level=logging.INFO,
format="%(asctime)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
else:
logging.disable(logging.CRITICAL)
def load_config(self):
config = DEFAULT_CONFIG.copy()
if os.path.exists(CONFIG_FILE):
try:
with open(CONFIG_FILE, "r") as f:
user_config = json.load(f)
config.update(
{k: v for k, v in user_config.items() if v is not None}
)
except Exception as e:
print(
f"Error loading '{CONFIG_FILE}': {e}. Using default settings.",
type="error",
)
if os.path.exists(API_KEY_FILE):
try:
with open(API_KEY_FILE, "r") as f:
api_key_data = json.load(f)
config["API_KEY"] = api_key_data.get("youtube").get("api_key")
except Exception as e:
print(
f"Error loading '{API_KEY_FILE}': {e}. API key not loaded.",
type="error",
)
else:
print(f"API key file '{API_KEY_FILE}' not found.", type="error")
return config
def _sanitize_filename(self, name: str, max_length=None) -> str:
"""Sanitize a string for use as a filename."""
if not isinstance(name, str):
return str(name)
sanitized = name
replacements = [
(r"[^\u0000-\u007F\u0080-\uFFFF]", ""), # Remove non-UTF8 chars
(r"( *)_( *)", r"\1 \2"), # Replace underscore with space
(r"( *)[:]( *)", " - "), # Replace colon with hyphen
(r" +", " "), # Fix multiple spaces
(r"[^\w\- ]", ""), # Remove invalid chars
]
for pattern, replacement in replacements:
sanitized = re.sub(pattern, replacement, sanitized)
if max_length is None:
max_length = self.transcript_filename_length
return sanitized.strip()[:max_length]
def sanitize_text(self, text):
if not text:
return ""
regex_special_chars = r"[\\^$.|?*+(){}[\]]"
emoji_and_non_utf8 = r"[^\u0000-\u007F\u0080-\uFFFF]"
multiple_spaces = r"\s+"
text = re.sub(regex_special_chars, "", text)
text = re.sub(emoji_and_non_utf8, "", text)
text = re.sub(multiple_spaces, " ", text)
return text.strip()
def parse_time_format(self, seconds):
if not isinstance(seconds, (int, float)):
raise ValueError(f"Expected a number, got: {seconds}")
total_seconds = int(seconds)
hours, remainder = divmod(total_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
return (
f"{hours:02}:{minutes:02}:{seconds:02}"
if hours
else f"{minutes:02}:{seconds:02}"
)
def fetch_video_metadata(self, video_id):
"""Fetch video metadata using YouTube Data API."""
try:
youtube = build("youtube", "v3", developerKey=self.api_key)
response = (
youtube.videos()
.list(part="snippet,contentDetails", id=video_id)
.execute()
)
if "items" in response and response["items"]:
item = response["items"][0]
snippet = item["snippet"]
content_details = item["contentDetails"]
title = snippet["title"]
channel_title = snippet["channelTitle"]
publish_date = snippet["publishedAt"][:10]
duration = content_details["duration"]
tags = snippet.get("tags", [])
return {
"title": title,
"channel_title": channel_title,
"publish_date": publish_date,
"duration": duration,
"tags": tags,
}
logging.warning(f"No metadata found for video ID: {video_id}")
except Exception as e:
logging.error(f"Error fetching metadata for video ID {video_id}: {e}")
print(f"An error occurred: {e}", type="error")
return {}
def fetch_single_video(self, video_url=None, metadata=None):
"""
Fetch transcript for a single video using metadata if provided,
otherwise fetch metadata using the YouTube Data API.
"""
if video_url is None:
video_url = input("Enter the video URL: ")
pattern = self.regex_patterns.get(
"youtube_video_id", r"(?:v=|\/)([0-9A-Za-z_-]{11})(?:[&?].*)?"
)
match = re.search(pattern, video_url)
if not match:
print("Invalid URL. Must contain a valid YouTube video ID.", type="warning")
return
video_id = match.group(1)
# Validate provided metadata
if metadata:
required_keys = {
"title",
"channel_title",
"publish_date",
"duration",
"tags",
}
# Check that all keys are present and their values are non-empty/valid
is_metadata_valid = all(
key in metadata and metadata[key] not in [None, "", []]
for key in required_keys
)
if is_metadata_valid:
print(f"Using provided metadata for video ID {video_id}", type=None)
else:
print(
f"Incomplete or invalid metadata for video ID {video_id}. Fetching from API.",
type="warning",
)
metadata = self.fetch_video_metadata(video_id)
# If no metadata could be fetched, skip this video
if not metadata:
print(
f"Failed to fetch metadata for video ID {video_id}. Skipping.",
type="warning",
)
return
# Fetch transcript
try:
transcript = YouTubeTranscriptApi.get_transcript(video_id)
# Save transcript and metadata
self.save_transcript(
video_url,
transcript,
metadata.get("channel_title", "Unknown"),
metadata.get("title", "Unknown"),
metadata.get("publish_date", "Unknown"),
)
print(
f"Transcript for {metadata.get('title', 'Unknown')} saved successfully.",
type="info",
)
except TranscriptsDisabled:
print("Transcripts are disabled for this video.", type="warning")
except NoTranscriptFound:
print("No transcript found for this video.", type="warning")
except Exception as e:
print(f"An error occurred: {e}", type="error")
def get_channel_id_from_url(self, url):
"""Extract the channel ID from a YouTube URL or handle."""
try:
youtube = build("youtube", "v3", developerKey=self.api_key)
if "/@" in url: # Handle or username
handle = url.split("/@")[-1]
response = (
youtube.search()
.list(part="snippet", type="channel", q=handle, maxResults=1)
.execute()
)
elif "channel/" in url: # Direct channel URL
return url.split("channel/")[-1]
else:
raise ValueError("Invalid YouTube channel URL or handle.")
if "items" in response and response["items"]:
return response["items"][0]["snippet"]["channelId"]
else:
raise ValueError("Channel not found.")
except Exception as e:
logging.error(f"Error fetching channel ID for URL {url}: {e}")
print(f"An error occurred: {e}", type="error")
return None
def parse_iso8601_duration(self, iso_duration):
"""Convert ISO 8601 duration (e.g., 'PT1H2M30S') to seconds."""
try:
duration = isodate.parse_duration(iso_duration)
return int(duration.total_seconds())
except Exception as e:
logging.error(f"Error parsing duration '{iso_duration}': {e}")
return 0 # Default to 0 seconds if parsing fails
def fetch_channel_videos(self, channel_url):
"""Fetch all public videos from a channel's uploads playlist with detailed metadata."""
channel_id = self.get_channel_id_from_url(channel_url)
if not channel_id:
print("Failed to retrieve channel ID.", type="error")
return
try:
youtube = build("youtube", "v3", developerKey=self.api_key)
# Get the uploads playlist ID
channel_response = (
youtube.channels()
.list(part="contentDetails,snippet", id=channel_id)
.execute()
)
uploads_playlist_id = channel_response["items"][0]["contentDetails"][
"relatedPlaylists"
]["uploads"]
channel_name = self.sanitize_filename(
channel_response["items"][0]["snippet"]["title"]
)
videos = []
next_page_token = None
while True:
# Fetch video IDs from playlistItems().list
playlist_response = (
youtube.playlistItems()
.list(
playlistId=uploads_playlist_id,
part="snippet",
maxResults=50,
pageToken=next_page_token,
)
.execute()
)
video_ids = []
for item in playlist_response.get("items", []):
snippet = item.get("snippet", {})
resource_id = snippet.get("resourceId", {})
video_id = resource_id.get("videoId")
if video_id:
video_ids.append(video_id)
else:
print(f"Skipping invalid item: {item}", type="warning")
# Fetch detailed metadata for the video IDs
if video_ids:
videos_response = (
youtube.videos()
.list(
part="snippet,contentDetails",
id=",".join(video_ids),
)
.execute()
)
for video in videos_response.get("items", []):
snippet = video.get("snippet", {})
content_details = video.get("contentDetails", {})
raw_duration = content_details.get("duration", "Unknown")
duration_seconds = self.parse_iso8601_duration(raw_duration)
formatted_duration = self.parse_time_format(duration_seconds)
videos.append(
[
video["id"],
snippet.get("title", "Unknown"),
snippet.get("tags", []),
snippet.get("channelTitle", "Unknown"),
snippet.get("publishedAt", "Unknown"),
formatted_duration,
]
)
next_page_token = playlist_response.get("nextPageToken")
if not next_page_token:
break
# Save videos to a CSV file
channel_dir = os.path.join("transcripts", channel_name)
os.makedirs(channel_dir, exist_ok=True)
output_file = os.path.join(channel_dir, f"{channel_name}.csv")
with open(output_file, "w", encoding="utf-8", newline="") as f:
writer = csv.writer(f)
writer.writerow(
[
"Video ID",
"Title",
"Tags",
"Channel Title",
"Publish Date",
"Duration",
]
)
writer.writerows(videos)
print(f"Fetched {len(videos)} videos. Saved to {output_file}.", type="info")
# Ask if the user wants to fetch transcripts for the videos, defaulting to yes on enter
fetch_transcripts = (
input("\nDo you want to fetch transcripts for these videos? [Y/n]: ")
.strip()
.lower()
)
if fetch_transcripts in ["", "y", "yes"]:
self.process_file_with_video_urls(output_file)
except Exception as e:
logging.error(f"Error fetching channel videos: {e}")
print(f"An error occurred: {e}", type="error")
def fetch_playlist_videos(self, playlist_url):
"""Fetch all videos from a YouTube playlist."""
try:
youtube = build("youtube", "v3", developerKey=self.api_key)
# Get the playlist ID from URL
if "/playlist" in playlist_url:
playlist_id = playlist_url.split("?list=")[-1]
else:
raise ValueError("Invalid playlist URL format")
videos = []
next_page_token = None
channel_name = None
while True:
# Fetch video IDs from playlistItems().list
playlist_response = (
youtube.playlistItems()
.list(
part="snippet",
playlistId=playlist_id,
maxResults=50,
pageToken=next_page_token,
)
.execute()
)
video_ids = []
for item in playlist_response.get("items", []):
snippet = item["snippet"]
resource_id = snippet.get("resourceId", {})
video_id = resource_id.get("videoId")
if video_id:
video_ids.append(video_id)
else:
print(f"Skipping invalid item: {item}", type="warning")
# Fetch detailed metadata for the video IDs
if video_ids:
videos_response = (
youtube.videos()
.list(
part="snippet,contentDetails",
id=",".join(video_ids),
)
.execute()
)
for video in videos_response.get("items", []):
snippet = video["snippet"]
content_details = video["contentDetails"]
raw_duration = content_details.get("duration", "Unknown")
duration_seconds = self.parse_iso8601_duration(raw_duration)
formatted_duration = self.parse_time_format(duration_seconds)
videos.append(
[
video["id"],
snippet.get("title", "Unknown"),
snippet.get("tags", []),
snippet.get("channelTitle", "Unknown"),
snippet.get("publishedAt", "Unknown"),
formatted_duration,
]
)
if not channel_name:
channel_name = snippet.get("channelTitle")
next_page_token = playlist_response.get("nextPageToken")
if not next_page_token:
break
# Save videos to a CSV file
filename = self.sanitize_filename(channel_name)
output_file = f"transcripts/{filename}/{filename}.csv"
os.makedirs(os.path.dirname(output_file), exist_ok=True)
with open(output_file, "w", encoding="utf-8", newline="") as f:
writer = csv.writer(f)
writer.writerow(
[
"Video ID",
"Title",
"Tags",
"Channel Title",
"Publish Date",
"Duration",
]
)
writer.writerows(videos)
print(f"Fetched {len(videos)} videos. Saved to {output_file}.", type="info")
# Ask if the user wants to fetch transcripts for the videos, defaulting to yes on enter
fetch_transcripts = (
input("\nDo you want to fetch transcripts for these videos? [Y/n]: ")
.strip()
.lower()
)
if fetch_transcripts in ["", "y", "yes"]:
self.process_file_with_video_urls(output_file)
except Exception as e:
logging.error(f"Error fetching playlist videos: {e}")
print(f"An error occurred: {e}", type="error")
def process_file_with_video_urls(self, file_path=None):
"""Process a file containing video URLs and fetch transcripts for each video."""
if file_path is None:
file_path = input("Enter the path to the file (Text/CSV): ").strip()
if not os.path.exists(file_path):
print("File not found. Please try again.", type="warning")
return
is_csv = file_path.endswith(".csv")
urls = []
try:
with open(file_path, "r", encoding="utf-8") as f:
if is_csv:
reader = csv.reader(f)
next(reader)
for row in reader:
urls.append(f"https://www.youtube.com/watch?v={row[0].strip()}")
else:
for line in f.readlines():
video_id = line.strip()
urls.append(
f"https://www.youtube.com/watch?v={video_id}"
if not video_id.startswith("https://")
else video_id
)
for url in tqdm(urls, desc="Processing URLs", dynamic_ncols=True):
self.fetch_single_video(url)
except Exception as e:
print(f"An error occurred while processing the file: {e}", type="error")
def find_duplicate_transcripts(self):
"""Find duplicate transcripts in the transcripts directory."""
transcripts_dir = input("Enter the path to search for duplicates: ")
if not os.path.exists(transcripts_dir):
print("Directory does not exist.", type="warning")
return
hashes = {}
duplicates = []
for root, _, files in os.walk(transcripts_dir):
for file in files:
if file.endswith(".json"):
file_path = os.path.join(root, file)
file_hash = self.compute_sha1(file_path)
if file_hash in hashes:
duplicates.append((file_path, hashes[file_hash]))
else:
hashes[file_hash] = file_path
if duplicates:
output_file = "duplicates.txt"
with open(output_file, "w", encoding="utf-8") as f:
for dup, original in duplicates:
f.write(f"Duplicate: {dup}\nOriginal: {original}\n\n")
print(f"Saved duplicate transcripts to {output_file}.", type="info")
else:
print("No duplicate transcripts found.", type="info")
def save_transcript(
self, video_url, transcript, channel_name, video_title, publish_date
):
"""Save transcript and metadata to a JSON file."""
sanitized_title = self.sanitize_filename(video_title)
channel_dir = os.path.join("transcripts", self.sanitize_filename(channel_name))
os.makedirs(channel_dir, exist_ok=True)
metadata = self.fetch_video_metadata(video_url.split("v=")[-1].split("&")[0])
transcript_data = {
"metadata": {
"video_url": video_url,
"channel_name": channel_name,
"video_title": video_title,
"publish_date": publish_date,
"duration": metadata.get("duration", "Unknown"),
"tags": metadata.get("tags", []),
},
"transcript": [
{
"text": self.sanitize_text(entry["text"]),
"at": self.parse_time_format(entry["start"]),
}
for entry in transcript
],
}
filename = os.path.join(channel_dir, f"{sanitized_title}.json")
with open(filename, "w", encoding="utf-8") as f:
json.dump(transcript_data, f, indent=4, ensure_ascii=False)
def compute_sha1(self, file_path):
"""Compute the SHA1 hash of a file."""
sha1 = hashlib.sha1()
try:
with open(file_path, "rb") as f:
while chunk := f.read(8192):
sha1.update(chunk)
return sha1.hexdigest()
except Exception as e:
logging.error(f"Error computing SHA1 for {file_path}: {e}")
return None
def main_menu(self):
while True:
print("Main Menu", type="info")
print(
"""
1. Get video transcript
2. Get multiple transcripts from video list
3. Fetch channel videos and save to CSV
4. Fetch playlist videos and save to CSV
5. Find duplicate transcripts
"""
)
print("6. Quit", type="error")
choice = input("Enter your choice: ")
if choice == "1":
self.fetch_single_video()
elif choice == "2":
self.process_file_with_video_urls()
elif choice == "3":
self.fetch_channel_videos(input("Enter channel URL: "))
elif choice == "4":
self.fetch_playlist_videos(input("Enter playlist URL: "))
elif choice == "5":
self.find_duplicate_transcripts()
elif choice == "0":
print("Goodbye!", type="success")
break
else:
print("Invalid choice. Please try again.", type="warning")
if __name__ == "__main__":
downloader = YouTubeTranscriptDownloader()
downloader.main_menu()