aboutsummaryrefslogtreecommitdiff
path: root/elac-play
blob: c2658cfdfc95a90859ff12adf9dab00766924cef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env 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 Player");
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 Player:\n    ffmpeg was not found or is incomplete");
    process.exit(2);
}

if (argv.length < 1) {
    console.error("Unable to start the ELAC Player:\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 play \"" + 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 play \"" + file + "\":\n    \"" + inputFile + "\": unable to decode file");
    process.exit(2);
}

if (!data._eqmc) {
    console.error("Unable to play \"" + 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 + ".mp3";
    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("Playing \"" + inputFile + ":/" + input._id + "\"...");
    child_process.spawnSync("ffplay", [ "-hide_banner", "-autoexit", "-nodisp", "--", outFile ], { stdio: "inherit" });

    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!");