forked from alexandreteles/jmusicbot-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutubeHandler.js
82 lines (70 loc) · 2.45 KB
/
youtubeHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const ytdl = require('@distube/ytdl-core');
const yts = require('yt-search');
const ffmpeg = require('fluent-ffmpeg');
const path = require('path');
const fs = require('fs');
const MUSIC_DIR = path.join(__dirname, 'music');
function ensureMusicDir() {
if (!fs.existsSync(MUSIC_DIR)) {
fs.mkdirSync(MUSIC_DIR, { recursive: true });
}
}
async function downloadYoutubeAudio(videoUrl, filename) {
return new Promise(async (resolve, reject) => {
console.log('Starting download process for:', filename);
ensureMusicDir();
const outputPath = path.join(MUSIC_DIR, `${filename}.mp3`);
if (fs.existsSync(outputPath)) {
console.log('File already exists:', outputPath);
resolve(outputPath);
return;
}
try {
console.log('Getting video info');
const info = await ytdl.getInfo(videoUrl);
const format = ytdl.chooseFormat(info.formats, { quality: 'highestaudio' });
console.log('Starting download with format:', format.mimeType);
const videoStream = ytdl(videoUrl, { format: format });
const ffmpegProcess = ffmpeg(videoStream)
.toFormat('mp3')
.audioBitrate('192k')
.on('end', () => {
console.log('Processing finished');
resolve(outputPath);
})
.on('error', (err) => {
console.error('Error:', err);
reject(err);
});
ffmpegProcess.save(outputPath);
} catch (error) {
console.error('Error in download process:', error);
if (fs.existsSync(outputPath)) {
fs.unlinkSync(outputPath);
}
reject(error);
}
});
}
async function searchYoutube(query) {
try {
console.log('Searching YouTube for:', query);
const results = await yts(query);
const videos = results.videos.slice(0, 10).map(video => ({
title: video.title,
url: video.url,
duration: video.duration.timestamp,
author: video.author.name
}));
console.log(`Found ${videos.length} results`);
return videos;
} catch (error) {
console.error('YouTube search error:', error);
throw error;
}
}
module.exports = {
searchYoutube,
downloadYoutubeAudio,
MUSIC_DIR
};