1
我想在iOS中将WMA文件转换为MP3。我已经通过学习示例代码在这里成功解析WMA流到AVFrame
:http://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)
任何一个可以帮助?谢谢。
我可能是错的,但我觉得ffmpeg的创建你需要libLame MP3,并且可能有一些许可证的问题,如果这是一个商业产品。所以我想这个问题是为什么你需要转换为mp3为什么不只是玩wma。我假设你使用libmms或某些东西来获取数据包,以便您可以转换为pcm,然后使用ios的audioQueue api或AudioUnit api来播放流或文件.https://github.com/mooncatventures-group/sampleDecoder/ blob/master/AudioDecodeController.m –
谢谢米歇尔坎农,你猜对了,我正在使用libmms + ffmpeg播放WMA流的小应用程序。我被困在播放的解码AVFrame步骤。示例代码似乎不起作用,并且某些文件丢失。你还有其他的例子吗?谢谢。 –
@MichelleCannon做mooncatventures-group还在工作吗? bcoz我试图实现它,并没有成功地使用任何在RTSP链接中流视频的项目。 – Dilip