FFmpegUtils.java 8.96 KB
package com.glsx.aicar.util;

import io.microshow.rxffmpeg.RxFFmpegCommandList;

/**
 * Created by: lixingzhou
 * Created Date: 2020/4/16 18:00
 * Description:FFmpeg命令集成工具
 */
public class FFmpegUtils {

    //视频添加水印  位置标志
    public static final int TYPE_SITE_LEFT_TOP = 1;//左上角
    public static final int TYPE_SITE_RIGHT_TOP = 2;//右上角
    public static final int TYPE_SITE_LEFT_BOTTOM = 3;//左下角
    public static final int TYPE_SITE_RIGHT_BOTTOM = 4;//右下角

    /**
     * 获取视频帧图片
     *
     * @param videoAbsolutePath 视频地址
     * @param startTime         开始时间  eg:第8s开始   00:00:08
     * @param duration          抓取时长
     * @param r                 每秒帧数
     * @param imageInfo         输出图片信息  eg:/storage/emulated/0/Video/image/pic_%03d.jpeg
     * @param photoSize         图片尺寸  eg:500x420
     * @return 命令集
     */
    public static String[] getVideoFrameImageCommand(String videoAbsolutePath, String startTime, String duration, String r, String imageInfo, String photoSize) {
        RxFFmpegCommandList cmdlist = new RxFFmpegCommandList();
        //如果目标文件已存在  覆盖目标文件
        cmdlist.append("-y");
        cmdlist.append("-i");
        cmdlist.append(videoAbsolutePath);
        cmdlist.append("-ss");
        cmdlist.append(startTime);
        cmdlist.append("-t");
        cmdlist.append(duration);

        cmdlist.append("-r");
        cmdlist.append(r);

        cmdlist.append("-f");
        cmdlist.append("image2");

        cmdlist.append("-s");
        cmdlist.append(photoSize);

        cmdlist.append(imageInfo);
        return cmdlist.build();
    }

    /**
     * 视频裁剪
     *
     * @param videoAbsolutePath 源视频路径
     * @param startTime         开始时间
     * @param duration          截取时长
     * @param outputVideoPath   输出文件目录
     * @return 命令集
     */
    public static String[] videoTailor(String videoAbsolutePath, String startTime, String duration, String outputVideoPath) {
        RxFFmpegCommandList cmdlist = new RxFFmpegCommandList();
        cmdlist.append("-y");
        cmdlist.append("-i");
        cmdlist.append(videoAbsolutePath);
        cmdlist.append("-ss");
        cmdlist.append(startTime);

        cmdlist.append("-c");
        cmdlist.append("copy");

        cmdlist.append("-t");
        cmdlist.append(duration);

        cmdlist.append(outputVideoPath);
        return cmdlist.build();
    }


    /**
     * 视频调速  ffmpeg -i TheOrigin.mp4 -vf  "setpts=0.25*PTS" UpTheOrigin.mp4
     *
     * @param inputVideoPath   视频地址
     * @param videoSpeed       视频倍速   0.5表示加速两倍    5表示慢速5倍  取值范围[0.25, 4]
     * @param musicSpeed       音频倍速    0.5表示慢速1/2   2表示加速2倍   取值范围[0.5, 2]
     * @param outputVideoPath  输出视频地址
     * @param isVideoWithAudio 视频是否包含音频
     * @return 命令集
     */
    public static String[] videoSpeedAdjust(String inputVideoPath, String videoSpeed, String musicSpeed, String outputVideoPath, boolean isVideoWithAudio) {
        RxFFmpegCommandList cmdlist = new RxFFmpegCommandList();
        cmdlist.append("-y");
        cmdlist.append("-i");
        cmdlist.append(inputVideoPath);

        if (isVideoWithAudio) {
            cmdlist.append("-filter_complex");
            cmdlist.append("[0:v]setpts=" + videoSpeed + "*PTS[v];[0:a]atempo=" + musicSpeed + "[a]");
            cmdlist.append("-map");
            cmdlist.append("[v]");
            cmdlist.append("-map");
            cmdlist.append("[a]");
        } else {
            cmdlist.append("-vf");
            cmdlist.append("setpts=" + videoSpeed + "*PTS");
        }

        cmdlist.append(outputVideoPath);
        return cmdlist.build();
    }

