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

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

ffplay 学习记录01_第1张图片
安装依赖

编译完成


ffplay 学习记录01_第2张图片

本篇记录内容

介绍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输出的文件信息:

ffplay 学习记录01_第3张图片

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

3. 打开一路视频流

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

videoStream=-1;
    for(i=0; inb_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; ydata[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

你可能感兴趣的:(ffplay 学习记录01)