ZLameMp3 - MP3转码器

作者: 吾非言 | 来源:发表于2022-06-17 20:05 被阅读0次

ZLameMp3

PCM(WAV)- Lame-MP3转换器

使用Lame将PCM/WAV文件转换成MP3文件。

引入ZLameMp3插件

  1. 在根gradle文件中引入jitpack仓库:
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  1. 在工程gradle文件中引入ZLameMp3插件
dependencies {
    implementation 'com.github.zrunker:ZLameMp3:v1.0.4'
}

使用

ZLameMp3提供使用两种方式进行文件格式转换:

一、Mp3Converter

Mp3Converter通过直接操作so库方法,来对PCM/WAV文件进行操作处理,该类适合开发者自定义实现,常用API如下:

public class Mp3Converter {

    /**
     * init lame
     *
     * @param inSampleRate  input sample rate in Hz
     * @param channel       number of channels
     * @param mode          0 = CBR, 1 = VBR, 2 = ABR.  default = 0
     * @param outSampleRate output sample rate in Hz
     * @param outBitRate    rate compression ratio in KHz
     * @param quality       quality=0..9. 0=best (very slow). 9=worst.<br />
     *                      recommended:<br />
     *                      2 near-best quality, not too slow<br />
     *                      5 good quality, fast<br />
     *                      7 ok quality, really fast
     */
    public native static void init(int inSampleRate, int channel, int mode,
                                   int outSampleRate, int outBitRate, int quality);

    /**
     * file convert to mp3
     * it may cost a lot of time and better put it in a thread
     *
     * @param inputPath    file path to be converted
     * @param mp3Path      mp3 output file path
     * @param inSampleRate input sample rate in Hz
     */
    public native static void convertMp3(String inputPath, String mp3Path, int inSampleRate, int channels);

    /**
     * file convert to mp3
     * it may cost a lot of time and better put it in a thread
     *
     * @param inputPath file path to be converted
     * @param mp3Path   mp3 output file path
     */
    public native static void wavConvertMp3(String inputPath, String mp3Path);

    /**
     * Encode buffer to mp3.
     *
     * @param bufferLeft  PCM data for left channel.
     * @param bufferRight PCM data for right channel.
     * @param samples     number of samples per channel.
     * @param mp3buf      result encoded MP3 stream. You must specified
     *                    "7200 + (1.25 * buffer_l.length)" length array.
     * @return <p>number of bytes output in mp3buf. Can be 0.</p>
     * <p>-1: mp3buf was too small</p>
     * <p>-2: malloc() problem</p>
     * <p>-3: lame_init_params() not called</p>
     * -4: psycho acoustic problems
     */
    public native static int encode(short[] bufferLeft, short[] bufferRight,
                                    int samples, byte[] mp3buf);

    /**
     * Flush LAME buffer.
     * <p>
     * REQUIRED:
     * lame_encode_flush will flush the intenal PCM buffers, padding with
     * 0's to make sure the final frame is complete, and then flush
     * the internal MP3 buffers, and thus may return a
     * final few mp3 frames.  'mp3buf' should be at least 7200 bytes long
     * to hold all possible emitted data.
     * <p>
     * will also write id3v1 tags (if any) into the bitstream
     * <p>
     * return code = number of bytes output to mp3buf. Can be 0
     *
     * @param mp3buf result encoded MP3 stream. You must specified at least 7200
     *               bytes.
     * @return number of bytes output to mp3buf. Can be 0.
     */
    public native static int flush(byte[] mp3buf);

    /**
     * Close LAME.
     */
    public native static void close();

    /**
     * get converted bytes in inputBuffer
     *
     * @return converted bytes in inputBuffer
     * to ignore the deviation of the file size,when return to -1 represents convert complete
     */
    public native static long getConvertBytes();

    /**
     * get library lame version
     *
     * @return lame version
     */
    public native static String getLameVersion();

}

Mp3Converter调用方式也很简单,直接调用静态API即可,例如:

// 获取当前lame版本
Mp3Converter.getLameVersion()

二、AudioToMp3Util

AudioToMp3Util是Mp3Converter的包装类,提供转码进度、错误和完成监听,用法如下:

AudioToMp3Util.getInstance().startConvert(
                "xxx.pcm/xxx.mav", "xx.mp3", new AudioToMp3Util.ConverterListener() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onProgress(float progress) {
                        // 转码进度:"转码中" + progress + "%"
                    }

                    @Override
                    public void onComplete(String mp3Path) {
                        // 转码完成
                    }

                    @Override
                    public void onError(String msg) {
                        // 转码出错
                    }
                });

为防止内存泄露,建议在Activity/Fragment的销毁方法中执行AudioToMp3Util的销毁事件:

@Override
protected void onDestroy() {
    super.onDestroy();
    AudioToMp3Util.getInstance().destroy();
}

阅读原文

相关文章

  • ZLameMp3 - MP3转码器

    ZLameMp3 PCM(WAV)- Lame-MP3转换器[https://github.com/zrunker...

  • iOS pcm转mp3出现尖锐声

    项目中需要将pcm音频文件转码为mp3格式后上传, 在网上查找转码mp3的资料, 应用时转码出来的mp3音频声音比...

  • iOS-使用Lame转码:PCM->MP3

    本文是iOS 使用AUGraph录音同时播放(并转码成MP3)中关于使用Lame将PCM转码成MP3的详细说明。看...

  • iOS音视频开发 - LAME编译

    LAME MP3编码引擎,目前在业界,转码成MP3格式的音频文件时,最常用的编码器就是LAME库。当达到320Kb...

  • windows平台使用wsl为Android编译lame

    lame lame是非常优秀的一种MP3编码引擎,在业界,转码成MP3 格式的音频文件时,最常用的编码器就是lam...

  • ffmpeg的转码问题

    //ffmpeg的转码问题 如果将pcm数据转码为mp3数据时需要设置 //AVSampleFormat samp...

  • 音频开发学习日记2~lame的使用

    前言 lame是目前非常优秀的一种mp3编码引擎,在业界,转码成mp3格式的音频文件时,最常用的编码器就是lame...

  • FFmpeg转码(2)

    FFmpeg音频转码 FFmpeg输出MP3 查看参数,使用之前需要先下载安装libmp3lame MP3的转换 ...

  • iOS 音频转码mp3,wav,amr格式

    主体功能:(Swift版本) 1、将录制的caf格式音频转码mp3格式 2、wav格式与amr格式音频相互转码 申...

  • AVLinearPCMIsFloatKey导致iOS 13转码杂

    前言项目之前用到wav音频转码成mp3的需求,更新到iOS13之后发现转码后的音频只有杂音且时长加倍! 项目用到l...

网友评论

    本文标题:ZLameMp3 - MP3转码器

    本文链接:https://www.haomeiwen.com/subject/quuhvrtx.html