diff options
author | Minteck <contact@minteck.org> | 2022-06-14 12:27:20 +0200 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2022-06-14 12:27:20 +0200 |
commit | a6eabecc147b17aae29c9dcfe2bfded153a0f548 (patch) | |
tree | e9d9c7d9aa2b7ee1828340f5a32a6e626e9a1aa2 /elac-decode | |
parent | ecb92e72778bb12b8875145650610617624c6530 (diff) | |
download | elac-a6eabecc147b17aae29c9dcfe2bfded153a0f548.tar.gz elac-a6eabecc147b17aae29c9dcfe2bfded153a0f548.tar.bz2 elac-a6eabecc147b17aae29c9dcfe2bfded153a0f548.zip |
Update
Diffstat (limited to 'elac-decode')
-rwxr-xr-x | elac-decode | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/elac-decode b/elac-decode new file mode 100755 index 0000000..02fb824 --- /dev/null +++ b/elac-decode @@ -0,0 +1,101 @@ +#!/usr/local/bin/node +global.fs = require('fs'); +global.os = require('os'); +global.path = require('path'); +global.zlib = require('zlib'); +global.child_process = require('child_process'); + +global.version = "1.1"; + +global.argv = process.argv; +argv.shift(); argv.shift(); + +console.log("Equestria Lossless Audio Codec"); +console.log(" (c) Copyright, Equestria.dev"); +console.log("ELAC Decoder"); +console.log(" (c) Copyright, Equestria.dev"); +console.log("version " + version); +console.log(""); + +try { + child_process.execSync("ffmpeg -version"); + child_process.execSync("ffprobe -version"); + child_process.execSync("ffplay -version"); +} catch (e) { + console.error("Unable to start the ELAC Decoder:\n ffmpeg was not found or is incomplete"); + process.exit(2); +} + +if (argv.length < 1) { + console.error("Unable to start the ELAC Decoder:\n No input file has been specified"); + process.exit(2); +} + +global.inputFile = argv[0].trim(); +global.file = path.basename(inputFile); + +if (!fs.existsSync(inputFile)) { + console.error("Unable to decode \"" + file + "\":\n \"" + inputFile + "\": no such file or directory"); + process.exit(2); +} + +console.log("Reading Equestria Lossless Audio Codec data..."); +try { + global.data = JSON.parse(zlib.inflateSync(fs.readFileSync(inputFile), { + level: -1, + strategy: 0, + memLevel: 9 + }).toString()); +} catch (e) { + console.error("Unable to decode \"" + file + "\":\n \"" + inputFile + "\": unable to decode file"); + process.exit(2); +} + +if (!data._eqmc) { + console.error("Unable to decode \"" + file + "\":\n \"" + inputFile + "\": not a valid ELAC file"); + process.exit(2); +} + +global.inputs = data.files; + +for (let input of inputs) { + console.log("\n" + input._id + ":\n " + Object.keys(input.tags).map(i => { return i + ": " + input.tags[i]; }).join("\n ") + "\n"); + + try { + if (fs.existsSync(os.tmpdir() + "/elac-play-temp")) { + fs.rmSync(os.tmpdir() + "/elac-play-temp", { recursive: true }); + } + fs.mkdirSync(os.tmpdir() + "/elac-play-temp", { recursive: true }); + } catch (e) { + console.error("Unable to decode \"" + file + "\":\n Cannot create temporary working directory"); + process.exit(2); + } + + console.log("Decoding \"" + inputFile + ":/" + input._id + "\"..."); + + let outFile = os.tmpdir() + "/elac-play-temp/" + input._id + ".opus"; + if (input.lossless) { + outFile = os.tmpdir() + "/elac-play-temp/" + input._id + ".flac"; + } + + fs.writeFileSync(outFile, zlib.inflateSync(Buffer.from(input.data, "base64"), { + level: -1, + strategy: 0, + memLevel: 9 + })); + + console.log("Moving to \"" + inputFile + "." + input._id + ".wav\"..."); + + child_process.execFileSync("ffmpeg", [ "-v", "quiet", "-i", outFile, "-map_metadata", "-1", "-fflags", "+bitexact", "-flags:v", "+bitexact", "-flags:a", "+bitexact", "-metadata", "id3v2_priv.XMP=", outFile + ".wav" ]); + + fs.renameSync(outFile + ".wav", inputFile + "." + input._id + ".wav"); + + try { + fs.rmSync(os.tmpdir() + "/elac-play-temp", { recursive: true }); + } catch (e) { + console.error("Unable to encode \"" + file + "\":\n Cannot clear temporary working directory"); + process.exit(2); + } +} + +console.log("Done!");
\ No newline at end of file |