On this page
One transcript request gives you everything for plain text, Markdown and SRT. Plain text is already in the response; Markdown and SRT take a few lines of code, so you control how they look.
Ask for lines so you have caption times:
# pip install cleanscript-ai
from cleanscript_ai import CleanScript
client = CleanScript()
result = client.transcript("https://youtu.be/jwnez8HdN7E", include=["lines"])Plain text #
open("transcript.txt", "w").write(result.text)text is the paragraphs separated by blank lines, with punctuation and capitals.
Markdown with timestamp links #
A heading per section, linked to its start, with the paragraphs under it:
def stamp(seconds):
m, s = divmod(int(seconds), 60)
return f"{m}:{s:02}"
post = result.post
out = [f"# {post.title or post.description}", f"By {post.author.name}. [Watch]({post.url})", ""]
for section in result.sections:
out.append(f"## [{stamp(section.start)}]({section.url}) {section.title or ''}".strip())
out += [p.text + "\n" for p in section.paragraphs]
open("transcript.md", "w").write("\n".join(out))post.title exists only on YouTube; TikTok and Instagram have a caption instead, in post.description. On those platforms a section's url is the post link, since their links can't open at a moment.
SRT subtitles #
SRT numbers each block and gives it a start and end as hh:mm:ss,mmm:
def srt_time(seconds):
ms = 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}"
blocks = (
f"{i}\n{srt_time(line.start)} --> {srt_time(line.end)}\n{line.text}\n"
for i, line in enumerate(result.lines, 1)
)
open("subtitles.srt", "w").write("\n".join(blocks))Run on Le Monde's TikTok about its explainer videos, the file starts:
1
00:00:00,080 --> 00:00:04,560
Comment réalisons nos vidéos verticales au Monde ? Bienvenue dans notre open space.
2
00:00:04,680 --> 00:00:08,880
C'est ici qu'on fabrique les vidéos d'explication qui sont publiées sur les réseaux sociaux du journal
3
00:00:09,040 --> 00:00:12,760
le monde. On existe depuis deux-mille-seize, et avant ça ressemblait à ça,Lines are caption-sized, so they work as subtitles as they are. For shorter blocks on screen, split long lines at sentence ends.
Before you publish subtitles #
Corrected automatic captions still contain some errors, so read the file once before you upload it. If the video isn't yours, keep the file for your own use.
Next steps #
- What the lines contain: YouTube transcript as JSON with timestamps.
- What the correction fixes: clean up YouTube auto captions.
Spot something wrong? Tell us.