Skip to content

Commit

Permalink
feat(media-provider): stream support
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 6, 2023
1 parent 4f60f1a commit 491bf28
Show file tree
Hide file tree
Showing 8 changed files with 144,983 additions and 56,769 deletions.
44 changes: 28 additions & 16 deletions packages/metascraper-media-provider/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,42 +68,54 @@ const isDownloadable = ({ url }) =>
new URL(url).searchParams.get('download') === '1'

const getFormatUrls =
({ orderBy }) =>
(input, filters) => {
(basicFilters, { orderBy }) =>
(input, extraFilters, { isStream }) => {
const filters = extraFilters.concat(basicFilters({ isStream }))

const formats = get(input, 'formats') ||
get(input, 'entries[0].formats') || [input]

const url = chain(formats)
.filter(overEvery(filters))
.orderBy(orderBy, 'asc')
.map('url')
.map(isStream ? 'manifest_url' : 'url')
.last()
.value()

return !isEmpty(url) ? url : undefined
}

const getVideoUrls = getFormatUrls({ orderBy: 'tbr' })

const getAudioUrls = getFormatUrls({ orderBy: 'abr' })
const VIDEO_FILTERS = ({ isStream }) => {
const filters = [
hasVideo,
isMp4,
isHttps,
negate(isDownloadable),
!isStream ? negate(isM3u8) : undefined,
negate(isMpd),
hasVideoCodec
].filter(Boolean)
filters.isStream = isStream
return filters
}

const VIDEO_FILTERS = [
hasVideo,
isMp4,
const AUDIO_FILTERS = () => [
hasAudio,
isHttps,
negate(isDownloadable),
negate(isM3u8),
negate(isMpd),
hasVideoCodec
hasAudioCodec
]

const AUDIO_FILTERS = [hasAudio, isHttps, negate(isDownloadable), hasAudioCodec]
const getVideoUrls = getFormatUrls(VIDEO_FILTERS, { orderBy: 'tbr' })

const getAudioUrls = getFormatUrls(AUDIO_FILTERS, { orderBy: 'abr' })

const getVideo = data =>
getVideoUrls(data, [hasAudioCodec, ...VIDEO_FILTERS]) ||
getVideoUrls(data, VIDEO_FILTERS)
getVideoUrls(data, [hasAudioCodec], { isStream: false }) ||
getVideoUrls(data, [], { isStream: false }) ||
getVideoUrls(data, [], { isStream: true })

const getAudio = data => getAudioUrls(data, AUDIO_FILTERS)
const getAudio = data => getAudioUrls(data, [], { isStream: false })

const getAuthor = ({ uploader, creator, uploader_id: uploaderId }) =>
find([creator, uploader, uploaderId], str => authorFn(str))
Expand Down
5 changes: 3 additions & 2 deletions packages/metascraper-media-provider/test/fixtures/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ const fs = require('fs/promises')

const { getFlags } = require('../../src/get-media/provider/generic')

const [, , url, filename] = process.argv
let [, , url, filename] = process.argv

if (!url) throw new TypeError('process.argv[2] should be an url')
if (!filename) throw new TypeError('process.argv[3] should be a filename')
if (!filename.endsWith('.json')) filename += '.json'
;(async () => {
const flags = getFlags({ url })
const payload = await youtubedl(url, flags)
const filepath = path.resolve(__dirname, 'provider', `${filename}.json`)
const filepath = path.resolve(__dirname, 'provider', filename)
await fs.writeFile(filepath, JSON.stringify(payload, null, 2))
console.log('Done at', filepath, '✨')
})()

0 comments on commit 491bf28

Please sign in to comment.