如何使用libavcodec解码wmp3视频? (FFMPEG)

时间:2012-03-13 04:10:46

标签: cocoa ffmpeg ios5 libavcodec

我正在开发一个从互联网上读取直播电视流的应用程序,我需要在我的ipad应用程序上播放它。

我已将ffmpeg编译到我的ipad应用程序中以使用libavcodec lib,但我无法在此lib中使用它...

我知道如何获取流数据包,如果是音频或视频数据包则读取,但我不知道如何使用lib将原始数据包编解码器转换为h264和mp3编解码器......

我需要将流数据包wmv3转换为h264并将其保存在文件中。

我的代码如下......

AVFormatContext* pFormatCtx = avformat_alloc_context();
int             i, videoStream, audioStream;
AVCodecContext  *pCodecCtx;
AVCodecContext *aCodecCtx;
AVCodec         *pCodec;
AVCodec         *aCodec;
AVFrame         *pFrame; 
AVFrame         *pFrameRGB;
AVPacket        packet;
int             frameFinished;
int             numBytes;
uint8_t         *buffer;

static struct SwsContext *img_convert_ctx;


// Register all formats and codecs
av_register_all();
avcodec_register_all();
avformat_network_init();

// Open video file
if(avformat_open_input(&pFormatCtx, [objURL cStringUsingEncoding:NSASCIIStringEncoding] ,NULL,NULL) != 0){
    return -1;
}

// Retrieve stream information
if(avformat_find_stream_info(pFormatCtx, NULL)<0)
    return -1; // Couldn't find stream information

// Dump information about file onto standard error
av_dump_format(pFormatCtx, 0, [objURL cStringUsingEncoding:NSASCIIStringEncoding], 0);

// Find the first video stream
videoStream=-1;
audioStream=-1;
for(i=0; i < pFormatCtx->nb_streams; i++) {
    if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO && videoStream < 0) {
        videoStream=i;
    }
    if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO && audioStream < 0) {
        audioStream=i;
    }
}
if(videoStream==-1)
    return -1; // Didn't find a video stream
if(audioStream==-1)
    return -1;

// Get a pointer to the codec context for the video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec;

// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL) {
    fprintf(stderr, "Unsupported codec!\n");
    return -1; // Codec not found
}

// Open codec
if(avcodec_open2(pCodecCtx, pCodec, NULL)<0)
    return -1; // Could not open codec

// Get a pointer to the codec context for the audio stream
aCodecCtx=pFormatCtx->streams[audioStream]->codec;

// Find the decoder for the audio stream
aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
if(!aCodec) {
    fprintf(stderr, "Unsupported codec!\n");
    return -1; // Codec not found
}

// Open codec
if(avcodec_open2(aCodecCtx, aCodec, NULL)<0)
    return -1; // Could not open codec

// Allocate video frame
pFrame=avcodec_alloc_frame();

// Allocate an AVFrame structure
pFrameRGB=avcodec_alloc_frame();
if(pFrameRGB==NULL)
    return -1;

// Determine required buffer size and allocate buffer
numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
                            pCodecCtx->height);
buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

// Assign appropriate parts of buffer to image planes in pFrameRGB
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
// of AVPicture
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
               pCodecCtx->width, pCodecCtx->height);

// Read frames and save first five frames to disk
i=0;
while(av_read_frame(pFormatCtx, &packet)>=0) {
    // Is this a packet from the video stream?
    if(packet.stream_index==audioStream) {
        NSLog(@"Audio.. i'll solve the video first...");
    } else if(packet.stream_index==videoStream) {

        /// HOW CONVERT THIS VIDEO PACKET TO H264 and save on a file? :(
    }

    // Free the packet that was allocated by av_read_frame
    av_free_packet(&packet);
}

// Free the RGB image
av_free(buffer);
av_free(pFrameRGB);

// Free the YUV frame
av_free(pFrame);

// Close the codec
avcodec_close(pCodecCtx);

// Close the video file
avformat_close_input(&pFormatCtx);

return 0;

0 个答案:

没有答案