@dada78641/ytdlpdata
v0.0.1
Published
Helper library for running yt-dlp to fetch JSON info
Downloads
30
Readme
@dada78641/ytdlpdata
This is a very basic library designed for personal purposes. Feel free to use it, but don't necessarily expect updates or support.
A basic library for fetching video metadata using yt-dlp. This library is designed to retrieve structured and typed information from video URLs but does not support video downloads. It runs yt-dlp with the --dump-json flag (among various other options) and returns the output as typed objects.
The yt-dlp output data is somewhat unpredictable due to feature differences between video hosting sites, and the fact that individual videos can differ in numerous ways. As a result of that, the TypeScript types are not entirely correct.
Usage
To start making calls, instantiate a YtDlpData class:
import {YtDlpData, type YtDlpVideoResult} from '../src/index.ts'
// Instantiate a new yt-dlp instance. This will not spawn a process just yet.
// Here you can pass the path to yt-dlp and set default arguments.
const ytDlp = new YtDlpData()
// Get a private video that my logged in user has access to.
const privateVideo = await ytDlp.getVideoData('https://www.youtube.com/watch?v=GsBeCEQwEEI', ['--cookies-from-browser', 'firefox'])
// Get the 10 latest videos for a particular channel, with a short delay to avoid rate limiting.
const latestVideos = await ytDlp.getPlaylistData('https://www.youtube.com/@CoachPupilLeague/videos', ['--playlist-items', '1-10', '--sleep-requests', '5'])
// Get only basic info about the 10 latest items. This completes much faster since it only needs to process a single HTML page.
const latestVideosFlat = await ytDlp.getPlaylistData('https://www.youtube.com/@CoachPupilLeague/videos', ['--playlist-items', '1-10', '--flat-playlist'])
// Retrieve only channel data, without any video info.
const channelInfo = await ytDlp.getChannelData('https://www.youtube.com/@CoachPupilLeague')
// Request yt-dlp to self-update. This returns an update information object.
const updateInfo = await ytDlp.updateYtDlp()
// Call --dump-json and emit payloads as they come in.
function getSeveralVideos() {
const payloads: YtDlpVideoResult[] = []
return new Promise(async (resolve, reject) => {
const call = await ytDlp.createVideoEmitter('https://ch.sooplive.co.kr/scv6256/vods/review')
call.on('log', (message: string) => {
// If --verbose is set, messages will be logged here.
console.log('log', message)
})
call.on('payload', (payload: YtDlpVideoResult) => {
// The payload is a single video (or a single part of a video, if it is really a playlist item).
// Process your payload here, or save them up in an array to resolve later, as we're doing here.
payloads.push(payload)
// If you have sufficient video results, you can exit the process and resolve.
call.abort()
resolve(payloads)
})
call.on('close', (code: number | null, signal: NodeJS.Signals | null) => {
// Called on process close.
console.log('close', code, signal)
resolve(payloads)
})
call.on('error', (err: Error) => {
reject(err)
})
})
}Note that some calls will take a very long time to complete. Make sure to set sensible limits.
License
MIT licensed.
