Video analysis: download, keyframes, transcripts, thumbnail grids and AI summaries of what's on screen.

0 downloads

dbbasic-video/src/dbbasic_video/get_transcript.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-video.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-video.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-video.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Update README with live PyPI link c32f6df 1 year, 1 month ago History
get_transcript.py · 2.3 KB · 80 lines
1#!/usr/bin/env python3
2"""
3Download YouTube transcript
4"""
5
6import argparse
7import json
8from youtube_transcript_api import YouTubeTranscriptApi as yt_api
9
10
11def download_transcript(video_id, output_file=None):
12 """Download transcript for a YouTube video."""
13 print(f"Fetching transcript for video ID: {video_id}")
14
15 try:
16 # Get transcript
17 fetched = yt_api().fetch(video_id)
18 transcript = [{'start': s.start, 'duration': s.duration, 'text': s.text} for s in fetched]
19
20 if not output_file:
21 output_file = f"{video_id}_transcript.json"
22
23 # Save JSON version
24 with open(output_file, 'w') as f:
25 json.dump(transcript, f, indent=2)
26
27 print(f"Transcript saved to {output_file}")
28
29 # Save readable text version
30 text_file = output_file.replace('.json', '.txt')
31 with open(text_file, 'w') as f:
32 for entry in transcript:
33 timestamp = entry['start']
34 text = entry['text']
35 f.write(f"[{timestamp:.2f}s] {text}\n")
36
37 print(f"Text version saved to {text_file}")
38
39 # Print summary
40 total_duration = transcript[-1]['start'] + transcript[-1]['duration']
41 print(f"\nTranscript stats:")
42 print(f" - Entries: {len(transcript)}")
43 print(f" - Duration: {total_duration:.2f}s ({total_duration/60:.2f} min)")
44 print(f" - Total text length: {sum(len(e['text']) for e in transcript)} characters")
45
46 return transcript
47
48 except Exception as e:
49 print(f"Error: {e}")
50 return None
51
52
53def main():
54 parser = argparse.ArgumentParser(
55 description="Download YouTube video transcript"
56 )
57 parser.add_argument(
58 "video_id",
59 help="YouTube video ID (e.g., 'xAji90wPgec') or URL"
60 )
61 parser.add_argument(
62 "-o", "--output",
63 help="Output JSON file (default: {video_id}_transcript.json)"
64 )
65
66 args = parser.parse_args()
67
68 # Extract video ID from URL if needed
69 video_id = args.video_id
70 if 'youtube.com' in video_id or 'youtu.be' in video_id:
71 if 'v=' in video_id:
72 video_id = video_id.split('v=')[1].split('&')[0]
73 elif 'youtu.be/' in video_id:
74 video_id = video_id.split('youtu.be/')[1].split('?')[0]
75
76 download_transcript(video_id, args.output)
77
78
79if __name__ == "__main__":
80 main()