web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

后端程序员如何通过java从视频里面提取音频(具体代码如下)

150 ℃
     

1、在项目pom文件中添加以下依赖

<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>

2、把下面代码到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.org
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");
  }
}

深入了解Java流程控制语句大全

Java如何从列表中删除所有元素

java语法中怎么使用switch语句

java语法中如何利用Map实现遍历

java语法如何利用Class实现实例化

标签: java提取音频, java语法

上面是“后端程序员如何通过java从视频里面提取音频(具体代码如下)”的全面内容,想了解更多关于 后端开发 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_15394.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > 后端开发
本文共计2142个字,预计阅读时长15分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一款完全免费的 Windows 软件卸载、清理工具——HiBit Uninstaller
下一篇: 推荐一款可免费商用圆体字库——江城圆体
x 打工人ai神器