Springboot AMR格式音频转换MP3

html5中audio支持音频格式

  • HTML5 Audio标签能够支持wav, mp3, ogg, acc, webm等格式

  • 但有个很重要的音乐文件格式amr(扩展名amr)却在各大浏览器中都没有内置的支持。

音频格式ChromeFirefoxIE9OperaSafari
OGG支持支持支持不支持不支持
MP3支持不支持支持不支持支持
WAV不支持支持不支持支持不支持

Java AMR格式音频转MP3

  • pom

    • ws.schild(Windows版本、Linux版本)

    • <!--        文件格式转换依赖-->
              <!-- https://mvnrepository.com/artifact/ws.schild/jave-core -->
              <dependency>
                  <groupId>ws.schild</groupId>
                  <artifactId>jave-core</artifactId>
                  <version>2.4.4</version>
              </dependency>
              <!-- https://mvnrepository.com/artifact/org.springframework/spring-mock -->
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-mock</artifactId>
                  <version>2.0.8</version>
              </dependency>
      
              <!-- https://mvnrepository.com/artifact/ws.schild/jave-native-win64     Windows版本-->
              <dependency>
                  <groupId>ws.schild</groupId>
                  <artifactId>jave-native-win64</artifactId>
                  <version>2.4.4</version>
              </dependency>
      
              <!-- https://mvnrepository.com/artifact/ws.schild/jave-native-linux64   Linux版本-->
              <dependency>
                  <groupId>ws.schild</groupId>
                  <artifactId>jave-native-linux64</artifactId>
                  <version>2.4.4</version>
              </dependency>
      
  • properties

    • filePath:
        sourcePath: /tmp/matter/tempFilePath/sourcePath/
        uploadPath: /tmp/matter/tempFilePath/uploadPath/
      
  • class工具类

    • package com.tqservice.web.core.utils;
      
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.mock.web.MockMultipartFile;
      import org.springframework.stereotype.Component;
      import org.springframework.web.multipart.MultipartFile;
      import ws.schild.jave.AudioAttributes;
      import ws.schild.jave.Encoder;
      import ws.schild.jave.EncodingAttributes;
      import ws.schild.jave.MultimediaObject;
      
      import java.io.*;
      
      /**
       * 文件格式转换工具
       */
      @Component
      public class FileFormat {
      
          @Value("${filePath.sourcePath}")
          private String AMRLOCALPATH;
      
          @Value("${filePath.uploadPath}")
          private String MP3LOCALPATH;
      
          /**
           * 上传文件下载到本地
           *
           * @param multipartFile
           */
          public String downloadFile(MultipartFile multipartFile) {
              OutputStream os = null;
              InputStream inputStream = null;
              String fileName = null;
              String fullPath = null;
              try {
                  inputStream = multipartFile.getInputStream();
                  fileName = multipartFile.getOriginalFilename();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              try {
                  // 保存到临时文件
                  byte[] bs = new byte[1024];
                  // 读取到的数据长度
                  int len;
                  // 输出的文件流保存到本地文件,判断文件夹是否存在
                  File tempFile = new File(AMRLOCALPATH);
                  if (!tempFile.exists()) {
                      tempFile.mkdirs();
                  }
                  os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
                  // 开始读取
                  while ((len = inputStream.read(bs)) != -1) {
                      os.write(bs, 0, len);
                  }
                  fullPath = AMRLOCALPATH + multipartFile.getOriginalFilename();
              } catch (IOException e) {
                  e.printStackTrace();
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  // 完毕,关闭所有链接
                  try {
                      os.close();
                      inputStream.close();
                      return fullPath;
                  } catch (IOException e) {
                      e.printStackTrace();
                      return fullPath;
                  }
              }
          }
      
      
          /**
           * 文件格式转换
           *
           * @param multipartFile 目标文件
           * @return
           */
          public MultipartFile formatConversion(MultipartFile multipartFile) {
              MockMultipartFile mockMultipartFile = null;
              try {
                  //转码源文件
                  String sourcePath = downloadFile(multipartFile);
                  if (null == sourcePath) {
                      return mockMultipartFile;
                  }
                  File source = new File(sourcePath);
                  //判断文件夹是否存在
                  File file = new File(MP3LOCALPATH);
                  if (!file.exists()) {
                      file.mkdirs();
                  }
                  //转码本地输出文件
                  String saveFilePath = multipartFile.getOriginalFilename();
                  saveFilePath = MP3LOCALPATH + saveFilePath.substring(0, saveFilePath.lastIndexOf(".") + 1) + "mp3";
                  File target = new File(saveFilePath);
      
                  //Audio Attributes
                  AudioAttributes audio = new AudioAttributes();
                  audio.setCodec("libmp3lame");
                  audio.setBitRate(128000);
                  audio.setChannels(2);
                  audio.setSamplingRate(44100);
                  //Encoding attributes
                  EncodingAttributes attrs = new EncodingAttributes();
                  attrs.setFormat("mp3");
                  attrs.setAudioAttributes(audio);
                  //Encode
                  Encoder encoder = new Encoder();
                  encoder.encode(new MultimediaObject(source), target, attrs);
                  //转MultipartFile
                  FileInputStream fileInputStream = new FileInputStream(target);
                  mockMultipartFile = new MockMultipartFile(target.getName(), fileInputStream);
              } catch (Exception ex) {
                  ex.printStackTrace();
              }
              return mockMultipartFile;
          }
      
      
          /**
           * 删除临时文件
           *
           * @param fileName
           */
          public void deleteTempFile(String fileName) {
              String amrFilePath = AMRLOCALPATH + fileName.substring(0, fileName.lastIndexOf(".") + 1) + "amr";
              String mp3FilePath = MP3LOCALPATH + fileName;
              File amrFile = new File(amrFilePath);
              File mp3File = new File(mp3FilePath);
              if (amrFile.isFile() && amrFile.exists()) {
                  //存在则进行删除
                  amrFile.delete();
              }
              if (mp3File.isFile() && mp3File.exists()) {
                  mp3File.delete();
              }
          }
      }
      
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值