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