首页 > 代码库 > ffmpeg解码文件

ffmpeg解码文件

只贴出部分代码,重在说明过程


av_register_all();

AVFormatContext* fctx = avformat_alloc_context();
int err = avformat_open_input(&fctx, "b.avi", NULL, NULL);

printf("open input:%d\n", err);

err = avformat_find_stream_info(fctx, NULL);

printf("find stream info:%d, streams:%d\n", err, fctx->nb_streams);

int video_id = -1;
for (int i = 0; i < fctx->nb_streams; i++)    //区分视频流和音频流
{
    if (fctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) //找到视频流,这里也可以换成音频
    {
        video_id = i;
        break;
    }
}

printf("video_id:%d\n", video_id);

AVCodec* codec = avcodec_find_decoder(fctx->streams[video_id]->codec->codec_id);
AVCodecContext* cctx = fctx->streams[video_id]->codec;
err = avcodec_open2(cctx, codec, NULL);    // 打开解码器

printf("open codec:%d\n", err);

int width = cctx->width;
int height = cctx->height;

printf("width:%d, height:%d\n", width, height);

AVRational frame_rate = fctx->streams[video_id]->r_frame_rate;

AVPicture pc;
AVPicture* pic = &pc;
avpicture_alloc(pic, PIX_FMT_RGB24, width, height);
AVFrame* frame = av_frame_alloc();

SwsContext* sctx = sws_getContext(width, height, cctx->pix_fmt, width, height, (PixelFormat)PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);

IplImage* img = cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, 3);
img->imageSize = pic->linesize[0];
img->imageData = http://www.mamicode.com/(char *)pic->data[0];>

ffmpeg解码文件