File size: 1,838 Bytes
43a2bcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import ffmpeg from "fluent-ffmpeg";
import path from "path";
import("@ffmpeg-installer/ffmpeg");
import fs from "fs-extra";

import { logger } from "../logger";
import { MusicManager } from "../short-creator/music";
import { Config } from "../config";

async function normalize(inputPath: string, outputPath: string) {
  return new Promise((resolve, reject) => {
    ffmpeg()
      .input(inputPath)
      .audioCodec("libmp3lame")
      .audioBitrate(96)
      .audioChannels(2)
      .audioFrequency(44100)
      .audioFilter("loudnorm,volume=0.1")
      .toFormat("mp3")
      .on("error", (err) => {
        logger.error(err, "Error normalizing audio:");
        reject(err);
      })
      .save(outputPath)
      .on("end", () => {
        logger.debug("Audio normalization complete");
        resolve(outputPath);
      });
  });
}

export async function normalizeMusic() {
  const config = new Config();
  const musicManager = new MusicManager(config);
  try {
    musicManager.ensureMusicFilesExist();
  } catch (error: unknown) {
    logger.error(error, "Missing music files");
    process.exit(1);
  }
  const musicFiles = musicManager.musicList();
  const normalizedDir = path.join(config.musicDirPath, "normalized");
  fs.ensureDirSync(normalizedDir);
  for (const musicFile of musicFiles) {
    const inputPath = path.join(config.musicDirPath, musicFile.file);
    const outputPath = path.join(normalizedDir, musicFile.file);
    logger.debug({ inputPath, outputPath }, "Normalizing music file");
    await normalize(inputPath, outputPath);
  }
}

normalizeMusic()
  .then(() => {
    logger.info(
      "Music normalization completed successfully - make sure to replace the original files with the normalized ones",
    );
  })
  .catch((error: unknown) => {
    logger.error(error, "Error normalizing music files");
  });