Skip to content

Instantly share code, notes, and snippets.

@considine
Created December 24, 2022 19:13
Embed
What would you like to do?
Transcribes a video saved in a Notion Database, and saves the transcription to the page
#!/usr/bin/env zx
const body = await fetch(
`https://api.notion.com/v1/databases/${process.env.NOTION_DATABASE_ID}/query`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.NOTION_API_KEY}`,
"Notion-Version": "2022-06-28",
"Content-Type": "application/json",
},
body: JSON.stringify({}),
}
).then((r) => r.json());
const data = body.results
.map((result) => {
const { id, properties } = result;
const videoUrl = properties["Video URL"].url;
const fileName = videoUrl.split("/").reverse()[0].split("?")[0];
return { id, videoUrl, fileName };
})
.slice(11, 12); // only do a few at a time
// get output of video
for await (let { videoUrl, fileName, id } of data) {
await $`curl -L ${videoUrl} --output ${fileName}`;
const r = await $`whisper ${fileName} --language en`;
const resp = await fetch(`https://api.notion.com/v1/blocks/${id}/children`, {
method: "PATCH",
headers: {
Authorization: `Bearer ${process.env.NOTION_API_KEY}`,
"Content-Type": "application/json",
"Notion-Version": "2022-02-22",
},
body: JSON.stringify({
children: [
{
object: "block",
type: "heading_1",
heading_1: {
rich_text: [
{
type: "text",
text: {
content: "Transcription",
},
annotations: {},
},
],
},
},
...r._stdout.split("\n").map((line) => ({
object: "block",
type: "paragraph",
paragraph: {
rich_text: [
{
type: "text",
text: {
content: line,
},
annotations: {
bold: false,
},
},
],
},
})),
],
}),
}).then((resp) => resp.json());
}
@considine
Copy link
Author

To run, install:

And then:

export NOTION_DATABASE_ID="" # the ID of the videos database 
export NOTION_API_KEY="" # get from the Notion API https://www.notion.so/my-integrations

chmod +x ./transcriber.mjs
./transcriber.mjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment