2012-12-17 2 views
1

iOS에서 WMA 파일을 MP3로 변환하고 싶습니다. 그리고 여기에 예제 코드를 배우면서 AVFrame의 WMA 스트림을 구문 분석했습니다. http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html 다음으로 AVFrame을 MP3 파일로 변환하는 방법을 모르지만이 코드를 시도했지만 작동하지 않습니다.ffmpeg wma to mp3 in iOS

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

내가 잘못 될 수입니다 swr_convert 시도 할 수 있지만,는 FFmpeg 당신이 libLame을 필요로 MP3를 만들 수 있도록 내가 생각하고 몇 가지 라이센스 문제가있을 수 있습니다 . 그래서 저는 왜 wma를 재생하지 않는 것이 아니라 mp3로 변환해야하는지에 대한 질문을 던집니다. libmms 또는 무언가를 사용하여 pcm으로 변환 한 다음 ios의 audioQueue API 또는 AudioUnit API를 사용하여 스트림 또는 file.https를 재생할 수 있도록 패킷을 가져 오는 것으로 가정합니다. //github.com/mooncatventures-group/sampleDecoder/ blob/master/AudioDecodeController.m –

+0

Michelle Cannon에게 감사드립니다. libmms + ffmpeg를 사용하여 WMA 스트림을 재생하는 작은 응용 프로그램을 만들고 있습니다. 그리고 난 AVFrame 단계를 해독 재생에 붙어 있어요. 예제 코드가 작동하지 않고 일부 파일이 누락되었습니다. 다른 예가 있습니까? 감사. –

+0

@MichelleCannon은 달 탐험 집단을 아직도 움직이게합니까? bcoz 나는 그것을 구현하려고 노력했는데 RTSP 링크에서 비디오를 스트리밍하는 프로젝트를 사용하지 못했습니다. – Dilip

답변

0

이 상용 제품의 경우 당신은이 코드는 참조

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