基于 ffmpeg 3.4.2 和 vs2017 开发
YUV420p的存储为先Y,再U,再V。
代码主要部分为获取frame,转换成YUV格式,YUV数据保存。
// ffmpeg_lesson01.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavcodec/version.h"
#include "libavformat/avformat.h"
#include "libavfilter/avfiltergraph.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"
#include "libavutil/avutil.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "libswscale/swscale.h"
};
int SaveYuv(unsigned char *buf, int wrap, int xsize, int ysize, char *filename)
{
FILE *f;
int i;
f = fopen(filename, "ab+");
for (i = 0; i1, xsize, f);
}
fclose(f);
return 1;
}
int main()
{
printf("\n\n");
printf("正在检查是否是标准C++:");
#if (!defined(__STDC__))
printf("非标准C!\n");
#elif defined(__STDC_VERSION__)
printf("标准C版本:%ld。", __STDC_VERSION__);
#else
printf("旧的标准C。\n");
#endif
printf("\nHello FFmpeg!\n");
printf("avcodec_version: %d\n", avcodec_version());
printf("\n");
#define VIDEO_PATH "D://xiongmao.flv"
char *outputfilename = "d:\\test3.yuv";
av_register_all();
//注册所有文件格式和编码器的库。
//下面函数将文件头信息读取到传入的参数中。
AVFormatContext *pFormatContext = NULL;
if (avformat_open_input(&pFormatContext,VIDEO_PATH,NULL,NULL) != 0) {
printf("打开输入文件失败!\n");
return -1;
}
else {
printf("文件打开成功!\n");
}
//下面开始检测文件的流信息
if (avformat_find_stream_info(pFormatContext,NULL) < 0) {
printf("未发现文件流信息\n");
return -1;
}
else {
printf("发现文件流信息!\n");
}
//打印文件信息
//av_dump_format(pFormatContext,0,VIDEO_PATH,0);
//查找视频流
int videoStream = -1;
for (int i = 0; i < pFormatContext->nb_streams; i++)
{
if (pFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
printf("发现视频流,索引ID: %d\n",videoStream);
break;
}
}
//查找音频流
int audioStream = -1;
for (int i = 0; i < pFormatContext->nb_streams; i++)
{
if (pFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
audioStream = i;
printf("发现音频流,索引ID: %d\n", audioStream);
break;
}
}
//查找到音视频流之后,开始查找音视频对应的解码器。有关编解码器的信息被称为“编码器上下文”
AVCodecContext *pCodecContext;
AVCodec *pCodec;
//查找视频流的编解码信息
pCodecContext = pFormatContext->streams[videoStream]->codec;
pCodec = avcodec_find_decoder(pCodecContext->codec_id);
if (pCodec == NULL) {
printf("未找到视频流解码器!\n");
return -1;
}else {
printf("找到视频流解码器,ID:%d\n", pCodecContext->codec_id);
}
//打开视频编码器
if (avcodec_open2(pCodecContext,pCodec,NULL) < 0) {
printf("打开解码器失败!");
return -1;
} else {
printf("视频解码器打开成功!\n");
}
//下面要从视频流中读取一个视频帧并保存成24位RGB色的PPM文件。
AVFrame *pFrame;
//下面开始读取数据
int frameFinished;
AVPacket packet;
//旧的版本使用img_convert就可以。后面改成sws_scale.
//功能更强大。需要详细指出源信息和目的帧信息。
SwsContext *pSwsCtx;
pSwsCtx = sws_getContext(pCodecContext->width,pCodecContext->height,pCodecContext->pix_fmt,pCodecContext->width,pCodecContext->height, AV_PIX_FMT_RGB24, SWS_BICUBIC,NULL,NULL,NULL);
int i = 0;
while (av_read_frame(pFormatContext,&packet) >= 0) {
if (packet.stream_index == videoStream) {//是否是视频流的数据包
pFrame = av_frame_alloc();
avcodec_decode_video2(pCodecContext, pFrame, &frameFinished, &packet);
if (frameFinished) {
SaveYuv(pFrame->data[0], pFrame->linesize[0], pCodecContext->width, pCodecContext->height, outputfilename);
SaveYuv(pFrame->data[1], pFrame->linesize[1], pCodecContext->width / 2, pCodecContext->height / 2, outputfilename);
SaveYuv(pFrame->data[2], pFrame->linesize[2], pCodecContext->width / 2, pCodecContext->height / 2, outputfilename);
}
av_free(pFrame);
}
//释放packet
av_free_packet(&packet);
}
//关闭解码器
avcodec_close(pCodecContext);
//关闭打开的文件
avformat_close_input(&pFormatContext);
return 0;
}
保存的yuv文件可以用YUVViewer.exe或者yuvplayer.exe播放