繁体   English   中英

C或C ++代码,用于使用LAME API将M4A(MPEG-4音频)转换为MP3

[英]C, or C++ code for using LAME API to convert an M4A (MPEG-4 audio) to MP3

我将本地LAME代码用于我的Android应用程序的一部分。 该代码应采用指向输入文件(M4A)的字符串和指向输出文件(MP3)的字符串。 我发现,从我所能收集的内容来看,有些代码可以做到这一点。 但是,当我播放MP3文件时,我听到的只是一个ZIP! 声音。 无论录音多长时间,我都会得到相同的声音。

我当时以为这与采样率有关,所以我尝试了所有标准采样率,并获得了几乎相同的结果(某些zip更少... zippy)。

这是我唯一能找到的C代码,但是,我也愿意使用C ++解决方案。

#include <jni.h>  
#include <string.h>
#include <stdio.h>
#include <android/log.h>
#include "lame.h"

#define DEBUG_TAG "WBA"  

void Java_net_smartnotes_media_RawAudioRecorder_transcode(JNIEnv * env, jobject this, jstring from, jstring to) {
    jboolean isCopy;  
    const char * szFrom = (*env)->GetStringUTFChars(env, from, &isCopy);
    const char * szTo = (*env)->GetStringUTFChars(env, to, &isCopy);

    __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szFrom);
    __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szTo);

    int read, write;

    FILE *pcm = fopen(szFrom, "rb");
    FILE *mp3 = fopen(szTo, "wb");

    const int PCM_SIZE = 8192;
    const int MP3_SIZE = 8192;

    short int pcm_buffer[PCM_SIZE*2];
    unsigned char mp3_buffer[MP3_SIZE];

    lame_t lame = lame_init();
    lame_set_in_samplerate(lame, 44100);
    lame_set_VBR(lame, vbr_default);
    lame_init_params(lame);

    do {
        read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
        if (read == 0)
            write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
        else
            write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
        fwrite(mp3_buffer, write, 1, mp3);
    } while (read != 0);

    lame_close(lame);
    fclose(mp3);
    fclose(pcm);

    (*env)->ReleaseStringUTFChars(env, from, szFrom);
    (*env)->ReleaseStringUTFChars(env, to, szTo); 
 }

在将M4A音频发送到LAME之前,您没有将其解码为原始音频数据。

“ PCM音频”表示无压缩。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM