java实现将.acc格式转化为mp3格式

java实现将.acc格式转化为mp3格式最近接到一个需求,将腾讯云的音频.acc格式的转为mp3格式这里用到的是jave,  jave2(Java音频视频编码器)库是Java对ffmpeg的包装,它可以很方便的实现视频音频格式的转换,本文简单记录一下将wav格式的音频转换成mp3格式的音频。实现步骤:1.maven包导入根据需要导入不同的包,有os、window、linux<dependencies&…

大家好,欢迎来到IT知识分享网。

最近接到一个需求,将腾讯云的音频.acc格式的转为mp3格式

这里用到的是jave,  jave2Java音频视频编码器)库是Java对ffmpeg的包装,它可以很方便的实现视频音频格式的转换,本文简单记录一下将wav格式的音频转换成mp3格式的音频。

实现步骤:

1.maven包导入

根据需要导入不同的包,有os、window、linux

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

2.代码实现

将远程下载的url复制到该工具类下面的main方法进行测试即可

package utils;

import ws.schild.jave.AudioAttributes;
import ws.schild.jave.Encoder;
import ws.schild.jave.EncodingAttributes;
import ws.schild.jave.MultimediaObject;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;

/**
 *  音频转化工具类
 * @author pengjie_yao
 * @date 2019年12月23日11:35:20
 */
public class AudioConvertUtils {

    /**
     * 根据远程资源路径,下载资源到本地临时目录
     *
     * @param remoteSourceUrl 远程资源路径
     * @param tmpFileFolder   本地临时目录
     * @return 下载后的文件物理路径
     */
    private static String downloadSource(String remoteSourceUrl, String tmpFileFolder) throws Exception {
        //下载资源
        URL url = new URL(remoteSourceUrl);
        DataInputStream dataInputStream = new DataInputStream(url.openStream());
        String tmpFilePath = tmpFileFolder + getOriginalFileName(remoteSourceUrl);
        FileOutputStream fileOutputStream = new FileOutputStream(new File(tmpFilePath));
        byte[] bytes = new byte[1024];
        int length = 0;
        while ((length = dataInputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes, 0, length);
            // System.out.println("下载中....");
        }
        // System.out.println("下载完成...");
        dataInputStream.close();
        fileOutputStream.close();

        return tmpFilePath;
    }

    /**
     * 将本地音频文件转换成mp3格式文件
     *
     * @param localFilePath 本地音频文件物理路径
     * @param targetPath    转换后mp3文件的物理路径
     */
    public static void changeLocalSourceToMp3(String localFilePath, String targetPath) throws Exception {

        File source = new File(localFilePath);
        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);
    }

    /**
     * 下载远程文件到本地,然后转换成mp3格式,再删除下载的资源文件,并返回mp3的文件绝对路径
     * <p>每次会优先检测mp3文件是否已存在,存在则直接返回mp3绝对路径,否则下载然后转换成mp3再返回</p>
     *
     * @param remoteSourceUrl 远程文件的URL
     * @param tmpFolder       临时文件存放的目录,如/usr/consult/tmp/
     * @return 转换成mp3的文件的绝对路径,如/usr/consult/tmp/fcd124a2-ed6c-4407-b574-81ecc51b4eb4.mp3
     */
    public static String changeRemoteSourceToMp3(String remoteSourceUrl, String tmpFolder) throws Exception {
        // 检测该文件是否已经下载并转换过一次,如果是,则直接返回
        String remoteSourceNameWithoutSuffix = getNameWithoutSuffix(remoteSourceUrl);
        // 格式为/usr/consult/tmp/fcd124a2-ed6c-4407-b574-81ecc51b4eb4.mp3
        String mp3FilePath = tmpFolder + File.separator + remoteSourceNameWithoutSuffix + ".mp3";
        File audioFile = new File(mp3FilePath);
        if (audioFile.exists()) {
            // 文件已在之前转换过一次,直接返回即可
            return mp3FilePath;
        }

        // 下载资源到临时目录
        String tmpRemoteFilePath = downloadSource(remoteSourceUrl, tmpFolder);
        // 转换成mp3格式
        changeLocalSourceToMp3(tmpRemoteFilePath, mp3FilePath);
        // 删除.acc格式
        File accFile = new File(tmpRemoteFilePath);
        accFile.delete();
        return mp3FilePath;
    }

    /**
     * 根据文件url获取文件名(包含后缀名)
     *
     * @param url 文件url
     * @return 文件名(包含后缀名)
     */
    private static String getOriginalFileName(String url) {
        String[] sarry = url.split("/");
        return sarry[sarry.length - 1];
    }

    /**
     * 根据文件url获取文件名(不包含后缀名)
     *
     * @param url 文件url
     * @return 文件名(包含后缀名)
     */
    private static String getNameWithoutSuffix(String url) {
        String originalFileName = getOriginalFileName(url);
        return originalFileName.substring(0, originalFileName.indexOf("."));
    }

    public static void main(String[] args) throws Exception {

        String result = changeRemoteSourceToMp3("XXX", "D:\\");
        System.out.println(result);
}

}

github的demo例子:https://github.com/CodeBoy975/aac-to-mp3-demo.git

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/23742.html

(0)
上一篇 2023-09-05 19:33
下一篇 2023-09-06 17:00

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信