import { getCommand } from './command.js';
import { BuildCommandToCompressMp4WithFfmpegModel, BuildCommandToConvertMp4ToGifWithFfmpegModel, BuildCommandToConvertVideoWithFfmpegModel, } from './type/index.js';
// convert gif into video
// ffmpeg -stream_loop 100 -i fire.gif -movflags faststart -pix_fmt yuv420p fire.mp4
export async function addAudioToVideoWithFfmpeg({ inputVideoPath, inputAudioPath, outputPath, audioCodec = 'aac', fit, }) {
    return [
        `ffmpeg -y -loglevel error -hide_banner -nostats -i "${inputVideoPath}" -i "${inputAudioPath}" ${fit ? '-shortest ' : ''} -c:v copy -c:a ${audioCodec} "${outputPath}"`,
    ];
}
export async function convertVideoToAudioWithFfmpeg({ inputPath, outputPath, }) {
    // output.mp3
    return [`ffmpeg -i "${inputPath}" -vn ${outputPath}`];
}
export async function removeAudioFromVideoWithFfmpeg({ inputPath, outputPath, }) {
    // output.mp3
    return [`ffmpeg -i "${inputPath}" -c copy -an ${outputPath}`];
}
export async function buildCommandToCompressMP4WithFfmpeg(source) {
    return BuildCommandToCompressMp4WithFfmpegModel.parse(source);
}
// ffmpeg -filters
// https://ffmpeg.org/ffmpeg-filters.html
// https://opensource.com/article/17/6/ffmpeg-convert-media-file-formats
// -ss 00:01:00 (HOURS:MM:SS.MILLISECONDS)
// You can also use the -an and -sn flags in the same manner to strip out audio and subtitle streams.
// https://img.ly/blog/ultimate-guide-to-ffmpeg/#example-material
// https://ottverse.com/change-resolution-resize-scale-video-using-ffmpeg/
// https://creatomate.com/blog/how-to-change-the-resolution-of-a-video-using-ffmpeg
// https://creatomate.com/blog/use-nodejs-to-generate-instagram-youtube-or-tiktok-videos
// -q:a
// wav to mp3
// ffmpeg -i audio.wav -acodec libmp3lame audio.mp3
// ogg to mp3
// ffmpeg -i audio.ogg -acodec libmp3lame audio.mp3
// ac3 to mp3
// ffmpeg -i audio.ac3 -acodec libmp3lame audio.mp3
// aac to mp3
// ffmpeg -i audio.aac -acodec libmp3lame audio.mp3
// crop: -filter:v "crop=w:h:x:y"
// add audio to video:
// ffmpeg -i audio.mp3 -i video.mp4 video_audio_mix.mkv
// concat videos
// ffmpeg -f concat -i file-list.txt -c copy output.mp4
// speed up video
// ffmpeg -i input.mp4 -vf "setpts=0.5*PTS" output.mp4
// compress video
// ffmpeg -i input.mp4 -vf scale=1280:-1 -c:v libx264 -preset veryslow -crf 24 output.mp4
// zoom in:
// "scale=2*iw:-1, crop=iw/2:ih/2"
// loop
// Use -1 for an infinite loop.
// ffmpeg -stream_loop 2 -i input.mp4 -c copy output.mp4
// copy metadata
// ffmpeg -i input.mov -map_metadata 0 -movflags use_metadata_tags output.mp4
// add subtitles
// convert image to video
// ffmpeg -loop 1 -i input.png -c:v libx264 -t 30 -pix_fmt yuv420p video.mp4
// change volume
// ffmpeg -i input.mp3 -af 'volume=0.5' output.mp3
// increase audio tempo
// ffmpeg -i input.mp3 -filter:a "atempo=2.0" -vn output.mp3
// add image to audio
// ffmpeg -loop 1 -i image.jpg -i input.mp3 -c:v libx264 -c:a aac -strict experimental -b:a 192k -shortest output.mp4
// encode for iphone
// ffmpeg -i source_video.avi input -acodec aac -ab 128kb -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 320x180 -title X final_video.mp4
// extract sound from video
// ffmpeg -i source_video.avi -vn -ar 44100 -ac 2 -ab 192k -f mp3 sound.mp3
// https://catswhocode.com/ffmpeg-commands/
export async function buildCommandToConvertVideoWithFfmpeg(source) {
    let { inputPath, outputPath, audioCodec, videoCodec, audioBitRate, videoBitRate, frameRate, startTime, endTime, strict, scaleWidth, scaleHeight, audioChannels, audioSamplingFrequency, subtitleCodec, duration, rotation, overwrite, progress, } = BuildCommandToConvertVideoWithFfmpegModel.parse(source);
    const cmd = getCommand(`ffmpeg`);
    cmd.list.push(`-loglevel`, 'error', `-hide_banner`);
    if (overwrite) {
        cmd.list.push(`-y`);
    }
    else {
        cmd.list.push(`-n`, `-nostdin`);
    }
    if (progress) {
        cmd.list.push(`-stats`, `-progress`, `-`);
    }
    cmd.list.push(`-i`, `${inputPath}`);
    if (startTime) {
        cmd.list.push(`-ss`, `${startTime}`);
    }
    if (endTime) {
        cmd.list.push(`-to`, `${endTime}`);
    }
    if (duration) {
        cmd.list.push(`-t`, `${duration}`);
    }
    if (strict) {
        cmd.list.push(`-strict`, `${strict}`);
    }
    if (audioChannels) {
        cmd.list.push(`-ac`, `${audioChannels}`);
    }
    if (audioSamplingFrequency) {
        cmd.list.push(`-ar`, `${audioSamplingFrequency}`);
    }
    if (scaleWidth) {
        if (scaleHeight) {
            cmd.list.push(`-filter:v`, `"scale=${scaleWidth}:${scaleHeight}"`);
        }
        else {
            cmd.list.push(`-filter:v`, `"scale=${scaleWidth}:-1"`);
        }
    }
    else if (scaleHeight) {
        cmd.list.push(`-filter:v`, `"scale=-1:${scaleHeight}"`);
    }
    else if (rotation) {
        cmd.list.push(`-filter:v`, `"rotate=${rotation}`);
    }
    if (audioBitRate) {
        cmd.list.push(`-b:a`, `${audioBitRate}`);
    }
    if (videoBitRate) {
        cmd.list.push(`-b:v`, `${videoBitRate}`);
    }
    if (frameRate) {
        cmd.list.push(`-r`, `${frameRate}`);
    }
    if (videoCodec) {
        cmd.list.push(`-c:v`, `${videoCodec}`);
    }
    if (audioCodec) {
        cmd.list.push(`-c:a`, `${audioCodec}`);
    }
    if (subtitleCodec) {
        cmd.list.push(`-s:a`, `${subtitleCodec}`);
    }
    cmd.list.push(`${outputPath}`);
    return [cmd];
}
export async function compressVideoWithFfmpeg() { }
export async function buildCommandToConvertMP4ToGifWithFfmpeg(source) {
    const input = BuildCommandToConvertMp4ToGifWithFfmpegModel.parse(source);
    const { inputPath, outputPath, fps, width, startTime, endTime, duration, } = input;
    let cmd = [
        `ffmpeg`,
        `-y`,
        `-loglevel`,
        `error`,
        `-hide_banner`,
        `-nostats`,
        `-i`,
        `"${inputPath}"`,
    ];
    if (startTime) {
        cmd.push(`-ss`, `${startTime}`);
    }
    if (endTime) {
        cmd.push(`-to`, `${endTime}`);
    }
    if (duration) {
        cmd.push(`-t`, `${duration}`);
    }
    cmd.push(`-vf`, `"fps=${fps},scale=${width}:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse"`, `-loop`, `0`, `"${outputPath}"`);
    return [cmd];
}
//# sourceMappingURL=video.js.map