Skip to content

Commit

Permalink
Support audio episodes
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Sep 22, 2022
1 parent c1d5c26 commit ad0d145
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
36 changes: 30 additions & 6 deletions lib/run-yt-dlp.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,34 @@ try {
}
}

const videoFormat = 'best[ext=mp4]/best[ext=mov]/mp4/mov'
const audioFormat = 'ba[ext=m4a]/ba[ext=mp4]/ba[ext=mp3]/mp3/m4a'

function getFormatArg (medium) {
if (!['video', 'audio'].includes(medium)) throw new Error('format must be video or audio')

const formatOpts = medium === 'video'
? [videoFormat, audioFormat].join('/')
: medium === 'audio'
? [audioFormat, videoFormat].join('/')
: null

if (!formatOpts) throw new Error('No format options generated. Please report this bug')

return formatOpts
}

export async function getYTDLPUrl ({
url
url,
medium
}) {
const formatOpts = getFormatArg(medium)

const ytDlpWrap = new YTDlpWrap(binPath)
const args = [
url,
'-f',
'best[ext=mp4]/ba[ext=m4a]'
formatOpts
]

const metadata = await ytDlpWrap.getVideoInfo(args)
Expand All @@ -49,6 +69,7 @@ export function runYTDLP ({
userId,
bookmarkId,
episodeId,
medium,
pg,
log
}) {
Expand All @@ -63,24 +84,27 @@ export function runYTDLP ({
const bookmark = bookmarkResults.rows[0]
const { url } = bookmark
if (!bookmark?.url) throw new Error(`Error looking up bookmark for ${bookmarkId} while populating episode ${episodeId}`)

const formatOpts = getFormatArg(medium)
const args = [
url,
'-f',
'best[ext=mp4]/ba[ext=m4a]'
formatOpts
]

try {
const metadata = await ytDlpWrap.getVideoInfo(args)
console.dir(metadata, { colors: true, depth: 999 })
const videoData = []

videoData.push(SQL`ready = true`)
videoData.push(SQL`url = ${url}`)
if (metadata.filesize_approx != null) videoData.push(SQL`size_in_bytes = ${metadata.filesize_approx}`)
if (metadata.duration != null) videoData.push(SQL`duration_in_seconds = ${metadata.duration}`)
if (metadata.filesize_approx != null) videoData.push(SQL`size_in_bytes = ${Math.round(metadata.filesize_approx)}`)
if (metadata.duration != null) videoData.push(SQL`duration_in_seconds = ${Math.round(metadata.duration)}`)
if (metadata.channel != null) videoData.push(SQL`author_name = ${metadata.channel}`)
if (metadata.filename != null) videoData.push(SQL`filename = ${metadata.filename}`)
if (metadata.ext != null) videoData.push(SQL`ext = ${metadata.ext}`)
if (metadata._type != null) videoData.push(SQL`src_type = ${metadata._type}`)
if (metadata._type != null) videoData.push(SQL`src_type = ${['mp3', 'm4a'].includes(metadata.ext) ? 'audio' : ['mp4', 'mov', 'm3u8'].includes(metadata.ext) ? 'video' : metadata._type}`)

const query = SQL`
update episodes
Expand Down
3 changes: 2 additions & 1 deletion routes/api/bookmarks/_id/put-bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export async function putBookmark (fastify, opts) {
}

if (bookmark?.createEpisode) {
const { id: episodeId } = await createEpisode({
const { id: episodeId, medium: episodeMedium } = await createEpisode({
client,
userId,
bookmarkId,
Expand All @@ -117,6 +117,7 @@ export async function putBookmark (fastify, opts) {
userId,
bookmarkId,
episodeId,
medium: episodeMedium,
pg: fastify.pg,
log: request.log
})).then(() => fastify.metrics.episodeCounter.inc()).catch(request.log.error)
Expand Down
3 changes: 2 additions & 1 deletion routes/api/bookmarks/put-bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export async function putBookmarks (fastify, opts) {
}

if (request?.body?.createEpisode) {
const { id: episodeId } = await createEpisode({
const { id: episodeId, medium: episodeMedium } = await createEpisode({
client,
userId,
bookmarkId: bookmark.id,
Expand All @@ -127,6 +127,7 @@ export async function putBookmarks (fastify, opts) {
userId,
bookmarkId: bookmark.id,
episodeId,
medium: episodeMedium,
pg: fastify.pg,
log: request.log
}))
Expand Down
2 changes: 1 addition & 1 deletion routes/api/episodes/create-episode-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function createEpisode ({
const createEpisodeQuery = SQL`
INSERT INTO episodes (owner_id, podcast_feed_id, bookmark_id, type, medium)
VALUES (${userId}, ${defaultFeedId}, ${bookmarkId}, ${type}, ${medium})
returning id;
returning id, type, medium, podcast_feed_id;
`

const episodeResults = await client.query(createEpisodeQuery)
Expand Down
2 changes: 1 addition & 1 deletion routes/api/feeds/_feed/episode/_episode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default async function podcastFeedsRoutes (fastify, opts) {
reply.header('fly-cache-status', 'MISS')
}

const metadata = await fastify.pqueue.add(() => getYTDLPUrl({ url: episode.src_url }))
const metadata = await fastify.pqueue.add(() => getYTDLPUrl({ url: episode.src_url, medium: episode.medium }))
cache.set(cacheKey, metadata.urls, metadata.urls)
reply.redirect(302, metadata.urls)
}
Expand Down

0 comments on commit ad0d145

Please sign in to comment.