java实现任意音频格式转mp3(任意音频格式互转)

需求

最近在集成一款物联网产品获取环境录音功能,从设备那边拿到的录音文件是amr格式的,小程序那边会有兼容性问题,所以我们需要转换为mp3格式。
在这里插入图片描述

Java实现

这里我们使用JAVE2库来实现音频格式转码,项目管理工具使用的Maven

The JAVE2 (Java Audio Video Encoder) library is Java wrapper on the ffmpeg project. Developers can take take advantage of JAVE2 to transcode audio and video files from a format to another. In example you can transcode an AVI file to a MPEG one, you can change a DivX video stream into a (youtube like) Flash FLV one, you can convert a WAV audio file to a MP3 or a Ogg Vorbis one, you can separate and transcode audio and video tracks, you can resize videos, changing their sizes and proportions and so on.

Many other formats, containers and operations are supported by JAVE2.

https://github.com/a-schild/jave2

在项目pom.xml文件里添加依赖
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-core</artifactId>
    <version>3.4.0</version>
</dependency>

<!-- Linux 环境 -->
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-nativebin-linux64</artifactId>
    <version>3.4.0</version>
</dependency>

<!-- MacOS 环境 -->
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-nativebin-osx64</artifactId>
    <version>3.4.0</version>
</dependency>

<!-- Win64 环境 -->
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-nativebin-win64</artifactId>
    <version>3.4.0</version>
</dependency>

tips: 上面的环境依赖按需添加

代码:将音频转换为mp3格式
/**
 * 将音频转换为mp3
 *
 * @param source 源音频文件
 * @param target 输出的音频文件
 */
public static void audioConvert2Mp3(File source, File target)
{
    try
    {
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libmp3lame");
        audio.setBitRate(128000);
        audio.setChannels(2);
        audio.setSamplingRate(44100);
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setOutputFormat("mp3");
        attrs.setAudioAttributes(audio);
        Encoder encoder = new Encoder();
        encoder.encode(new MultimediaObject(source), target, attrs);
    }
    catch(Exception ex)
    {
        log.error(ex.getMessage(), ex);
    }
}
更多高级用法

详见 github

换下面这些音频和视频,压缩包有包和源码,还有文档api. 整理资料不易,不喜欢可以自己百度 Audio decoders adpcm_4xm adpcm_adx adpcm_ct adpcm_ea adpcm_ea_r1 adpcm_ea_r2 adpcm_ea_r3 adpcm_ea_xas adpcm_ima_amv adpcm_ima_dk3 adpcm_ima_dk4 adpcm_ima_ea_eacs adpcm_ima_ea_sead adpcm_ima_qt adpcm_ima_smjpeg adpcm_ima_wav adpcm_ima_ws adpcm_ms adpcm_sbpro_2 adpcm_sbpro_3 adpcm_sbpro_4 adpcm_swf adpcm_thp adpcm_xa adpcm_yamaha alac ape atrac 3 cook dca dsicinaudio flac g726 imc interplay_dpcm liba52 libamr_nb libamr_wb libfaad libgsm libgsm_ms mace3 mace6 mp2 mp3 mp3adu mp3on4 mpc sv7 mpc sv8 mpeg4aac nellymoser pcm_alaw pcm_mulaw pcm_s16be pcm_s16le pcm_s16le_planar pcm_s24be pcm_s24daud pcm_s24le pcm_s32be pcm_s32le pcm_s8 pcm_u16be pcm_u16le pcm_u24be pcm_u24le pcm_u32be pcm_u32le pcm_u8 pcm_zork qdm2 real_144 real_288 roq_dpcm shorten smackaud sol_dpcm sonic truespeech tta vmdaudio vorbis wavpack wmav1 wmav2 ws_snd1 xan_dpcm Audio encoders ac3 adpcm_adx adpcm_ima_wav adpcm_ms adpcm_swf adpcm_yamaha flac g726 libamr_nb libamr_wb libfaac libgsm libgsm_ms libmp3lame libvorbis mp2 pcm_alaw pcm_mulaw pcm_s16be pcm_s16le pcm_s24be pcm_s24daud pcm_s24le pcm_s32be pcm_s32le pcm_s8 pcm_u16be pcm_u16le pcm_u24be pcm_u24le pcm_u32be pcm_u32le pcm_u8 pcm_zork roq_dpcm sonic sonicls vorbis wmav1 wmav2 Video decoders 4xm 8bps VMware video aasc amv asv1 asv2 avs bethsoftvid bmp c93 camstudio camtasia cavs cinepak cljr cyuv dnxhd dsicinvideo dvvideo dxa ffv1 ffvhuff flashsv flic flv fraps gif h261 h263 h263i h264 huffyuv idcinvideo indeo2 indeo3 interplayvideo jpegls kmvc loco mdec mjpeg mjpegb mmvideo mpeg1video mpeg2video mpeg4 mpegvideo msmpeg4 msmpeg4v1 msmpeg4v2 msrle msvideo1 mszh nuv pam pbm pgm pgmyuv png ppm ptx qdraw qpeg qtrle rawvideo roqvideo rpza rv10 rv20 sgi smackvid smc snow sp5x svq1 svq3 targa theora thp tiertexseqvideo tiff truemotion1 truemotion2 txd ultimotion vb vc1 vcr1 vmdvideo vp3 vp5 vp6 vp6a vp6f vqavideo wmv1 wmv2 wmv3 wnv1 xan_wc3 xl zlib zmbv Video encoders asv1 asv2 bmp dnxhd dvvideo ffv1 ffvhuff flashsv flv gif h261 h263 h263p huffyuv jpegls libtheora libx264 libxvid ljpeg mjpeg mpeg1video mpeg2video mpeg4 msmpeg4 msmpeg4v1 msmpeg4v2 pam pbm pgm pgmyuv png ppm qtrle rawvideo roqvideo rv10 rv20 sgi snow svq1 targa tiff wmv1 wmv2 zlib zmbv
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值