ffplay 学习记录01

背景

MacOS FFmpeg的安装:
https://trac.ffmpeg.org/wiki/CompilationGuide/macOS

brew 安装:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

使用brew安装ffmpeg:
brew install ffmpeg

推荐的编译选项:
brew install ffmpeg --with-fdk-aac --with-tools --with-freetype --with-libass --with-libvorbis --with-libvpx --with-x265

升级:
brew update && brew upgrade ffmpeg

第一次运行产生的文件依赖:

安装依赖

编译完成


本篇记录内容

介绍ffmpeg处理一个视频文件的简单流程,打开视频文件并解码,取出关键帧,保存到本地文件中。结果是一张一张的图片

1. 初始化ffmpeg       
    av_register_all();
    avformat_network_init(); // 播放流媒体文件时才需要,本地文件不需要
    avcodec_register_all();
2. 打开媒体文件 

一个视频文件的基本信息:

  • 是否包含视频、音频
  • 码流的封装格式
  • 视频的编码格式,用于初始化视频解码器
  • 音频的编码格式,用于初始化音频解码器
  • 视频的分辨率、帧率、码率,用于视频的渲染。
  • 音频的采样率、位宽、通道数,用于初始化音频播放器。
  • 码流的总时长,用于展示、拖动 Seek。
  • 其他 Metadata 信息,如作者、日期等,用于展示。

avformat_open_input 这个函数主要负责服务器的连接和码流头部信息的拉取,在ffmpeg中使用这个函数来打开媒体文件:

    // Open video file
    if(avformat_open_input(&pFormatCtx, fileName, NULL, NULL)!=0)
        return -1; // Couldn't open file

avformat_find_stream_info : 用来处理媒体信息的探测和分析工作。 av_dump_format :负责将ffmpeg得到的媒体信息打印出来

    // 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, fileName, 0);

av_dump_format输出的文件信息:

由此可见经过avformat_find_stream_info的处理能够得媒体的封装格式,总时长,流信息,metaData,码率,帧率,编码格式等信息。

3. 打开一路视频流

pFormatCtx->streams 是一个 AVStream 指针的数组,里面包含了媒体资源的每一路流信息,数组的大小为 pFormatCtx->nb_streams, 根据类型AVMEDIA_TYPE_VIDEO我们可以取出视频流在nb_streams 中的index。

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; // Didn't find a video stream

4. 打开解码器
  • AVStream中得到AVCodecContext
  • 根据AVCodecContext的codec_id 得到AVCodec
  • 调用avcodec_open2打开解码器
// 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, &optionsDict)<0)
        return -1; // Could not open codec

5. 创建一个AVFrame 存放RGB数据
  • 创建一个AVFrame:pFrameRGB
  • 获取格式为PIX_FMT_RGB24,宽为pCodecCtx->width,高为pCodecCtx->height 的数据在内存中所占的大小numBytes
  • 使用av_malloc申请一块内存
  • 使用avpicture_fill填充 pFrameRGB
    // Allocate an AVFrame structure
    pFrameRGB=av_frame_alloc();
    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);
6. 解码
  • 使用函数av_read_frame 对视频进行解封装操作
  • avcodec_decode_video2 进行解码操作,解码出来的数据存放在pFrame中,ffmpeg解码出来的数据一般为YUV格式的数据
  • 需要调用sws_scale把 YUV格式的数据转化为RGB24类型,并存放到pFrameRGB中
    while(av_read_frame(pFormatCtx, &packet)>=0) {
        // Is this a packet from the video stream?
        if(packet.stream_index==videoStream) {
            // Decode video frame
            avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished,
                                  &packet);
            
            // Did we get a video frame?
            if(frameFinished) {
                // Convert the image from its native format to RGB
                sws_scale
                (
                 sws_ctx,
                 (uint8_t const * const *)pFrame->data,
                 pFrame->linesize,
                 0,
                 pCodecCtx->height,
                 pFrameRGB->data,
                 pFrameRGB->linesize
                 );
                
                // Save the frame to disk
                if(++i<=100)
                    SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height,
                              i);
            }
        }    
        // Free the packet that was allocated by av_read_frame
        av_free_packet(&packet);
    }
    
7. 文件储存到本地
void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) {
    FILE *pFile;
    char szFilename[32];
    int  y;
    // Open file
    // 字符串格式化命令,主要功能是把格式化的数据写入某个字符串中。sprintf 是个变参函数。
    sprintf(szFilename, "frame%d.ppm", iFrame);
    pFile=fopen(szFilename, "wb");
    if(pFile==NULL)
        return;
    
    // Write header
    fprintf(pFile, "P6\n%d %d\n255\n", width, height);
    
    /* Write pixel data
     size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
     注意:这个函数以二进制形式对文件进行操作,不局限于文本文件
     返回值:返回实际写入的数据块数目
     (1)buffer:是一个指针,对fwrite来说,是要获取数据的地址;
     (2)size:要写入内容的单字节数;
     (3)count:要进行写入size字节的数据项的个数;
     (4)stream:目标文件指针;
     返回值 返回实际写入的数据项个数count。
     */
    for(y=0; y<height; y++)
        fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);
    
    // Close file
    fclose(pFile);
}

编译执行

使用xcode运行后,最终在xx/ffmpeg_tutorial/DerivedData/Build/Products/Debug 目录下生成了100张视频关键帧的图片

参考Demo:https://github.com/zjunchao/ffmpeg_tutorial/tree/master/tutorial01

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容