# @onepunya/yt-converter-js
A simple Node.js class to interact with YouTube video search and conversion services.
## Installation
To use this package, you need to have Node.js installed. You can install this package via npm:
```bash
npm install @onepunya/yt-converter-js
Usage
Importing the Class
import Youtube from '@onepunya/yt-converter-js';
or
const Youtube = require('@onepunya/yt-converter-js');
Creating an Instance
const youtube = new Youtube();
Searching for Videos
Use the search
method to search for videos based on a query.
const query = 'Kokoronashi';
youtube.search(query)
.then(result => {
console.log(result);
})
.catch(error => {
console.error('Error:', error);
});
Parameters:
query
(string): The search query string.
Returns:
A promise that resolves to an object with the following structure:
{
"status": "ok",
"message": "",
"query": "faded",
"items": [
{
"videoId": "60ItHLz5WEA",
"title": "Alan Walker - Faded",
"youtubeUrl": "https://youtu.be/60ItHLz5WEA"
},
{
"videoId": "jlKOSk-tXFQ",
"title": "Alan Walker - Fade (Official Music Video)",
"youtubeUrl": "https://youtu.be/jlKOSk-tXFQ"
}
],
"platform": "YouTube"
}
Converting Videos
Use the download
method to convert a YouTube video to MP3 or MP4.
const videoUrl = 'https://www.youtube.com/watch?v=video_id';
youtube.download(videoUrl)
.then(result => {
console.log(result);
})
.catch(error => {
console.error('Error:', error);
});
Parameters:
videoUrl
(string): The URL of the YouTube video.
Returns:
A promise that resolves to an object with the following structure:
{
"title": "video_title",
"thumbnail": [],
"mp4DownloadLink": "link_to_mp4_file",
"mp3DownloadLink": "link_to_mp3_file"
}
Error Handling
Both methods return promises that will be rejected if an error occurs. Handle errors using .catch
in your promise chain.
License
This project is licensed under the MIT License.
Follow Me