Repository navigation
Expand file tree
/
Copy pathreel.py
More file actions
61 lines (50 loc) · 2.55 KB
/
Copy pathreel.py
File metadata and controls
61 lines (50 loc) · 2.55 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
"""Transcribe one public Instagram Reel or video post with mode auto and a spend cap.
Cost: AI transcription, 2 credits per started minute with a 60 second minimum.
MAX_CREDITS (default 20) caps it: 20 covers up to 10 minutes. Longer media fails
with BUDGET_EXCEEDED and nothing is charged. Paid plans only.
captions_only is not supported for Instagram (422 CAPABILITY_UNAVAILABLE).
Setup: python -m pip install transcriptdock
Run: TRANSCRIPTDOCK_API_KEY=td_live_... python reel.py INSTAGRAM_URL [MAX_CREDITS]
"""
import os
import sys
from uuid import uuid4
from transcriptdock import ApiError, JobTimeoutError, TranscriptDock
if len(sys.argv) < 2:
sys.exit("Usage: python reel.py INSTAGRAM_URL [MAX_CREDITS]")
url = sys.argv[1]
max_credits = int(sys.argv[2]) if len(sys.argv) > 2 else 20
client = TranscriptDock(os.environ["TRANSCRIPTDOCK_API_KEY"])
# Generate once per intended operation. Save this key and the returned job ID.
# Reuse the same key and exact body if a submission's response is lost.
submission_key = str(uuid4())
print("Idempotency-Key:", submission_key, file=sys.stderr)
try:
job = client.create_job(
# Instagram has no caption path, so auto goes straight to AI transcription.
{"source": {"url": url}, "mode": "auto", "max_credits": max_credits},
idempotency_key=submission_key,
)
# AI transcription takes roughly a quarter of the media length.
job = client.wait_for_job(job["id"], timeout=900, poll_interval=5)
except JobTimeoutError as error:
sys.exit(f"Still running: {error.job}")
except ApiError as error:
if error.code == "PLAN_REQUIRED":
print("Instagram needs a paid plan: https://www.transcriptdock.com/pricing", file=sys.stderr)
sys.exit(f"{error.code} {error.status} request {error.request_id}")
if job["status"] != "succeeded":
code = (job.get("error") or {}).get("code")
if code == "BUDGET_EXCEEDED":
sys.exit("Longer than the cap allows. Nothing was charged.")
sys.exit(f"{job['status']}: {job.get('error')}")
transcript = client.get_transcript(job["result_id"])
recognition = transcript.get("recognition") or {}
print(f"{transcript['duration_seconds']} s, language {transcript['language']}, "
f"quality {recognition.get('quality_status')}, "
f"{job['billing']['credits_charged']} credits charged", file=sys.stderr)
# Word timing straight from the transcript, in seconds. Never estimate it.
for word in (transcript["words"] or [])[:20]:
print(f"{word['start']:.2f}s {word['text']}")
print()
print(client.export_transcript(job["result_id"], "srt"))