Skip to content

Commit

Permalink
Merge pull request #158 from ben0815/ytTranscriptLanguage
Browse files Browse the repository at this point in the history
add language option to yt.py
  • Loading branch information
danielmiessler authored Mar 13, 2024
2 parents 88e2964 + 4c56fd7 commit 70cbf8d
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions helpers/yt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python3

import re
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
Expand All @@ -7,7 +9,7 @@
import json
import isodate
import argparse

import sys

def get_video_id(url):
# Extract video ID from URL
Expand Down Expand Up @@ -47,9 +49,29 @@ def main_function(url, options):
duration_seconds = isodate.parse_duration(duration_iso).total_seconds()
duration_minutes = round(duration_seconds / 60)

# Get video transcript language
try:
transcript_available = False
transcript_options = ''
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
for transcript in transcript_list:
if options.language == transcript.language_code:
transcript_available = True
else:
transcript_options += transcript.language + ' (' + \
transcript.language_code + '); '

if not transcript_available:
# exit with existing languages, cause get_transcript will fail
sys.exit('"' + options.language + '" not available. ' + \
'Following languages exists: ' + transcript_options)

except Exception as e:
print(e)

# Get video transcript
try:
transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
transcript_list = YouTubeTranscriptApi.get_transcript(video_id, languages=[options.language])
transcript_text = ' '.join([item['text']
for item in transcript_list])
transcript_text = transcript_text.replace('\n', ' ')
Expand Down Expand Up @@ -77,6 +99,8 @@ def main():
parser = argparse.ArgumentParser(
description='vm (video meta) extracts metadata about a video, such as the transcript and the video\'s duration. By Daniel Miessler.')
parser.add_argument('url', nargs='?', help='YouTube video URL')
parser.add_argument('-l', '--language',
help='Set transcript language (default en)', default='en')
parser.add_argument('--duration', action='store_true',
help='Output only the duration')
parser.add_argument('--transcript', action='store_true',
Expand Down

0 comments on commit 70cbf8d

Please sign in to comment.