Repository navigation
Expand file tree
/
Copy pathtranscript.py
More file actions
87 lines (71 loc) · 3.44 KB
/
Copy pathtranscript.py
File metadata and controls
87 lines (71 loc) · 3.44 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
"""Download a YouTube video's transcript as plain text and as an .srt subtitle file.
pip install requests
export SEMUST_API_USER=... SEMUST_API_PASSWORD=...
python transcript.py "https://www.youtube.com/watch?v=_bewU9AvKwU"
python transcript.py _bewU9AvKwU --language en --translate-to de
python transcript.py _bewU9AvKwU --list # which transcript languages the video has
Each transcript or video lookup is billed as one video. Errors are not charged.
"""
import argparse
import os
import sys
import time
import requests
API = "https://data.semust.com"
HEADERS = {
"SEMUST-API-USER": os.environ["SEMUST_API_USER"],
"SEMUST-API-PASSWORD": os.environ["SEMUST_API_PASSWORD"],
}
WAIT = 60
def call(path, payload):
resp = requests.post(f"{API}{path}", params={"wait": WAIT}, json=payload,
headers=HEADERS, timeout=WAIT + 30)
while resp.status_code == 202:
task = resp.json()
time.sleep(int(resp.headers.get("Retry-After", 5)))
resp = requests.get(task["poll_url"], headers=HEADERS, timeout=30)
return resp
def srt_time(seconds):
ms = int(round(seconds * 1000))
h, ms = divmod(ms, 3_600_000)
m, ms = divmod(ms, 60_000)
s, ms = divmod(ms, 1000)
return f"{h:02}:{m:02}:{s:02},{ms:03}"
def main():
ap = argparse.ArgumentParser()
ap.add_argument("video", help="11-character video ID or any YouTube video URL")
ap.add_argument("--country", default="US")
ap.add_argument("--language", default="en", help="transcript language, e.g. en, tr, en-GB")
ap.add_argument("--translate-to", help="translate the transcript into this language, e.g. de")
ap.add_argument("--list", action="store_true", help="list the video's transcript languages and exit")
args = ap.parse_args()
if args.list:
resp = call("/v1/youtube", {"type": "video", "video_id": args.video, "country": args.country})
if resp.status_code != 200:
sys.exit(f"Error {resp.status_code}: {resp.text}")
video = resp.json()["data"]
print(f"{video['title']}\n")
for track in video["transcripts"]:
print(f" {track['language']:<8} {'auto-generated' if track['auto_generated'] else 'uploaded'}")
return
payload = {"type": "transcript", "video_id": args.video, "country": args.country,
"transcript_language": args.language}
if args.translate_to:
payload["translate_to"] = args.translate_to
resp = call("/v1/youtube", payload)
if resp.status_code != 200:
err = resp.json() if resp.headers.get("Content-Type", "").startswith("application/json") else {}
if err.get("code") == "TRANSCRIPT_NOT_FOUND":
sys.exit(f"No {args.language!r} transcript. Run with --list to see the languages this video has.")
sys.exit(f"Error {resp.status_code}: {resp.text}")
data = resp.json()["data"]
name = f"transcript_{data['video_id']}_{data['translated_to'] or data['language']}"
with open(f"{name}.txt", "w", encoding="utf-8") as f:
f.write(data["text"] + "\n")
with open(f"{name}.srt", "w", encoding="utf-8") as f:
for i, seg in enumerate(data["segments"], 1):
f.write(f"{i}\n{srt_time(seg['start'])} --> {srt_time(seg['end'])}\n{seg['text']}\n\n")
print(f"{data['title'] or data['video_id']}: {len(data['text'].split()):,} words, {len(data['segments'])} segments")
print(f"Saved {name}.txt and {name}.srt")
if __name__ == "__main__":
main()