基于ffmpeg 获取视频帧保存成图像转成yuv图像序列

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
// Decode packets until we have decoded a complete frame
while(true)
{
// Work on the current packet until we have decoded all of it
while(bytesRemaining > 0)
av_free_packet(&packet);
// Read new packet
if(/*av_read_packet*/av_read_frame(pFormatCtx, &packet)<0)
goto loop_exit;
memcpy(&packettest, &packet, sizeof(AVPacket));
printf("%d\n", packet.stream_index);
} while(packet.stream_index!=videoStream);
bytesRemaining=packet.size;
rawData=packet.data;
}
loop_exit:
// Decode the rest of the last frame
if(bytesDecoded < 0)
{
fprintf(stderr, "Error while decoding frame\n");
return false;
}
bytesRemaining-=bytesDecoded;
rawData+=bytesDecoded;
// Did we finish the current frame? Then we can return
return;
// Write header
// fprintf(pFile, "P6\n%d %d\n255\n", width, height);
// Write pixel data
fwrite(&m_fileHeader, sizeof(m_fileHeader), 1, pFile);
fwrite(&m_infoHeader, sizeof(m_infoHeader), 1, pFile);
for(i = 0; i < width; i++) {
ucRGB = ucTmp[3*i];
ucTmp[3*i] = ucTmp[3*i+2];
ucTmp[3*i+2] = ucRGB;
}
ucRGB = 0;
fwrite(ucTmp, 1, width*3, pFile);
fwrite(&ucRGB, 1, (uiTmp-uiTmp2)/height , pFile);
//保留。设置为0
m_fileHeader.bfReserved1 = 0;
//保留。设置为0
m_fileHeader.bfReserved2 = 0;
//从文件开始到位图数据的偏移量(单位:字节)
m_fileHeader.bfOffBits = sizeof(m_fileHeader) + sizeof(m_infoHeader);
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"swscale.lib")
bool GetNextFrame(AVFormatContext *pFormatCtx, AVCodecContext *pCodecCtx, int videoStream, AVFrame *pFrame)
//位图的面数(为1)
m_infoHeader.biPlanes = 1;
//每个像素的位数
m_infoHeader.biBitCount = 24;
//压缩说明。0(BI_RGB)表示不压缩
m_infoHeader.biCompression = 0;
//用字节数表示的位图数据的大小(为4的位数)
for(y=height-1; y>=0; y--) {
if(ucTmp != NULL) {
delete []ucTmp;
ucTmp = NULL;
}
ucTmp = new unsigned char[width*3];
memcpy(ucTmp, pFrame->data[0]+y*pFrame->linesize[0], width*3);
// fmt = av_find_input_format("flv");
// pFormatCtx->iformat = fmt;
// Open video file
if(av_open_input_file(&pFormatCtx,filename, /*fmt*/NULL, 0, NULL)!=0)
//重要的颜色数
m_infoHeader.biClrImportant = 0;
/////////////////////
// Open file
sprintf(szFilename, "frame%d.bmp", iFrame);
pFile=fopen(szFilename, "wb");
if(pFile==NULL)
//信息头长度(单位:字节)。典型值为28
m_infoHeader.biSize = 0x28;
//位图宽度(单位:像素)
m_infoHeader.biWidth = width;
//位图高度(单位:像素)。若其为正,表示倒向的位图。若为负,表示正向的位图
m_infoHeader.biHeight = height;
}
// Close file
fclose(pFile);
}
int main(/*int argc, char *argv[]*/)
{
AVFormatContext *pFormatCtx;
unsigned int i;
int videoStream;
AVCodecContext *pCodecCtx;
if(frameFinished)
return true;
}
// Read the next packet, skipping all packets that aren't for this
// stream
do
{
++ii;
// Free old packet
if(packet.data!=NULL)
m_infoHeader.biSizeImage = uiTmp;
//水平分辨率(单位:像素/米)
m_infoHeader.biXPelsPerMeter = 0;
//垂直分辨率(单位:像素/米)
m_infoHeader.biYPelsPerMeter = 0;
//位图使用的颜色数
m_infoHeader.biClrUsed = 0;
// scanf("%s",filename);
strcpy(filename,"chicken.avi");
// Register all formats and codecs
av_register_all();
//avformat_network_init();
//pFormatCtx=av_alloc_format_context();
{
// Decode the next chunk of data
bytesDecoded=avcodec_decode_video2(pCodecCtx, pFrame,
&frameFinished, &packet);//, rawData, bytesRemaining);
// Was there an error?
{
static AVPacket packet;
static int bytesRemaining=0;
static uint8_t *rawData;
static bool fFirstTime=true;
int bytesDecoded;
int frameFinished;
int ii = 0;
{
videoStream=i;
break;
}
if(videoStream==-1)
return -1; // Didn't find a video stream
// Get a pointer to the codec context for the video stream
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
#include "libswscale/swscale.h"
#pragma comment(lib,"avcodec.lib")
return -1; // Couldn't open file
// Retrieve stream information
if(av_find_stream_info(pFormatCtx)<0)
return -1; // Couldn't find stream information
// Dump information about file onto standard error
#include <stdio.h>
#include <windows.h>
#include "string.h"
#include "stdlib.h"
// #include "avcodec.h"
// #include "avformat.h"
// #include "avutil.h"
// #include "flvdec.h"
AVCodec *pCodec;
AVFrame *pFrame;
AVFrame *pFrameRGB;
int numBytes;
uint8_t *buffer;
AVInputFormat *fmt;
char filename[50];
//printf("please input a filename:\n");
bytesDecoded=avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished,
&packet);//, rawData, bytesRemaining);
// Free last packet
if(packet.data!=NULL)
av_free_packet(&packet);
BITMAPFILEHEADER m_fileHeader;
BITMAPINFOHEADER m_infoHeader;
unsigned int uiTmp, uiTmp2;
unsigned char *ucTmp = NULL;
unsigned char ucRGB;
int i;
uiTmp = (width*3+3)/4*4*height;
AVPack来自百度文库t packettest;
// First time we're called, set packet.data to NULL to indicate it
// doesn't have to be freed
if(fFirstTime)
{
fFirstTime=false;
packet.data=NULL;
dump_format(pFormatCtx, 0, filename, false);
// Find the first video stream
videoStream=-1;
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)
return frameFinished!=0;
}
void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame)
{
FILE *pFile;
char szFilename[32];
int y;
/////////////////////
uiTmp2 = width*height*3;
//文件标识"BM"(即0x4D42)表示位图
m_fileHeader.bfType = 0x4D42;
//整个文件的大小(单位:字节)
m_fileHeader.bfSize = sizeof(m_fileHeader) + sizeof(m_infoHeader) + uiTmp;
相关文档
最新文档