文件格式转换音频转MP3(aac转MP3......) java

一:引入相关包(maven)。

<dependency>
   <groupId>ws.schild</groupId>
   <artifactId>jave-core</artifactId>
   <version>2.4.2</version>
</dependency>
<dependency>
   <groupId>ws.schild</groupId>
   <artifactId>jave-native-linux64</artifactId>
   <version>2.4.2</version>
</dependency>

<dependency>
   <groupId>ws.schild</groupId>
   <artifactId>jave-native-win64</artifactId>
   <version>2.4.2</version>
</dependency>
<dependency>
   <groupId>ws.schild</groupId>
   <artifactId>jave-native-osx64</artifactId>
   <version>2.4.2</version>
</dependency>

二:转换方法(例:音频转Mp3)

public static void changeSourceToMp3(File source, String targetPath) throws Exception {

    MultimediaObject multimediaObject = new MultimediaObject(source);
    File target = new File(targetPath);
    AudioAttributes audio = new AudioAttributes();
    Encoder encoder = new Encoder();

    audio.setCodec("libmp3lame");
    EncodingAttributes attrs = new EncodingAttributes();
    attrs.setFormat("mp3");
    attrs.setAudioAttributes(audio);

    encoder.encode(multimediaObject, target, attrs);
}

三:使用示例:

public JsonResult uploadFile(HttpServletRequest request) {
    JsonResult responseInfo = new JsonResult();
    List<Map<String,String>> listMap = new ArrayList<>();
    try {
        String contentType = request.getContentType();
        if(null != contentType && contentType.toLowerCase().startsWith("multipart/")){
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
            //获取上传上来的文件
            Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
            if(null != fileMap && fileMap.size()>0){
                for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
                    MultipartFile file = entry.getValue();
                    String fileName = file.getOriginalFilename();
                    String suffix =  fileName.substring(fileName.lastIndexOf("."),fileName.length());
                    String key = entry.getKey();
                    //uploadPath 文件上传配置的访问地址
                    String folderStr = uploadPath+"/file/" + UtilDates.getYear()+"/"+UtilDates.getMonthDay() +"/"+fileName;
                    String  picpathTemp = "/file/" + UtilDates.getYear()+"/"+UtilDates.getMonthDay() +"/";
                    String path = folderStr+fileName;
                    File folder = new File(folderStr);
                    if (!folder.exists() && !folder.isDirectory()) {
                        folder.mkdirs();//创建文件夹
                    }
                    file.transferTo(new File(path));

                    if(suffix.equals(".acc")||suffix.equals(".aac")||suffix.equals(".amr")){
                        String pathTemp = path.substring(0,path.lastIndexOf("."))+".mp3";
                        File newFile  = new File(path);
                        AudioToMp3.changeSourceToMp3(newFile,pathTemp);
                        //删除源(原)文件
                        newFile.delete();
                        picpathTemp = picpathTemp.substring(0,picpathTemp.lastIndexOf("."))+".mp3";
                    }
                    Map<String,String> map = new HashMap<>();
                    map.put(key,picpathTemp);
                    listMap.add(map);
                }
            }
        }else{
            return responseInfo.setSuccess(false,"上传文件为空,请上传文件!");
        }
    } catch (Exception e) {
        return responseInfo.setSuccess(false,"文件保存失败!");
    }
    responseInfo.setData(listMap);
    return responseInfo.setSuccess(true,"文件上传成功");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值