2012-12-17 100 views
1

我想在iOS中將WMA文件轉換爲MP3。我已經通過學習示例代碼在這裏成功解析WMA流到AVFramehttp://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html 但接下來我不知道如何將AVFrame轉換爲MP3文件,並且我嘗試了這樣的代碼,但它不起作用:ffmpeg在iOS中將wma轉換爲mp3

AVCodecContext *outCodecContext = NULL; 
AVCodec *pCodec = avcodec_find_decoder(AV_CODEC_ID_MP3); 
if(pCodec == NULL){ 
    fprintf(stderr, "out avcodec_find_decoder error\n"); 
    exit(1); 
} 

AVCodecContext *pCodecCtx = avcodec_alloc_context3(pCodec); 

if(pCodecCtx == NULL){ 
    fprintf(stderr, "out avcodec_alloc_context3 error\n"); 
    exit(1); 
} 

if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0){ 
    fprintf(stderr, "out avcodec_open error\n"); 
    exit(1); 
} 
outCodecContext = pCodecCtx; 


do{ 
    AVPacket packet; 
    if(av_read_frame(pFormatCtx, &packet) < 0){ 
     break; 
    } 

    int got_frame_ptr; 

    if(packet.stream_index==videoStream) 
    { 
     avcodec_decode_audio4(pCodecCtx, pFrame, &got_frame_ptr, &packet); 
     if(got_frame_ptr) 
     { 
      AVPacket outPacket; 
      int out_got_packet_ptr; 
      avcodec_encode_audio2(outCodecContext, &outPacket, pFrame, &out_got_packet_ptr); 

      [finalData appendBytes:outPacket.data length:outPacket.size]; 
      NSLog(@"finalData len: %d", [finalData length]); 
     } 
    } 
    av_free_packet(&packet); 

}while(true); 

NSString *svfp = [getAppDocumentDirectory() stringByAppendingPathComponent:@"finalData"]; 
[finalData writeToFile:svfp atomically:YES]; 

我總是在控制檯消息:

[mp3 @ 0xa393200] nb_samples (12288) != frame_size (0) (avcodec_encode_audio2) 

任何一個可以幫助?謝謝。

+1

我可能是錯的,但我覺得ffmpeg的創建你需要libLame MP3,並且可能有一些許可證的問題,如果這是一個商業產品。所以我想這個問題是爲什麼你需要轉換爲mp3爲什麼不只是玩wma。我假設你使用libmms或某些東西來獲取數據包,以便您可以轉換爲pcm,然後使用ios的audioQueue api或AudioUnit api來播放流或文件.https://github.com/mooncatventures-group/sampleDecoder/ blob/master/AudioDecodeController.m –

+0

謝謝米歇爾坎農,你猜對了,我正在使用libmms + ffmpeg播放WMA流的小應用程序。我被困在播放的解碼AVFrame步驟。示例代碼似乎不起作用,並且某些文件丟失。你還有其他的例子嗎?謝謝。 –

+0

@MichelleCannon做mooncatventures-group還在工作嗎? bcoz我試圖實現它,並沒有成功地使用任何在RTSP鏈接中流視頻的項目。 – Dilip

回答

0

您可以嘗試swr_convert 此代碼是供你參考

swr_convert(_swrContext, 
          outbuf, 
          _audioFrame->nb_samples * ratio, 
          (const uint8_t **)_audioFrame->data, 
          _audioFrame->nb_samples);