    /**
     * 视频配乐 ffmpeg -i input1.avi -i input2.mp3 -vcodec copy -acodec copy out.avi
     * ffmpeg -y -i /storage/emulated/0/1/input.mp4 -i /storage/emulated/0/1/input.mp3 -filter_complex [0:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo,volume=0.2[a0];[1:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo,volume=1[a1];[a0][a1]amix=inputs=2:duration=first[aout] -map [aout] -ac 2 -c:v copy -map 0:v:0 -preset superfast /storage/emulated/0/1/result.mp4
     *
     * @param inputVideoPath   视频地址
     * @param musicPath        音频地址
     * @param originalVolume   原声音量  取值 0~1  数值越小音量越小
     * @param dubVolume        配乐音量   取值 0~1  数值越小音量越小
     * @param outputVideoPath  输出文件地址
     * @param isVideoWithAudio 视频是否包含音频
     * @return 命令集
     */
    public static String[] videoMusicConfig(String inputVideoPath, String musicPath, double originalVolume, double dubVolume, String outputVideoPath, boolean isVideoWithAudio) {
        RxFFmpegCommandList cmdlist = new RxFFmpegCommandList();
        cmdlist.append("-y");
        cmdlist.append("-i");
        cmdlist.append(inputVideoPath);
        cmdlist.append("-i");
        cmdlist.append(musicPath);

        if (isVideoWithAudio) {
            cmdlist.append("-filter_complex");
//        cmdlist.append("[0:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo,volume=0.3[a0];" + //原声音量
//                "[1:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo,volume=0.7[a1];" +  //配乐音量
//                "[a0][a1]amix=inputs=2:duration=first[aout]");
            cmdlist.append("[0:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo,volume=" + originalVolume + "[a0];" + //原声音量
                    "[1:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo,volume=" + dubVolume + "[a1];" +  //配乐音量
                    "[a0][a1]amix=inputs=2:duration=first[aout]");

            cmdlist.append("-map");
            cmdlist.append("[aout]");

            cmdlist.append("-ac");
            cmdlist.append("2");

            cmdlist.append("-c:v");
            cmdlist.append("copy");

            cmdlist.append("-map");
            cmdlist.append("0:v:0");

            cmdlist.append("-preset");
            cmdlist.append("superfast");
        } else {

            cmdlist.append("-c:v");
            cmdlist.append("copy");

            cmdlist.append("-preset");
            cmdlist.append("superfast");
        }

        cmdlist.append(outputVideoPath);
        return cmdlist.build();
    }

    /**
     * 视频压缩  ffmpeg -y -i /storage/emulated/0/1/input.mp4 -b 2097k -r 30 -vcodec libx264 -preset superfast /storage/emulated/0/1/result.mp4
     *
     * @param inputVideoPath  视频地址
     * @param outputVideoPath 输出视频地址
     * @return 命令集
     */
    public static String[] videoCompress(String inputVideoPath, String outputVideoPath, String crf) {
        RxFFmpegCommandList cmdlist = new RxFFmpegCommandList();
        cmdlist.append("-y");
        cmdlist.append("-i");
        cmdlist.append(inputVideoPath);

//        cmdlist.append("-b");
//        cmdlist.append("2097k");
//        cmdlist.append("-r");
//        cmdlist.append("30");

        cmdlist.append("-crf");//-crf: 编码质量,取值范围是0-51,默认值为23,数字越小输出视频的质量越高。
        cmdlist.append(crf);//当前由于限制30s视频  故该参数当前写死  值为30大概可以将源视频压缩一半 质量基本有保证

        cmdlist.append("-vcodec");
        cmdlist.append("libx264");
        cmdlist.append("-preset");
        cmdlist.append("superfast");

        cmdlist.append(outputVideoPath);
        return cmdlist.build();
    }

    /**
     * 添加水印图
     *
     * @param inputVideoPath     视频地址
     * @param watermarkImagePath 水印图片路径
     * @param site               水印位置  参见:{@link FFmpegUtils#TYPE_SITE_LEFT_TOP}
     * @param outputVideoPath    输出视频地址
     * @return 命令集
     */
    public static String[] addWatermarkImage(String inputVideoPath, String watermarkImagePath, int site, String outputVideoPath) {
        RxFFmpegCommandList cmdlist = new RxFFmpegCommandList();
        cmdlist.append("-y");
        cmdlist.append("-i");
        cmdlist.append(inputVideoPath);
        cmdlist.append("-i");
        cmdlist.append(watermarkImagePath);

        cmdlist.append("-filter_complex");
        String siteType;
        switch (site) {
            case TYPE_SITE_RIGHT_TOP:
                siteType = "overlay=W-w";
                break;
            case TYPE_SITE_LEFT_BOTTOM:
                siteType = "overlay=0:H-h";
                break;
            case TYPE_SITE_RIGHT_BOTTOM:
                siteType = "overlay=W-w:H-h";
                break;
            case TYPE_SITE_LEFT_TOP:
            default:
                siteType = "overlay";
                break;
        }
        cmdlist.append(siteType);

        cmdlist.append(outputVideoPath);
        return cmdlist.build();
    }

}