MusicServer/utils/download.ts

34 lines
1.1 KiB
TypeScript

import { v4 } from 'uuid';
import shjs from 'shelljs';
import fs from 'fs';
import md5 from 'md5';
const process: Record<string, boolean> = {};
export default function downloadSong(id: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
const url: string = `https://music.youtube.com/watch?v=${id}`;
const filename: string = md5(id);
if (process[id]) {
reject(new Error(`Already downloading ${id}`));
return;
}
process[id] = true;
shjs.exec(`../yt-dlp.sh --sponsorblock-remove all -o ${filename} ${url}`, {
cwd: './tmpStore',
async: true
}).on('exit', () => {
const fileName = fs.readdirSync('./tmpStore').find(x => x.includes(filename));
const newFileName = `${filename}.mp3`;
shjs.exec(`ffmpeg -i '${fileName}' '${newFileName}'`, {
async: true,
cwd: './tmpStore'
}).on('exit', () => {
resolve(newFileName);
delete process[id];
});
});
});
}