2022-06-17 01:25:43 +02:00
|
|
|
import os from 'node:os';
|
|
|
|
import { exec } from 'node:child_process';
|
2022-09-14 11:32:01 +02:00
|
|
|
const { NODE_ENV } = process.env;
|
2022-06-17 01:25:43 +02:00
|
|
|
|
|
|
|
export default {
|
|
|
|
downloadVideo,
|
|
|
|
upload,
|
2022-06-17 02:14:14 +02:00
|
|
|
ffmpeg,
|
2022-06-20 08:34:18 +02:00
|
|
|
stringIsAValidurl,
|
2022-08-22 20:23:27 +02:00
|
|
|
compressVideo,
|
2023-03-20 03:55:53 +01:00
|
|
|
getVideoCodec,
|
2022-06-17 01:25:43 +02:00
|
|
|
};
|
2022-06-20 08:34:18 +02:00
|
|
|
async function downloadVideo(urlArg, output, format = 'bestvideo*+bestaudio/best') {
|
2022-06-17 01:25:43 +02:00
|
|
|
await new Promise((resolve, reject) => {
|
2023-04-10 14:48:26 +02:00
|
|
|
exec(`./bin/yt-dlp -f ${format} "${urlArg}" -o "${os.tmpdir()}/${output}.%(ext)s" --force-overwrites --no-playlist --remux-video=mp4/webm/mov`, (err, stdout, stderr) => {
|
2022-06-17 01:25:43 +02:00
|
|
|
if (err) {
|
|
|
|
reject(stderr);
|
|
|
|
}
|
|
|
|
if (stderr) {
|
|
|
|
console.error(stderr);
|
|
|
|
}
|
2022-09-14 11:32:01 +02:00
|
|
|
console.log(NODE_ENV === 'development' ? stdout : null);
|
2022-09-12 11:33:25 +02:00
|
|
|
resolve();
|
2022-06-17 01:25:43 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function upload(file) {
|
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
exec(`./bin/upload.sh ${file}`, (err, stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
reject(stderr);
|
|
|
|
}
|
|
|
|
if (stderr) {
|
|
|
|
console.error(stderr);
|
|
|
|
}
|
|
|
|
resolve(stdout);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function ffmpeg(command) {
|
|
|
|
return await new Promise((resolve, reject) => {
|
2023-04-08 02:28:59 +02:00
|
|
|
exec(`ffmpeg -hide_banner ${command}`, (err, stdout, stderr) => {
|
2022-06-17 01:25:43 +02:00
|
|
|
if (err) {
|
|
|
|
reject(stderr);
|
|
|
|
}
|
|
|
|
if (stderr) {
|
|
|
|
console.error(stderr);
|
|
|
|
}
|
2022-09-14 11:32:01 +02:00
|
|
|
console.log(NODE_ENV === 'development' ? stdout : null);
|
2022-09-12 11:33:25 +02:00
|
|
|
resolve();
|
2022-06-17 01:25:43 +02:00
|
|
|
});
|
|
|
|
});
|
2022-06-20 08:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function stringIsAValidurl(s) {
|
|
|
|
try {
|
|
|
|
new URL(s);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-06 18:52:44 +02:00
|
|
|
}
|
2022-08-22 20:23:27 +02:00
|
|
|
|
|
|
|
async function compressVideo(input, output, preset) {
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
exec(`./bin/HandBrakeCLI -i '${input}' -Z '${preset}' -o '${os.tmpdir()}/${output}'`, (err, stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
reject(stderr);
|
|
|
|
}
|
|
|
|
if (stderr) {
|
|
|
|
console.error(stderr);
|
|
|
|
}
|
2022-09-14 11:32:01 +02:00
|
|
|
console.log(NODE_ENV === 'development' ? stdout : null);
|
2022-09-12 11:33:25 +02:00
|
|
|
resolve();
|
2022-08-22 20:23:27 +02:00
|
|
|
});
|
|
|
|
});
|
2023-03-20 03:55:53 +01:00
|
|
|
}
|
|
|
|
async function getVideoCodec(input) {
|
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
exec(`ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 ${input}`, (err, stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
reject(stderr);
|
|
|
|
}
|
|
|
|
if (stderr) {
|
|
|
|
console.error(stderr);
|
|
|
|
}
|
|
|
|
resolve(stdout.trim());
|
|
|
|
});
|
|
|
|
});
|
2022-08-22 20:23:27 +02:00
|
|
|
}
|