Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9842e52806 | |||
|
b7eabcaa07
|
|||
|
592ac93d40
|
|||
| 2b6d608618 | |||
| 423b9250c5 | |||
| 6138018fa3 | |||
| 98b1a676cb | |||
| 25e9e36372 | |||
|
98b76e8f23
|
|||
| 6a0881e1ee | |||
| d07b46561a | |||
| c3051c1c12 | |||
| f14665d47d |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,2 @@
|
||||
__pycache__
|
||||
transcript_*.txt
|
||||
/transcript_*.txt
|
||||
Binary file not shown.
@@ -24,6 +24,19 @@ COMMON_FALLBACK_LANGUAGES = [
|
||||
ENGLISH_LANGUAGES = ['en', 'en-US', 'en-GB']
|
||||
# This can be used as the list of allowed fallback languages
|
||||
ALLOWED_FALLBACK_LANGUAGES = COMMON_FALLBACK_LANGUAGES
|
||||
COMMON_ENGLISH_WORDS = {
|
||||
'the', 'be', 'to', 'of', 'and', 'a', 'in', 'that','has','acquire', 'have', 'i',
|
||||
'it', 'for', 'not', 'on', 'with', 'he', 'as', 'you', 'do', 'at',
|
||||
'this', 'but', 'his', 'by', 'from', 'they', 'we', 'say', 'her',
|
||||
'she', 'or', 'an', 'will', 'my', 'one', 'all', 'would', 'there',
|
||||
'their', 'what', 'so', 'up', 'out', 'if', 'about', 'who', 'get',
|
||||
'which', 'go', 'me', 'when', 'make', 'can', 'like', 'time', 'no',
|
||||
'just', 'him', 'know', 'take', 'people', 'into', 'year', 'your',
|
||||
'good', 'some', 'could', 'them', 'see', 'other', 'than', 'then',
|
||||
'now', 'look', 'only', 'come', 'its', 'over', 'think', 'also',
|
||||
'back', 'after', 'use', 'two', 'how', 'our', 'work', 'first', 'well',
|
||||
'way', 'even', 'new', 'want', 'because', 'any', 'these', 'give', 'day', 'without'
|
||||
}
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("YouTube Language Codes:")
|
||||
|
||||
44
youtube_transcript_downloader.py
Normal file → Executable file
44
youtube_transcript_downloader.py
Normal file → Executable file
@@ -19,36 +19,17 @@ def extract_video_id(url):
|
||||
patterns = [
|
||||
r'(?:v=|\/)([0-9A-Za-z_-]{11}).*',
|
||||
r'youtu\.be\/([0-9A-Za-z_-]{11})',
|
||||
r'([0-9A-Za-z_-]{11})'
|
||||
]
|
||||
|
||||
for pattern in patterns:
|
||||
match = re.search(pattern, url)
|
||||
if match:
|
||||
return match.group(1)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def get_transcript(video_id, languages=None):
|
||||
"""
|
||||
Get transcript for a YouTube video.
|
||||
|
||||
Args:
|
||||
video_id: The YouTube video ID
|
||||
languages: List of language codes to try (default: ['en', 'en-US'])
|
||||
|
||||
Returns:
|
||||
Transcript object or None if not found
|
||||
"""
|
||||
if languages is None:
|
||||
languages = ['en', 'en-US']
|
||||
|
||||
try:
|
||||
transcript = YouTubeTranscriptApi().fetch(video_id, languages=languages)
|
||||
return transcript
|
||||
except Exception as e:
|
||||
print(f"Error getting transcript: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def get_transcript_text(transcript):
|
||||
"""Extract plain text from transcript."""
|
||||
@@ -212,26 +193,15 @@ def main():
|
||||
def is_english(text):
|
||||
"""Simple check to determine if text is likely English."""
|
||||
# Common English words to check against
|
||||
common_english_words = {
|
||||
'the', 'be', 'to', 'of', 'and', 'a', 'in', 'that', 'have', 'i',
|
||||
'it', 'for', 'not', 'on', 'with', 'he', 'as', 'you', 'do', 'at',
|
||||
'this', 'but', 'his', 'by', 'from', 'they', 'we', 'say', 'her',
|
||||
'she', 'or', 'an', 'will', 'my', 'one', 'all', 'would', 'there',
|
||||
'their', 'what', 'so', 'up', 'out', 'if', 'about', 'who', 'get',
|
||||
'which', 'go', 'me', 'when', 'make', 'can', 'like', 'time', 'no',
|
||||
'just', 'him', 'know', 'take', 'people', 'into', 'year', 'your',
|
||||
'good', 'some', 'could', 'them', 'see', 'other', 'than', 'then',
|
||||
'now', 'look', 'only', 'come', 'its', 'over', 'think', 'also',
|
||||
'back', 'after', 'use', 'two', 'how', 'our', 'work', 'first', 'well',
|
||||
'way', 'even', 'new', 'want', 'because', 'any', 'these', 'give', 'day'
|
||||
}
|
||||
|
||||
|
||||
# Simple heuristic: count common English words
|
||||
words = set(text.lower().split())
|
||||
matches = sum(1 for word in words if word in common_english_words)
|
||||
|
||||
matches = sum(1 for word in words if word in language_codes.COMMON_ENGLISH_WORDS)
|
||||
word_count = len(words)
|
||||
print(f"found matches: {matches} out of {word_count} words")
|
||||
# If we find many common English words, it's likely English
|
||||
return matches > len(words) * 0.05 if words else False
|
||||
return matches > len(words) * 0.04 if words else False
|
||||
|
||||
|
||||
def download_captions(video_id):
|
||||
|
||||
Reference in New Issue
Block a user