Skip to content

Instantly share code, notes, and snippets.

@ScottJWalter
Forked from ll14m4n/yt_video_url.md.txt
Last active January 26, 2024 08:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ScottJWalter/3d6873ba592675d443a67b494e528d15 to your computer and use it in GitHub Desktop.
Save ScottJWalter/3d6873ba592675d443a67b494e528d15 to your computer and use it in GitHub Desktop.
obsidian youtube templater
<%"---"%>
created: <% tp.file.creation_date('YYYY-MM-DD HH:MM:ssSS') %>
updated: <% tp.file.creation_date('YYYY-MM-DD HH:MM:ssSS') %>
cloud_host: "pcloud"
tags:
- video
- youtube
<%*
/*
REMEMBER: Save this file as 'Video - YouTube.md' (strip the '.txt' extension)
when you save it to your templates folder. The '.txt' extension is added
only to force github to visually format the content better.
You need to install yt-dlp and jq to use this template:
on macOS:
$ brew install yt-dlp jq
on Windows, you can download the yt-dlp.exe and jq.exe files and put them
somewhere in your path.
You need to define user function in Templater plugin settings named
"ytmeta" with the following command
on linux:
<PATH1>/yt-dlp -j "https://www.youtube.com/watch?v=${id}" | <PATH2>/jq "${query}"
on Windows:
<PATH1>\yt-dlp -j "https://www.youtube.com/watch?v=%id%" | <PATH12\jq "%query%"
where:
<PATH1> - Path to the yt-dlp executable
<PATH2> - Path to the jq executable
*/
const query = `{
title : .title,
thumbnail : .thumbnail,
duration_seconds : .duration,
upload_date : .upload_date,
description : .description,
chapters : .chapters,
channel_name : .channel,
uploader_id : .uploader_id
}`
format_duration = (duration_seconds) => {
const duration_ms = duration_seconds * 1000
duration_h = Math.floor(moment.duration(duration_ms).asHours()) || ""
duration_m_s = moment.utc(duration_ms).format("mm:ss")
return [duration_h, duration_m_s].filter(Boolean).join(":")
}
clean_obsidian_link = (name) => {
const filename_forbid_chars = /[*\/\\<>:|?"']/g
return name.replace(filename_forbid_chars, '_')
}
const url_input = await tp.system.prompt("Video URL")
const u = new URL(url_input);
const video_id = u.searchParams.get("v")
const meta_resp = await tp.user.ytmeta( { id: video_id, query: query } )
const meta = JSON.parse(meta_resp)
const title = meta.title
const thumbnail = meta.thumbnail
const duration = format_duration(meta.duration_seconds)
const ud = meta.upload_date
const upload_date = ud.slice(0, 4) + '-' + ud.slice(4, 6) + '-' + ud.slice(6, 8);
const description = meta.description
const canonical_url = `https://www.youtube.com/watch?v=${video_id}`
const filename = clean_obsidian_link('📺 ' + title)
const chapters_ar = meta.chapters || []
let chapters = ''
let notes = "## Notes\n"
if (chapters_ar.length > 0) {
chapters += chapters_ar.map((c) => {
const start = format_duration(c.start_time)
return `- [${c.title} (${start})](${canonical_url}&t=${c.start_time})`
}).join("\n")
notes += chapters_ar.map((c) => {
const start = format_duration(c.start_time)
return `### ${c.title}\n[(${start})](<${canonical_url}&t=${c.start_time}>)\n`
}).join("\n")
}
await tp.file.rename(filename)
tR += `
id: "${video_id}"
channel: "[[${meta.uploader_id}]]"
title: "${title}"
duration: ${duration}
publish_date: ${upload_date}
url: ${canonical_url}
---
![Thumbnail](${thumbnail})
[Watch (${duration})](${canonical_url})
${chapters}
## Description
\`\`\`
${description}
\`\`\`
${notes}
`
%>
@ScottJWalter
Copy link
Author

ScottJWalter commented Jan 25, 2024

REMEMBER: Save this file as 'Video - YouTube.md' (strip the '.txt' extension) when you save it to your templates folder. The '.txt' extension is added only to force github to visually format the content better.

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