"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertAudioToOgg = exports.convertAudioToWav = void 0;
const fluent_ffmpeg_1 = __importDefault(require("fluent-ffmpeg"));
const util_1 = __importDefault(require("util"));
const child_process_1 = require("child_process");
const exec = util_1.default.promisify(child_process_1.exec);
function convertAudioToOgg(inputFile, outputFile) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const command = `ffmpeg -i ${inputFile} -c:a libopus ${outputFile}.ogg && rm ${inputFile}`;
            const { stdout, stderr } = yield exec(command);
            console.log("Conversion finished");
            console.log("stdout:", stdout);
            console.error("stderr:", stderr);
        }
        catch (error) {
            console.error("Error:", error);
            throw error;
        }
    });
}
exports.convertAudioToOgg = convertAudioToOgg;
function convertAudioToWav(inputFile, outputFile) {
    return new Promise((resolve, reject) => {
        const command = fluent_ffmpeg_1.default(inputFile)
            .audioCodec("pcm_s16le") // Set the audio codec to libvorbis (OGG)
            .format("wav") // Set the output format to OGG
            .on("end", () => {
            console.log("Conversion finished");
            resolve(); // Resolve the promise when the conversion is successful
        })
            .on("error", (err) => {
            console.error("Error:", err);
            reject(err); // Reject the promise if there is an error
        });
        // Save the output to the specified file
        command.save(outputFile);
    });
}
exports.convertAudioToWav = convertAudioToWav;
