使用FFMPEG打开视频文件,并解码保存成一张张的图片。
具体的步骤如下所示:
1.初始化FFMPEG 调用了这个才能正常使用编码器和解码器。使用这个函数完成编码器和解码器的初始化,只有初始化了编码器和解码器才能正常使用,否则会在打开编解码器的时候失败。
av_register_all();
2.接着需要分配一个AVFormatContext,FFMPEG所有的操作都要通过这个AVFormatContext来进行 它是FFMPEG解封装(flv,mp4,rmvb,avi)功能的结构体。AVFormatContext就是对容器或者媒体文件层次的抽象, 容器/文件(Container/File):即特定格式的多媒体文件,比如MP4,flv,mov等
AVFormatContext *pFormatCtx;
pFormatCtx = avformat_alloc_context();
3.调用avformat_open_input打开视频文件
- 第一个参数是AVFormatContext结构体的指针,
- 第二个参数为文件路径;
- 第三个参数用来设定输入文件的格式,如果设为null,将自动检测文件格式;
- 第四个参数用来填充AVFormatContext一些字段以及Demuxer的private选项。 AVFormatContext包含有较多的码流信息参数,通常由avformat_open_input创建并填充关键字段
const char *file_path = "C:/Users/Administrator/Desktop/qt/92/1.mp4";//自己根据路径定义
if (avformat_open_input(&pFormatCtx, file_path, nullptr, nullptr) != 0)
{
printf("can't open the file.");
return -1;
}
4.文件打开成功后就是查找文件中的视频流 avformat_find_stream_info
if (avformat_find_stream_info(pFormatCtx, nullptr) < 0)
{
printf("Could't find stream infomation.");
return -1;