backend/utils/ytdlp.js

37 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-03-03 16:44:40 +00:00
const child_process = require('child_process')
2023-09-30 15:32:32 +00: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-10-04 14:17:51 +00:00
const child = child_process.spawn("../yt-dlp", ['--proxy http://gluetun:8888', url], {cwd: 'videos', shell: false})
2023-10-04 13:20:59 +00:00
// https://github.com/yt-dlp/yt-dlp/blob/cc8d8441524ec3442d7c0d3f8f33f15b66aa06f3/README.md?plain=1#L1500
2023-03-03 16:44:40 +00:00
child.stdout.on("data", data => {
const msg = data.toString().trim()
if (!msg) return
2023-09-30 15:32:32 +00: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 16:55:10 +00:00
fail: true
2023-03-03 16:44:40 +00:00
})
} else {
resolve({
fail: false
})
}
})
})
}
module.exports = { downloadVideo }