java实现视频音频的剪切
1.基于业务需要将原视频进行剪切下载功能实现;前面的文档介绍过视频的转换流程,这里还是会用到之前的外部组件,这边文件的位置就不在写上去了,可以看上一篇的文档
2.我这边直接写了工具类进行剪切,自己测试过了,少数部分不常见视频格式剪切有点问题,有大佬可以留言教学下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通