最近在学数字音视频的课,就研究了下如何从一个视频文件中提取视频流存为YUV以及提取视频文件中的音频文件存为PCM。
由于我是初学者,对于ffmpeg并不是很熟悉,在这里大的框架主要还是参考了雷大神的代码。
话不多说,直接上代码,如下:
#include <stdio.h>
#define __STDC_CONSTANT_MACROS
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
};
int main(int argc, char* argv[])
{
AVFormatContext *pFormatCtx;
int i, videoindex,audioindex;
AVCodecContext *pCodecCtx,*iaCodecCtx;
AVCodec *pCodec,*iaCodec;
AVFrame *pFrame,*pFrameYUV,*aFrame;
uint8_t *out_buffer,*buffer;
AVPacket *packet;
int y_size;
int ret, got_picture;
struct SwsContext *img_convert_ctx;
//输入文件路径
char filepath[]="Titanic.ts";
int frame_cnt = 0;
av_register_all();
//avformat_network_init();
pFormatCtx = avformat_alloc_context();
if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){
printf("Couldn't open input stream.\n");
return -1;
}
if(avformat_find_stream_info(pFormatCtx,NULL)<0){
printf("Couldn't find stream information.\n");
return -1;
}
videoindex=-1;
a