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

↘锁芯ラ 提交于 2019-12-13 08:12:10

问题


I'm using native LAME code for a part of my Android application. This code is supposed to take a string pointing to the input file (M4A) and a string to the output file (MP3). I found some code that seems, from what I can gather, to do such a thing. However, when I play back the MP3 files, all I hear is a ZIP! sound. No matter how long the recording is, I get the same sound.

I was thinking that this had something to do with the sample rate, so I've tried all of the standard ones, and get nearly the same result (some zips are less... zippy).

Here is the the only C code that I've been able to find, however, I'm open to using a C++ solution, as well.

#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); 
 }

回答1:


You are not decoding the M4A audio into raw audio data before sending it into LAME.

"PCM audio" means no compression.



来源:https://stackoverflow.com/questions/9139004/c-or-c-code-for-using-lame-api-to-convert-an-m4a-mpeg-4-audio-to-mp3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!