uzair-mtx-sir-ytdl
Version:
A wrapper package for ytdl-core to simplify YouTube video and audio downloading.
88 lines (80 loc) • 2.7 kB
JavaScript
const ytdl = require('ytdl-core');
class UzairsirmtxYTDL {
/**
* Initializes the UzairsirmtxYTDL wrapper.
*/
constructor() {
console.log('[UzairsirmtxYTDL] Initialized.');
}
/**
* Gets information about a YouTube video.
* @param {string} url - The URL of the YouTube video.
* @returns {Promise<Object>} - A promise that resolves with video information.
*/
async getVideoInfo(url) {
if (!ytdl.validateURL(url)) {
throw new Error('Invalid YouTube URL provided.');
}
try {
const info = await ytdl.getInfo(url);
return info;
} catch (error) {
console.error(`[UzairsirmtxYTDL] Error getting video info for ${url}: ${error.message}`);
throw error;
}
}
/**
* Downloads a YouTube video stream.
* @param {string} url - The URL of the YouTube video.
* @param {Object} options - Options for ytdl-core download.
* @returns {ReadableStream} - A readable stream of the video.
*/
downloadVideo(url, options = {}) {
if (!ytdl.validateURL(url)) {
throw new Error('Invalid YouTube URL provided.');
}
try {
return ytdl(url, options);
} catch (error) {
console.error(`[UzairsirmtxYTDL] Error downloading video for ${url}: ${error.message}`);
throw error;
}
}
/**
* Downloads a YouTube audio stream.
* @param {string} url - The URL of the YouTube video.
* @param {Object} options - Options for ytdl-core download.
* @returns {ReadableStream} - A readable stream of the audio.
*/
downloadAudio(url, options = {}) {
if (!ytdl.validateURL(url)) {
throw new Error('Invalid YouTube URL provided.');
}
try {
return ytdl(url, { filter: 'audioonly', ...options });
} catch (error) {
console.error(`[UzairsirmtxYTDL] Error downloading audio for ${url}: ${error.message}`);
throw error;
}
}
/**
* Checks if a given string is a valid YouTube video URL.
* @param {string} url - The string to validate.
* @returns {boolean} - True if valid, false otherwise.
*/
isValidUrl(url) {
return ytdl.validateURL(url);
}
/**
* Gets the video ID from a YouTube URL.
* @param {string} url - The URL of the YouTube video.
* @returns {string|null} - The video ID or null if invalid.
*/
getVideoID(url) {
if (!ytdl.validateURL(url)) {
return null;
}
return ytdl.getURLVideoID(url);
}
}
module.exports = UzairsirmtxYTDL;