var fs = require("fs"); var path = require("path"); var filetree = {}; const util = require("util"); const runInTerminal = util.promisify(require("child_process").exec); const limitDecimalsWithRounding = (value, maxDecimals = 2) => { const amount = parseFloat(value); const power = 10 ** maxDecimals; return Math.round(amount * power) / power; }; var walkDirectory = async function (location, obj = {}, untilNow = __dirname) { var dir = fs.readdirSync(location); for (var i = 0; i < dir.length; i++) { var name = dir[i]; var target = location + "/" + name; var stats = fs.statSync(target); if (stats.isFile()) { if (name.slice(-5).includes(".")) { // obj[name.slice(0, -3)] = require(target); const currentFilePath = path.join(untilNow, name); const isVideo = currentFilePath.endsWith(".mp4"); if (!isVideo) false; const op = !isVideo ? await runInTerminal(`du -h "${currentFilePath}"`) : await runInTerminal( `ffprobe "${currentFilePath}" -show_entries format=duration -v quiet -of csv="p=0";` ); const { stdout, stderr } = op; // console.log({ stdout, stderr }); const usefulOp = isVideo ? Math.round( 1e3 * limitDecimalsWithRounding(stdout.split("\n").at(0), 4) ) : limitDecimalsWithRounding(stdout.split("\t").at(0), 4); obj[name] = stderr ? usefulOp : null; } } else if (stats.isDirectory()) { obj[name] = {}; await walkDirectory(target, obj[name], path.join(untilNow, name)); } } }; // walkDirectory(".", filetree).then(() => console.log(filetree)); module.exports = { /** * @returns {Object} */ async getVideoFilesTreeAsObject(location = ".") { const retVal = await walkDirectory(location, {}, location); return retVal; }, };