ffmpeg入门

总入口

http://blog.csdn.net/leixiaohua1020/article/details/15811977

 

各结构体介绍

http://blog.csdn.net/leixiaohua1020/article/details/14215833

 

简单的ffmpeg+sdl例子视频教学

http://blog.csdn.net/leixiaohua1020/article/details/47068015

 

关于解码与播放,例子是音频

http://blog.csdn.net/leixiaohua1020/article/details/46890259

 

ffmpeg函数调用流程

http://zhuang0393.blog.163.com/blog/static/9285104201302174532913/

/*示例代码*/
#include <stdio.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> int main(int argc, char **argv) { uint8_t * out_buffer; AVCodec *pCodec; // 表示音视频编解码器 AVFrame *pFrame; // 原始数据 AVFrame *pFrameYUV; AVPacket packet; // 代表音视频数据帧 AVCodecContext *pCodecCtx; // 当前 Codec 使用的上下文 AVFormatContext *pFormatCtx;//当前文件容器格式使用的上下文 char filepath[20] = "1.mp4"; int got_picture, videoStream, i; av_register_all(); //注册所有的编解码器,复用/解复用器等等组件。 avformat_network_init(); //网络流初始化 pFormatCtx = avformat_alloc_context();

  //打开文件并读取头信息
    if (avformat_open_input(&pFormatCtx,filepath,NULL,NULL) != 0){
        printf("%s, %d\n", __func__, __LINE__);
        return 1;
    }

  //查找 stream info
    if (avformat_find_stream_info(pFormatCtx,NULL) < 0) {
        printf("%s, %d\n", __func__, __LINE__);
        return 1;
    }

    // 查找第一个视频流
    videoStream = -1;
    for(i = 0 ;i < pFormatCtx->nb_streams; i++ )
        if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
        {   
            videoStream = i ;
            break;
        }   
    if (videoStream == -1)
        return -1;

    pCodecCtx = pFormatCtx ->streams[videoStream]->codec;

  //查找解码器
    pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
  //打开解码器
    avcodec_open2(pCodecCtx, pCodec,NULL);
  
    pFrame=av_frame_alloc();
    out_buffer=(uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));

  //循环读取视频帧
    while (av_read_frame(pFormatCtx, &packet) >= 0) {

        printf("index: %d\n", packet.stream_index); // ==videoStream则为视频帧
        avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, &packet); //解码一帧

    //获得sws上下文句柄
        struct SwsContext *img_convert_ctx = NULL;
        img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
                pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);

    //执行自定义转换
        sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
        /*
      这边已经得到一个yuv420p格式的AVFrame原始数据, 我们可以
      1. 把数据保存成一帧一帧的图片
      2. 可以用sdl库直接播放出来
    */
     sws_freeContext(img_convert_ctx);
        av_free_packet(&packet);
    }

    av_frame_free(&pFrame);
    avcodec_close(pCodecCtx);
    avformat_close_input(&pFormatCtx);
}
posted @ 2016-08-10 09:24  chencesc  阅读(189)  评论(0编辑  收藏  举报