提取方法:1、spring boot项目pom文件中添加对应依赖;2、Java单类实现代码,复制到Spring boot项目中;3、运行结果即可。
1、spring boot项目pom文件中添加以下依赖
ws.schildjave-core3.1.1 ws.schildjave-nativebin-win643.1.1 ws.schildjave-nativebin-linux643.1.1
登录后复制
2、Java单类实现代码,复制到Spring boot项目中
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;//java项目www.fhadmin.orgpublic 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"); } }
登录后复制
3、运行结果截图
立即学习“Java免费学习笔记(深入)”;
以上就是java中怎么从视频里面提取音频的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2621493.html