"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addWatermark = exports.addIntroOutro = exports.cutVideo = void 0;
const tslib_1 = require("tslib");
const constants_1 = require("../../initializers/constants");
const logger_1 = require("../logger");
const ffmpeg_commons_1 = require("./ffmpeg-commons");
const ffmpeg_presets_1 = require("./ffmpeg-presets");
const ffprobe_utils_1 = require("./ffprobe-utils");
const lTags = (0, logger_1.loggerTagsFactory)('ffmpeg');
function cutVideo(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { inputPath, outputPath } = options;
        logger_1.logger.debug('Will cut the video.', Object.assign({ options }, lTags()));
        let command = (0, ffmpeg_commons_1.getFFmpeg)(inputPath, 'vod')
            .output(outputPath);
        command = (0, ffmpeg_presets_1.presetCopy)(command);
        if (options.start)
            command.inputOption('-ss ' + options.start);
        if (options.end) {
            const endSeeking = options.end - (options.start || 0);
            command.outputOption('-to ' + endSeeking);
        }
        yield (0, ffmpeg_commons_1.runCommand)({ command });
    });
}
exports.cutVideo = cutVideo;
function addWatermark(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { watermarkPath, inputPath, outputPath, availableEncoders, profile } = options;
        logger_1.logger.debug('Will add watermark to the video.', Object.assign({ options }, lTags()));
        const videoProbe = yield (0, ffprobe_utils_1.ffprobePromise)(inputPath);
        const fps = yield (0, ffprobe_utils_1.getVideoStreamFPS)(inputPath, videoProbe);
        const { resolution } = yield (0, ffprobe_utils_1.getVideoStreamDimensionsInfo)(inputPath, videoProbe);
        let command = (0, ffmpeg_commons_1.getFFmpeg)(inputPath, 'vod')
            .output(outputPath);
        command.input(watermarkPath);
        command = yield (0, ffmpeg_presets_1.presetVOD)({
            command,
            input: inputPath,
            availableEncoders,
            profile,
            resolution,
            fps,
            canCopyAudio: true,
            canCopyVideo: false
        });
        const complexFilter = [
            {
                inputs: ['[1]', '[0]'],
                filter: 'scale2ref',
                options: {
                    w: 'oh*mdar',
                    h: `ih*${constants_1.VIDEO_FILTERS.WATERMARK.SIZE_RATIO}`
                },
                outputs: ['[watermark]', '[video]']
            },
            {
                inputs: ['[video]', '[watermark]'],
                filter: 'overlay',
                options: {
                    x: `main_w - overlay_w - (main_h * ${constants_1.VIDEO_FILTERS.WATERMARK.HORIZONTAL_MARGIN_RATIO})`,
                    y: `main_h * ${constants_1.VIDEO_FILTERS.WATERMARK.VERTICAL_MARGIN_RATIO}`
                }
            }
        ];
        command.complexFilter(complexFilter);
        yield (0, ffmpeg_commons_1.runCommand)({ command });
    });
}
exports.addWatermark = addWatermark;
function addIntroOutro(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { introOutroPath, inputPath, outputPath, availableEncoders, profile, type } = options;
        logger_1.logger.debug('Will add intro/outro to the video.', Object.assign({ options }, lTags()));
        const mainProbe = yield (0, ffprobe_utils_1.ffprobePromise)(inputPath);
        const fps = yield (0, ffprobe_utils_1.getVideoStreamFPS)(inputPath, mainProbe);
        const { resolution } = yield (0, ffprobe_utils_1.getVideoStreamDimensionsInfo)(inputPath, mainProbe);
        const mainHasAudio = yield (0, ffprobe_utils_1.hasAudioStream)(inputPath, mainProbe);
        const introOutroProbe = yield (0, ffprobe_utils_1.ffprobePromise)(introOutroPath);
        const introOutroHasAudio = yield (0, ffprobe_utils_1.hasAudioStream)(introOutroPath, introOutroProbe);
        let command = (0, ffmpeg_commons_1.getFFmpeg)(inputPath, 'vod')
            .output(outputPath);
        command.input(introOutroPath);
        if (!introOutroHasAudio && mainHasAudio) {
            const duration = yield (0, ffprobe_utils_1.getVideoStreamDuration)(introOutroPath, introOutroProbe);
            command.input('anullsrc');
            command.withInputFormat('lavfi');
            command.withInputOption('-t ' + duration);
        }
        command = yield (0, ffmpeg_presets_1.presetVOD)({
            command,
            input: inputPath,
            availableEncoders,
            profile,
            resolution,
            fps,
            canCopyAudio: false,
            canCopyVideo: false
        });
        const complexFilter = [
            {
                inputs: ['1', '0'],
                filter: 'scale2ref',
                options: {
                    w: 'iw',
                    h: `ih`
                },
                outputs: ['intro-outro', 'main']
            },
            {
                inputs: ['intro-outro', 'main'],
                filter: 'scale2ref',
                options: {
                    w: 'iw',
                    h: `ih`
                },
                outputs: ['to-scale', 'main']
            },
            {
                inputs: 'to-scale',
                filter: 'drawbox',
                options: {
                    t: 'fill'
                },
                outputs: ['to-scale-bg']
            },
            {
                inputs: ['1', 'to-scale-bg'],
                filter: 'scale2ref',
                options: {
                    w: 'iw',
                    h: 'ih',
                    force_original_aspect_ratio: 'decrease',
                    flags: 'spline'
                },
                outputs: ['to-scale', 'to-scale-bg']
            },
            {
                inputs: ['to-scale-bg', 'to-scale'],
                filter: 'overlay',
                options: {
                    x: '(main_w - overlay_w)/2',
                    y: '(main_h - overlay_h)/2'
                },
                outputs: 'intro-outro-resized'
            }
        ];
        const concatFilter = {
            inputs: [],
            filter: 'concat',
            options: {
                n: 2,
                v: 1,
                unsafe: 1
            },
            outputs: ['v']
        };
        const introOutroFilterInputs = ['intro-outro-resized'];
        const mainFilterInputs = ['main'];
        if (mainHasAudio) {
            mainFilterInputs.push('0:a');
            if (introOutroHasAudio) {
                introOutroFilterInputs.push('1:a');
            }
            else {
                introOutroFilterInputs.push('2:a');
            }
        }
        if (type === 'intro') {
            concatFilter.inputs = [...introOutroFilterInputs, ...mainFilterInputs];
        }
        else {
            concatFilter.inputs = [...mainFilterInputs, ...introOutroFilterInputs];
        }
        if (mainHasAudio) {
            concatFilter.options['a'] = 1;
            concatFilter.outputs.push('a');
            command.outputOption('-map [a]');
        }
        command.outputOption('-map [v]');
        complexFilter.push(concatFilter);
        command.complexFilter(complexFilter);
        yield (0, ffmpeg_commons_1.runCommand)({ command });
    });
}
exports.addIntroOutro = addIntroOutro;
//# sourceMappingURL=ffmpeg-edition.js.map