backend/utils/ytdlp.js

36 lines
1011 B
JavaScript
Raw Normal View History

2023-03-03 16:44:40 +00:00
const child_process = require('child_process')
2023-09-30 16:32:32 +01:00
const DOMPurify = require('isomorphic-dompurify')
2023-03-03 16:44:40 +00:00
async function downloadVideo(url, ws) {
return new Promise((resolve, reject) => {
2023-09-15 17:55:10 +01:00
const child = child_process.spawn("../yt-dlp", [url], {cwd: 'videos', shell: false})
2023-03-03 16:44:40 +00:00
child.stdout.on("data", data => {
const msg = data.toString().trim()
if (!msg) return
2023-09-30 16:32:32 +01:00
if (ws) ws.send(`DATA - ${DOMPurify.sanitize(msg)}`)
})
child.stderr.on("data", data => {
const msg = data.toString().trim()
if (!msg) return
if (ws) ws.send(`DATA - ${DOMPurify.sanitize(msg)}`)
2023-03-03 16:44:40 +00:00
})
child.on("close", async (code, signal) => {
if (code == 2) {
reject({
2023-09-15 17:55:10 +01:00
fail: true
2023-03-03 16:44:40 +00:00
})
} else {
resolve({
fail: false
})
}
})
})
}
module.exports = { downloadVideo }