parent
1b9374cb37
commit
bcac42135c
@ -0,0 +1,63 @@
|
||||
/*
|
||||
This is not a server file, but a command to remux files on your server.
|
||||
|
||||
Usage (in the root of the folder): node ./scripts/remux-unplayable.js
|
||||
*/
|
||||
|
||||
const config = require("../config.json")
|
||||
const {MongoClient} = require("mongodb");
|
||||
const client = new MongoClient(config["db-url"], {useUnifiedTopology: true});
|
||||
const fs = require("fs");
|
||||
const {spawn} = require("child_process");
|
||||
const ffmpegStatic = require("ffmpeg-static");
|
||||
if (!config["media-folder"].startsWith("/")) {
|
||||
config["media-folder"] = `${__dirname.split("/scripts")[0]}/${config["media-folder"]}`;
|
||||
}
|
||||
|
||||
(async function() {
|
||||
console.log("Connecting to DB...");
|
||||
await client.connect();
|
||||
console.log("- Connected to DB.");
|
||||
let db = await client.db("mediahost");
|
||||
let files = await db.collection("uploads");
|
||||
let f = await files.find({}).toArray();
|
||||
let a = [];
|
||||
|
||||
if (!fs.existsSync(config?.["remux"]?.["folder"])) fs.mkdirSync(config?.["remux"]?.["folder"]);
|
||||
|
||||
for (let c in f) {
|
||||
if (f[c].mime.startsWith("video/")) {
|
||||
if (
|
||||
f[c].mime != "video/mp4" &&
|
||||
f[c].mime != "video/webm" &&
|
||||
!f[c].transcodedFile || !fs.existsSync(f[c].transcodedFile)
|
||||
) {
|
||||
console.log(`Remuxing ${f[c].id} [${f[c].name}]`);
|
||||
await ffmpeg(f[c]);
|
||||
a.push(f[c].id);
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(`Remuxed ${a.length} unplayable files from database.`)
|
||||
process.exit();
|
||||
})()
|
||||
|
||||
async function ffmpeg(file) {
|
||||
let db = await client.db("mediahost");
|
||||
let files = await db.collection("uploads");
|
||||
return new Promise(function(resolve, reject) {
|
||||
let originalFile = `${config["media-folder"]}/${file.id}/${file.name}`;
|
||||
let transFile = `${config["remux"]["folder"]}/${file.id}/${file.name}.${config["remux"]["container"]}`; // based file
|
||||
if (!fs.existsSync(`${config["remux"]["folder"]}/${file.id}`)) fs.mkdirSync(`${config["remux"]["folder"]}/${file.id}`);
|
||||
let ffmpegProcess = spawn(ffmpegStatic, [`-i`, originalFile, `-vcodec`, config["remux"]["video-codec"], `-acodec`, config["remux"]["audio-codec"], `-preset`, `ultrafast`, transFile]);
|
||||
|
||||
ffmpegProcess.on("close", async function(code) {
|
||||
if (code == 0) {
|
||||
file.transcodedFile = `/transcoded/${file.id}/${file.name}.${config["remux"]["container"]}`;
|
||||
await files.findOneAndReplace({id: file.id}, file);
|
||||
console.log(`- Remuxed ${file.id} [${file.name}]`);
|
||||
resolve();
|
||||
} else console.log(`ffmpeg failed on ${file.id}`);
|
||||
});
|
||||
})
|
||||
}
|
Loading…
Reference in new issue