Java使用ffmpeg实现视频剪切(单位:M、G)

直接运行代码即可:

import java.io.IOException;

public class FfmpegDemo {


    static String command = "$0 -i $1 -vcodec copy -acodec copy -ss $2 -t $3 $4 -y";

    /**
     * @Title: createSubVideo
     * @Description: 视频剪辑
     * @param videoResourceFilePath 剪辑前的视频源文件路径
     * @param videoTargetFilePath 剪辑后的视频文件路径
     * @param startTime 剪辑开始时间点
     * @param durationTime 剪辑视频总时长
     * @return
     * @return: String
     */
    public static String createSubVideo(String videoResourceFilePath, String videoTargetFilePath,
                                        String startTime, String durationTime) {
        String ffmpegPath = "D:\\Java\\operSources\\ffmpeg-4.3.1\\bin\\ffmpeg.exe";
        String str = command.replace("$0", ffmpegPath).replace("$1", videoResourceFilePath).replace("$2", startTime)
                .replace("$3", durationTime).replace("$4", videoTargetFilePath);
        System.out.println(str);
        Runtime runtime = Runtime.getRuntime();
        try {
            Process proce = runtime.exec(str);
            //处理结果信息Start
//            BufferedReader br = new BufferedReader(new InputStreamReader(proce.getErrorStream()));
//            String line = null;
//            while ((line = br.readLine()) != null) {
//                System.out.println(line);
//            }
            //处理结果信息Start
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String args[]) {
        // 剪辑前的视频源文件路径
        String videoResourceFilePath = "E:\\java\\other\\av\\av\\av\\av\\丰腴极品美女-趁闺蜜外出勾引她男友(时长32分钟).mp4";
        // 剪辑后的视频文件路径
        String videoTargetFilePath = "E:\\java\\other\\av\\av\\av\\av\\丰腴极品美女-趁闺蜜外出勾引她男友(剪切前16分钟).mp4";
        // 剪辑开始时间点
        String startTime = "00:03:00";
        // 剪辑视频总时长
        String durationTime = "00:16:00";

        //调用方法
        createSubVideo(videoResourceFilePath, videoTargetFilePath, startTime, durationTime);
    }
}

视频剪切前是2.9G

Java使用ffmpeg实现视频剪切(单位:M、G)_第1张图片

视频剪切后是1.4G

Java使用ffmpeg实现视频剪切(单位:M、G)_第2张图片 

 

你可能感兴趣的:(ffmpeg,ffmpeg,java,音视频)