-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (107 loc) · 3.92 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const fs = require('fs');
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
const transliteration = require('transliteration');
const ffmpeg = require('fluent-ffmpeg');
const inputFolderPath = './input'; // Path to the input folder
const downloadMP3 = async (videoUrl, searchQuery, outputFolder) => {
try {
const info = await ytdl.getInfo(videoUrl);
const format = ytdl.filterFormats(info.formats, 'audioonly').find((f) => f.container === 'mp4');
if (!format) {
console.log(`No audio format available for search query: ${searchQuery}`);
return;
}
const outputFileName = getOutputFileName(info.videoDetails.title);
const output = `${outputFolder}/${outputFileName}`;
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder, { recursive: true });
}
options = {
quality: 'highest',
filter: 'audioonly',
};
const videoStream = ytdl(videoUrl, options);
videoStream.pipe(fs.createWriteStream(output));
videoStream.on('end', () => {
console.log(`Download complete for search query: ${searchQuery}`);
ffmpeg()
.input(output)
.audioCodec('libmp3lame')
.toFormat('mp3')
.on('end', () => {
console.log('-------------------------------------');
console.log(`Converted to MP3: ${output}`);
fs.unlink(output, (err) => {
if (err) {
console.error('Error deleting file:', err.message);
return;
}
console.log(`File ${output} deleted successfully`);
console.log('-------------------------------------');
});
})
.on('error', (err) => {
console.error('Error converting to MP3:', err);
})
.save(`${outputFolder}/${getOutputFileName(info.videoDetails.title, 'mp3')}`);
});
videoStream.on('error', (error) => {
console.error(`Error downloading file for search query '${searchQuery}':`, error.message);
});
} catch (error) {
console.error(`Error for search query '${searchQuery}':`, error.message);
}
};
const processInputFile = async (file) => {
const fileContent = fs.readFileSync(file, 'utf-8');
const searchQueries = fileContent.split('\n').filter((query) => query.trim() !== '');
const outputFolder = `./output/${getOutputFolderName(file)}`;
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder, { recursive: true });
}
for (const searchQuery of searchQueries) {
try {
const { videos } = await ytSearch(searchQuery);
if (videos.length > 0) {
const videoUrl = `https://www.youtube.com/watch?v=${videos[0].videoId}`;
await downloadMP3(videoUrl, searchQuery, outputFolder);
} else {
console.log(`No search results found for query: ${searchQuery}`);
}
} catch (error) {
console.error(`Error for search query '${searchQuery}':`, error.message);
}
}
};
const getOutputFolderName = (inputFile) => {
const fileName = inputFile.replace(/^.*[\\/]/, '').replace('.txt', '');
return transliterateText(fileName);
};
const sanitizeFileName = (fileName) => {
return fileName.replace(/[_|/\\]+|[^\w\s-|/\\]/gi, '');
};
const transliterateText = (text) => {
return transliteration.transliterate(text);
};
const getOutputFileName = (videoTitle, format = 'mp4') => {
const transliteratedTitle = transliterateText(videoTitle);
const sanitizedTitle = sanitizeFileName(transliteratedTitle);
return `${sanitizedTitle}.${format}`;
};
const processInputFiles = async () => {
fs.readdir(inputFolderPath, (err, files) => {
if (err) {
console.error('Error reading input folder:', err.message);
return;
}
for (const file of files) {
if (file.endsWith('.txt')) {
const filePath = `${inputFolderPath}/${file}`;
console.log(`Processing input file: ${filePath}`);
processInputFile(filePath);
}
}
});
};
processInputFiles();