uzair-mtx-sir-ytdl
Version:
A wrapper package for ytdl-core to simplify YouTube video and audio downloading.
126 lines (83 loc) • 4.38 kB
Markdown
# uzairsirmtx-ytdl-core
`uzairsirmtx-ytdl-core` is a simplified wrapper around the popular `ytdl-core` library, designed to make YouTube video and audio downloading easier to integrate into your Node.js applications. It provides a clean, class-based interface for common `ytdl-core` functionalities.
## Features
- **Simplified API**: Easy-to-use methods for common `ytdl-core` tasks.
- **Video Information**: Retrieve detailed information about YouTube videos.
- **Video & Audio Downloading**: Stream YouTube videos or extract only audio.
- **URL Validation**: Helper function to validate YouTube URLs.
## Installation
To install the package, use npm:
```bash
npm install uzairsirmtx-ytdl-core
```
**Required Dependencies:**
- `ytdl-core` (v4.x or higher)
## Usage
Here's how to use `UzairsirmtxYTDL` in your Node.js application:
```javascript
const UzairsirmtxYTDL = require('uzairsirmtx-ytdl-core');
const fs = require('fs');
const ytdlClient = new uzairsirmtx();
const videoUrl = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'; // Example YouTube URL
async function exampleUsage() {
try {
// Get video information
const info = await ytdlClient.getVideoInfo(videoUrl);
console.log('Video Title:', info.videoDetails.title);
console.log('Video Author:', info.videoDetails.author.name);
// Download video (example: to a file)
console.log('Downloading video...');
ytdlClient.downloadVideo(videoUrl)
.pipe(fs.createWriteStream('video.mp4'))
.on('finish', () => console.log('Video downloaded!'))
.on('error', err => console.error('Video download error:', err.message));
// Download audio (example: to a file)
console.log('Downloading audio...');
ytdlClient.downloadAudio(videoUrl)
.pipe(fs.createWriteStream('audio.mp3'))
.on('finish', () => console.log('Audio downloaded!'))
.on('error', err => console.error('Audio download error:', err.message));
// Validate URL
console.log('Is valid URL?', ytdlClient.isValidUrl(videoUrl));
console.log('Is invalid URL?', ytdlClient.isValidUrl('https://not-a-youtube-url.com'));
// Get video ID
console.log('Video ID:', ytdlClient.getVideoID(videoUrl));
} catch (error) {
console.error('An error occurred:', error.message);
}
}
exampleUsage();
```
### `UzairsirmtxYTDL` Class
#### `constructor()`
Initializes the `UzairsirmtxYTDL` wrapper.
#### `getVideoInfo(url)`
Gets detailed information about a YouTube video.
- `url` (string): The URL of the YouTube video.
- Returns: `Promise<Object>` - A promise that resolves with video information (see `ytdl-core` documentation for structure).
- Throws: `Error` if the URL is invalid or an error occurs during fetching.
#### `downloadVideo(url, options)`
Downloads a YouTube video stream.
- `url` (string): The URL of the YouTube video.
- `options` (Object, optional): Options to pass directly to `ytdl-core` (e.g., `quality`, `filter`).
- Returns: `ReadableStream` - A readable stream of the video content.
- Throws: `Error` if the URL is invalid or an error occurs during streaming.
#### `downloadAudio(url, options)`
Downloads a YouTube audio stream (audio-only).
- `url` (string): The URL of the YouTube video.
- `options` (Object, optional): Options to pass directly to `ytdl-core`.
- Returns: `ReadableStream` - A readable stream of the audio content.
- Throws: `Error` if the URL is invalid or an error occurs during streaming.
#### `isValidUrl(url)`
Checks if a given string is a valid YouTube video URL.
- `url` (string): The string to validate.
- Returns: `boolean` - `true` if the URL is a valid YouTube video URL, `false` otherwise.
#### `getVideoID(url)`
Extracts the video ID from a YouTube URL.
- `url` (string): The URL of the YouTube video.
- Returns: `string|null` - The video ID if valid, otherwise `null`.
## Important Notes
- `ytdl-core` might require `ffmpeg` for certain functionalities (e.g., combining audio and video streams if you download them separately). Ensure you have it installed if needed for your specific use case.
- Always ensure you are complying with YouTube's terms of service when using this package.
## License
This project is licensed under the ISC License.