java仅用30行代码就实现了视频转音频的批量转换
本功能实现需要用到第三方jar包 jave,JAVE 是java调用FFmpeg的封装工具。
spring boot项目pom文件中添加以下依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <!-- https://mvnrepository.com/artifact/ws.schild/jave-core --> < dependency > < groupId >ws.schild</ groupId > < artifactId >jave-core</ artifactId > < version >3.1.1</ version > </ dependency > <!-- 以下依赖根据系统二选一 --> <!-- win系统平台的依赖 --> < dependency > < groupId >ws.schild</ groupId > < artifactId >jave-nativebin-win64</ artifactId > < version >3.1.1</ version > </ dependency > <!-- linux系统平台的依赖 --> < dependency > < groupId >ws.schild</ groupId > < artifactId >jave-nativebin-linux64</ artifactId > < version >3.1.1</ version > </ dependency > |
Java单类实现代码,复制到Spring boot项目中,用idea编辑器 主方法运行。
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 | import ws.schild.jave.Encoder; import ws.schild.jave.EncoderException; import ws.schild.jave.MultimediaObject; import ws.schild.jave.encode.AudioAttributes; import ws.schild.jave.encode.EncodingAttributes; import java.io.File; import java.util.Arrays; public class VideoToAudio { //要输出的音频格式 private static String outputFormat= "mp3" ; /** * 获得转化后的文件名 * @param sourceFilePath : 源视频文件路径 * @return */ public static String getNewFileName(String sourceFilePath) { File source = new File(sourceFilePath); String fileName=source.getName().substring( 0 , source.getName().lastIndexOf( "." )); return fileName+ "." +outputFormat; } /** * 转化音频格式 * @param sourceFilePath : 源视频文件路径 * @param targetFilePath : 目标音乐文件路径 * @return */ public static void transform(String sourceFilePath, String targetFilePath) { File source = new File(sourceFilePath); File target = new File(targetFilePath); // 设置音频属性 AudioAttributes audio = new AudioAttributes(); audio.setCodec( null ); // 设置转码属性 EncodingAttributes attrs = new EncodingAttributes(); attrs.setOutputFormat(outputFormat); attrs.setAudioAttributes(audio); try { // 音频转换格式类 Encoder encoder = new Encoder(); MultimediaObject mediaObject= new MultimediaObject(source); encoder.encode(mediaObject, target, attrs); System.out.println( "转换已完成..." ); } catch (EncoderException e) { e.printStackTrace(); } } /** * 批量转化音频格式 * @param sourceFolderPath : 源视频文件夹路径 * @param targetFolderPath : 目标音乐文件夹路径 * @return */ public static void batchTransform(String sourceFolderPath, String targetFolderPath) { File sourceFolder = new File(sourceFolderPath); if (sourceFolder.list().length!= 0 ){ Arrays.asList(sourceFolder.list()).forEach(e->{ transform(sourceFolderPath+ "\\" +e, targetFolderPath+ "\\" +getNewFileName(e)); }); } } public static void main(String[] args) { batchTransform( "C:\\Users\\tarzan\\Desktop\\video" , "C:\\Users\\tarzan\\Desktop\\audio" ); } } |
运行结果截图
测试结果
视频格式为mp4,大小约6.65MB,转为音频格式MP3,大小约1.60MB,转化时间1s左右。
到此这篇关于java仅用30行代码就实现了视频转音频的批量转换的文章就介绍到这了,更多相关java 视频转音频内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
IntelliJ IDEA 2021 Tomcat 8启动乱码问题的解决步骤
很多朋友遇到过IntelliJ IDEA 2021 Tomcat 8启动的时候出现各种奇葩问题,最近有童鞋反映IntelliJ IDEA 2021 Tomcat 8启动乱码,正好我也遇到这个问题,下面我把解决方法分享给大家需要的朋友参考下吧2021-06-06SpringCloud Eureka服务的基本配置和操作方法
Eureka是Netflix开源的一个基于REST的服务治理框架,主要用于实现微服务架构中的服务注册与发现,Eureka是Netflix开源的服务发现框架,用于在分布式系统中实现服务的自动注册与发现,本文介绍SpringCloud Eureka服务的基本配置和操作方法,感兴趣的朋友一起看看吧2023-12-12
最新评论