原创

音频转码工具(java)

作者:cndz 围观群众:714 更新于 标签:java音频转码工具类在线工具类

jave简介

JAVE2 (Java Audio Video Encoder)库是ffmpeg项目的Java包装器。开发人员可以利用JAVE2将音频和视频文件从一种格式转换为另一种格式例如,您可以将AVI文件转码为MPEG文件,可以将DivX视频流更改为(类似youtube的)Flash FLV视频流,可以将WAV音频文件转换为MP3或Ogg Vorbis音频文件,可以分离和转码音频和视频轨道,可以调整视频大小,更改其大小和比例等等。

maven引入

 <dependency>
                <groupId>ws.schild</groupId>
                <artifactId>jave-all-deps</artifactId>
                <version>2.7.1</version>
            <exclusions>
                <!--  排除windows 32位系统      -->
                <exclusion>
                    <groupId>ws.schild</groupId>
                    <artifactId>jave-nativebin-win32</artifactId>
                </exclusion>
                <!--  排除linux 32位系统      -->
                <exclusion>
                    <groupId>ws.schild</groupId>
                    <artifactId>jave-nativebin-linux32</artifactId>
                </exclusion>
                <!-- 排除Mac系统-->
                <exclusion>
                    <groupId>ws.schild</groupId>
                    <artifactId>jave-nativebin-osx64</artifactId>
                </exclusion>
                <!-- 排除osxm-->
                <exclusion>
                    <groupId>ws.schild</groupId>
                    <artifactId>jave-nativebin-osxm1</artifactId>
                </exclusion>
                <!-- 排除arm-->
                <exclusion>
                    <groupId>ws.schild</groupId>
                    <artifactId>jave-nativebin-linux-arm32</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>ws.schild</groupId>
                    <artifactId>jave-nativebin-linux-arm64</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

音频转换工具类(其他格式转mp3/wav)

package top.jhone.common.utils;

import lombok.extern.slf4j.Slf4j;
import ws.schild.jave.AudioAttributes;
import ws.schild.jave.Encoder;
import ws.schild.jave.EncodingAttributes;
import ws.schild.jave.MultimediaObject;

import java.io.File;

/**
 * @author:zzy
 * @date:Created in 2023/5/21 17:50
 * @modified By:zzy
 */
@Slf4j
public class AudioConvertUtil {

    public static String convertwav(String sourcePath, String targetPath) {
        File source = new File(sourcePath);
        File target = new File(targetPath);

        boolean rs = false;
        try {

            // Audio Attributes
            AudioAttributes audio = new AudioAttributes();
            audio.setCodec("pcm_s16le");
            audio.setBitRate(16000);
            audio.setChannels(1);
            audio.setBitRate(16000);
            audio.setSamplingRate(16000);

            // Encoding attributes
            EncodingAttributes attrs = new EncodingAttributes();
            attrs.setFormat("wav");
            attrs.setAudioAttributes(audio);

            // Encode
            Encoder encoder = new Encoder();
            encoder.encode(new MultimediaObject(source), target, attrs);
            rs = true;
        } catch (Exception e) {
            log.error("convertMP3 errpr : ", e);
        }
        return targetPath;
    }

    public static String convertMp3 (String sourcePath, String targetPath) {
        File source = new File(sourcePath);
        File target = new File(targetPath);

        boolean rs = false;
        try {

            // Audio Attributes
            AudioAttributes audio = new AudioAttributes();
            audio.setCodec("libmp3lame");
            audio.setBitRate(new Integer(128000));
            audio.setChannels(new Integer(2));
            audio.setSamplingRate(new Integer(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);
        } catch (Exception e) {
            log.error("convertMP3 errpr : ", e);
        }
        return targetPath;
    }

}
加入收藏