2

我正在开发一个用于将 .opus 文件转换为 .mp3 的 android 应用程序。我正在使用本机代码来执行此操作。下面是我正在使用的函数的源代码。.mp3 输出以 0 字节生成。:(

方法

JNIEXPORT void JNICALL Java_methodName 

(JNIEnv *env, jclass 类, jstring opus_path, jstring empty, jstring dest_path) {

  const char *outputStr = (*env)->GetStringUTFChars(env, dest_path, 0);
  const char *opusStr = (*env)->GetStringUTFChars(env, opus_path, 0);
  const char *v19 = (*env)->GetStringUTFChars(env, test, 0);
  FILE *stream = fopen(outputStr, "wb");
  lame_t lame = lame_init();
  lame_set_in_samplerate(lame, 48000);
lame_set_brate(lame,64);
lame_init_params(lame);
cleanupPlayer();
initPlayer(opusStr);
jint *v20;
jbyte *v22;
jbyte *v23;
jint v12;
jint v13;
jint v21;
jint v16;
jint v17;


 do
  {
    fillBuffer(v22, 16384, v20);

    v12 = lame_encode_buffer(lame, v22, v22, v20, v23, 8192);
    fwrite(v23, v12, 1, stream);
  }
  while ( v20 != 1 );
  v13 = lame_encode_flush(lame, v23, 8192);
    fwrite(v23, v13, 1, stream);
    cleanupPlayer(); 
    fclose(stream);
    lame_close(lame);     
  }

填充缓冲区:代码

void fillBuffer(uint8_t *buffer, int capacity, int *args) {
if (_opusFile) {
    args[1] = max(0, op_pcm_tell(_opusFile));

    if (_finished) {
        args[0] = 0;
        args[1] = 0;
        args[2] = 1;
        return;
    } else {
        int writtenOutputBytes = 0;
        int endOfFileReached = 0;

        while (writtenOutputBytes < capacity) {
            int readSamples = op_read(_opusFile, (opus_int16 *)(buffer + writtenOutputBytes), (capacity - writtenOutputBytes) / 2, NULL);

            if (readSamples > 0) {
                writtenOutputBytes += readSamples * 2;
            } else {
                if (readSamples < 0) {
                    LOGE("op_read failed: %d", readSamples);
                }
                endOfFileReached = 1;
                break;
            }
        }

        args[0] = writtenOutputBytes;

        if (endOfFileReached || args[1] + args[0] == _totalPcmDuration) {
            _finished = 1;
            args[2] = 1;
        } else {
            args[2] = 0;
        }
    }
    } else {
        memset(buffer, 0, capacity);
        args[0] = capacity;
        args[1] = _totalPcmDuration;
    }
}

我被困在这一点上。有人可以帮我解决这个问题,转换.opus.mp3使用 opus & lame。

4

0 回答 0