Java仅用30行代码就实现了视频转音频的批量转换

发表于:2021-4-23 09:52

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:洛阳泰山    来源:CSDN

  本功能实现需要用到第三方jar包 jave,JAVE 是java调用FFmpeg的封装工具。
  spring boot项目pom文件中添加以下依赖:
      <!-- 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编辑器 主方法运行。
  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左右。

      本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号