java实现视频音频的剪切

2021/9/29 17:10:58

本文主要是介绍java实现视频音频的剪切,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.基于业务需要将原视频进行剪切下载功能实现;前面的文档介绍过视频的转换流程,这里还是会用到之前的外部组件,这边文件的位置就不在写上去了,可以看上一篇的文档

2.我这边直接写了工具类进行剪切,自己测试过了,少数部分不常见视频格式剪切有点问题,有大佬可以留言教学下

package com.example.filetranfor.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MontageUtils {
    private static String ffmpegEXE = "E:/server/ffmpeg/bin/win64/ffmpeg.exe";//上篇文章视频转换为MP4的云盘有可以直接下载的

    private static List<String> VIDEO_LIST = new ArrayList<>(Arrays.asList("mov", "mpg", "wmv","3gp", "asf", "asx","avi", "wmv9", "rm","rmvb","flv"));
    private static List<String> AUDIO_LIST = new ArrayList<>(Arrays.asList("mp3", "acm", "wav", "wma", "mp1", "aif"));

    /**
     * @throws Exception:
     * @Description montageInputPath:源文件路径
     * @Description montageStart:开始时间
     * @Description montageEnd:结束时间点
     * @Description montageOutputPath:新生成文件位置
     */
    public  static Boolean montageVideoOrAudio(String montageInputPath, String montageStart, String montageEnd, String montageOutputPath) throws Exception {
        File file = new File(montageOutputPath);
        if (file.exists()) {
            return false;
        }
        if (!file.getParentFile().isDirectory()) {
            file.getParentFile().mkdirs();
        }
        List<String> command = getCommonList(montageInputPath, montageStart, montageEnd, montageOutputPath);
        if(command.size() == 0){
            return  false;
        }
        ProcessBuilder builder = new ProcessBuilder();
        Process process = builder.command(command).redirectErrorStream(true).start();
        new PrintStream(process.getInputStream()).start();
        new PrintStream(process.getErrorStream()).start();
        process.waitFor();
        process.destroy();
        return true;

    }

    public static List<String> getCommonList(String montageInputPath, String montageStart, String montageEnd, String montageOutputPath){
        String suffix = montageInputPath.substring(montageInputPath.lastIndexOf(".") + 1);
        List<String> command = new ArrayList<>();
        if(VIDEO_LIST.contains(suffix)){
            //视频格式
            command.add(ffmpegEXE);
            command.add("-ss");
            command.add(montageStart);
            command.add("-to");
            command.add(montageEnd);
            command.add("-i");
            command.add(montageInputPath);
            command.add("-c:v");
            command.add("libx264");
            command.add("-c:a");
            command.add("aac");
            command.add("-strict");
            command.add("experimental");
            command.add("-b:a");
            command.add("98k");
            command.add(montageOutputPath);
            command.add("-y");
        }else if(AUDIO_LIST.contains(suffix)){
            //音频格式
            command.add(ffmpegEXE);
            command.add("-i");
            command.add(montageInputPath);
            command.add("-ss");
            command.add(montageStart);
            command.add("-to");
            command.add(montageEnd);
            command.add(montageOutputPath);
            command.add("-y");
        }else {
            return new ArrayList<>();
        }
        return command;
    }

    public static void main(String[] args) {
        String input = "E:\\test\\video\\test.mp3";
        String out= "E:\\test\\video\\22.mp3";
        String suffix = input.substring(input.lastIndexOf(".") + 1);
        System.out.println(suffix);
        String start = "00:00:30";
        String end="00:00:55";
        try {
            MontageUtils.montageVideoOrAudio(input,start,end,out);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

  自测的时候 rm视频格式在剪切的时候出现问题,剪切后打不开,还在摸索中,也没报错,疯狂找问题,知道的留言告知12



这篇关于java实现视频音频的剪切的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程