summaryrefslogtreecommitdiff
path: root/includes/process.js
blob: 0a1798f16a7c2fc77fad31d47e3f55bdc8a78f27 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
const fs = require('fs');
const cp = require('child_process');
const path = require('path');
const uuid = require('crypto').randomUUID;

const substitutes = [
    ["illenium", "ILLENIUM"]
];

const songs = require('../assets/content/songs.json');
const albums = require('../assets/content/albums.json');

function scandir(dir) {
    return new Promise((res, rej) => {
        const walk = (dir, done) => {
            let results = [];
            fs.readdir(dir, function(err, list) {
                if (err) return done(err);
                let pending = list.length;

                if (!pending) return done(null, results);
                list.forEach(function(file) {
                    file = path.resolve(dir, file);
                    fs.stat(file, function(err, stat) {
                        if (stat && stat.isDirectory()) {
                            walk(file, function(err, res) {
                                results = results.concat(res);
                                if (!--pending) done(null, results);
                            });
                        } else {
                            results.push(file);
                            if (!--pending) done(null, results);
                        }
                    });
                });
            });
        }

        walk(dir, (err, data) => {
            if (err) {
                rej(err);
            } else {
                res(data);
            }
        })
    });
}

function substitute(text) {
    for (let sub of substitutes) {
        if (text.trim() === sub[0].trim()) {
            return sub[1].trim();
        }
    }
}

(async () => {
    for (let file of (await scandir("../assets/content/_")).filter(i => i.endsWith(".flac"))) {
        let id = uuid();
        let metadata = JSON.parse(cp.execFileSync("ffprobe", ["-v", "quiet", "-print_format", "json", "-show_format", "-show_streams", file]).toString());

        songs[id] = {
            title: substitute(metadata['format']['tags']['TITLE'] ?? path.basename(file, ".flac")),
            length: parseInt(metadata['format']['duration']),
            album: substitute(metadata['format']['tags']['ALBUM'] ?? "Unknown album"),
            artist: substitute(metadata['format']['tags']['ARTIST'] ?? "Unknown artist"),
            albumArtist: substitute(metadata['format']['tags']['album_artist'] ?? metadata['format']['tags']['ARTIST'] ?? "Unknown artist"),
            date: parseInt(metadata['format']['tags']['DATE']) ?? 0,
            track: parseInt(metadata['format']['tags']['track']) ?? 0,
            size: parseInt(metadata['format']['size']),
            bitRate: parseInt(metadata['format']['bit_rate']),
            bitDepth: parseInt(metadata['streams'][0]['bits_per_raw_sample']),
            sampleRate: parseInt(metadata['streams'][0]['sample_rate']),
            hiRes: parseInt(metadata['streams'][0]['sample_rate']) > 44100 || parseInt(metadata['streams'][0]['bits_per_raw_sample']) > 16,
            channels: parseInt(metadata['streams'][0]['channels']),
        }

        fs.writeFileSync("../assets/content/songs.json", JSON.stringify(songs));
        cp.execFileSync("ffmpeg", ["-i", file, "-map", "0", "-map", "-0:v?", "-b:a", "256k", "../assets/content/" + id + ".m4a"]);
        cp.execFileSync("ffmpeg", ["-i", file, "-map", "0", "-map", "-0:v?", "../assets/content/" + id + ".flac"]);
        cp.execFileSync("ffmpeg", ["-i", file, "-an", "../assets/content/" + id + ".jpg"]);
        fs.unlinkSync(file);
    }

    for (let song of Object.keys(songs)) {
        if (Object.values(albums).filter(i => i.title === songs[song].album).length > 0) {
            Object.values(albums).filter(i => i.title === songs[song].album)[0].tracks.push(song);
            Object.values(albums).filter(i => i.title === songs[song].album)[0].hiRes = Object.values(albums).filter(i => i.title === songs[song].album)[0].hiRes || songs[song].hiRes;
        } else {
            let albumID = uuid();
            fs.copyFileSync("../assets/content/" + song + ".jpg", "../assets/content/" + albumID + ".jpg")

            albums[albumID] = {
                title: songs[song].album,
                artist: songs[song].albumArtist,
                date: songs[song].date,
                hiRes: songs[song].hiRes,
                tracks: [song]
            }
        }
    }

    for (let albumID of Object.keys(albums)) {
        let album = albums[albumID];
        album["tracks"] = album["tracks"].sort((a, b) => {
            return songs[a]['track'] - songs[b]['track'];
        });
    }

    fs.writeFileSync("../assets/content/albums.json", JSON.stringify(albums));
})()