2,543
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include "windows.h"
#include "time.h"
extern "C"
{
#include "libavformat\avformat.h"
#include "libavutil\avutil.h"
#include "libavcodec\avcodec.h"
#include "libswscale\swscale.h"
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
}
void SaveBmp(AVCodecContext *CodecContex, AVFrame *Picture, int width, int height);
#define MAX_BUF_SIZE 256*1024
int _tmain(int argc, char* argv[])
{
int ret=-1, i=0, videoindex=-1,audioindex=-1, nComplete=0, len=0, bianma=0,wa=0,frame_index=0;//wa专用测试值
unsigned char *pEnCodeBuf = new unsigned char[MAX_BUF_SIZE];
char *sourceFile = "666.asf";
char *destFile = "123.mpeg";
av_register_all();
AVFormatContext *pInputFormatContext=NULL;
AVCodec *pInputCodec = NULL;
AVCodecContext *pInputCodecContext = NULL;
AVPacket InPack;
int videoWidth, videoHeight;
AVOutputFormat *pOutputFmt = NULL;
AVFormatContext *pOutFormatContext = NULL;
AVCodecContext *pOutCodecContext = NULL;
AVCodec *pOutCodec = NULL;
AVStream *pOutStream = NULL;
AVPacket OutPack;
AVFrame *OutFrame;
if(ret=avformat_open_input(&pInputFormatContext, sourceFile, NULL, NULL) <0 )
{
//打开输入文件
printf("打开源文件失败 %s—_—\n",sourceFile);
exit(1);
}
if(avformat_find_stream_info(pInputFormatContext,NULL)<0)
{
//查找流信息
printf("找不到相应的解码信息—_—\n");
goto lk_error;
}
av_dump_format(pInputFormatContext, 0, sourceFile, false);
for(i=0; i<pInputFormatContext->nb_streams; i++)
{
if(pInputFormatContext->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
{
videoindex=i;
break;
}
/* else if(pInputFormatContext->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO)
{
audioindex=i;
}*/
}
if(-1 == videoindex)
{
//没有找到视频流
printf("视频流未找到—_—\n");
goto lk_error;
}
pInputCodecContext = pInputFormatContext->streams[videoindex]->codec;/
pInputCodec = avcodec_find_decoder(pInputCodecContext->codec_id);
if(NULL == pInputCodec)
{
printf("解码器未找到—_—\n");
//没有找到解码器
goto lk_error;
}
if(avcodec_open2(pInputCodecContext, pInputCodec,NULL) != 0)
{
printf("解码器打开失败了—_—\n");//打开解码器失败
goto lk_error;
}
videoWidth = pInputCodecContext->width ;
videoHeight = pInputCodecContext->height;
pOutputFmt = av_guess_format (NULL, destFile, NULL);
if(NULL == pOutputFmt)
{
printf("分析输出文件失败了—_—\n");
//分析输出文件格式失败
goto lk_error;
}
pOutFormatContext =avformat_alloc_context();
if(NULL == pOutFormatContext)
{
goto lk_error;
}
pOutFormatContext->oformat = pOutputFmt;
pOutStream = av_new_stream(pOutFormatContext, 0);
if(NULL == pOutStream)
{
printf("创建流失败了—_—\n");
//创建流失败
goto lk_error;
}
pOutCodecContext = pOutStream->codec;
pOutCodecContext->codec_id = CODEC_ID_MPEG1VIDEO;
pOutCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
pOutCodecContext->bit_rate = 400000;
pOutCodecContext->width = 352;
pOutCodecContext->height =288;
pOutCodecContext->time_base = pInputCodecContext->time_base;
pOutCodecContext->gop_size = pInputCodecContext->gop_size;
pOutCodecContext->pix_fmt = pInputCodecContext->pix_fmt;
pOutCodecContext->max_b_frames = pInputCodecContext->max_b_frames;
pOutCodecContext->time_base.den = 25;
pOutCodecContext->time_base.num = 1;
pOutStream->r_frame_rate = pInputFormatContext->streams[videoindex]->r_frame_rate;
pOutCodec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
if(NULL == pOutCodec)
{
printf("相应的编码器找不到不是MPEG1—_—\n");
//找不到指定编码器
goto lk_error;
}
if(avcodec_open2(pOutCodecContext, pOutCodec,NULL) < 0)
{
printf("编码器打开出错。不是mpeg1—_—\n");
//打开指定编码器错误
goto lk_error;
}
if (!(pOutFormatContext->flags & AVFMT_NOFILE))
{
if(avio_open(&pOutFormatContext->pb, destFile, AVIO_FLAG_WRITE)<0)//(url_fopen(&pOutFormatContext->pb, destFile, URL_WRONLY)<0)AVIO_FLAG_READ_WRITE
//1表示只读,2表示只写 还有个可读可写
{
printf("无法打开输出文件—_—\n");
//打开输出文件
goto lk_error;
}
}
if(!pOutFormatContext->nb_streams)
{
fprintf(stderr,"输出文件里没有流\n");
exit(0);
}
if(avformat_write_header(pOutFormatContext,NULL) < 0)
{
printf("输出容器写失败了!—_—\n");
//写入输出文件头失败
goto lk_error;
}
av_dump_format(pOutFormatContext, 0, destFile, true);//输出信息
while((wa=av_read_frame(pInputFormatContext, &InPack)) >= 0)
{
OutFrame=avcodec_alloc_frame();
len = avcodec_decode_video2(pInputCodecContext, OutFrame, &nComplete, &InPack);
if(nComplete >0)
{
memset(pEnCodeBuf,0, MAX_BUF_SIZE);
fflush(stdout);
OutFrame->pts = av_rescale(frame_index, AV_TIME_BASE*(int64_t)pOutCodecContext->time_base.num, pOutCodecContext->time_base.den);
。
OutFrame->pict_type = AV_PICTURE_TYPE_NONE;
bianma= avcodec_encode_video(pOutCodecContext, pEnCodeBuf, MAX_BUF_SIZE, OutFrame);
if (bianma >= 0) //编码成功了
{
//ffmpeg写基本输出方式
av_init_packet(&OutPack); //输出包初始化
if(pOutCodecContext->coded_frame && pOutCodecContext->coded_frame->key_frame)
{
OutPack.flags |= AV_PKT_FLAG_KEY;
}
OutPack.flags = InPack.flags;
OutPack.stream_index = InPack.stream_index;
OutPack.data = pEnCodeBuf;
OutPack.size = bianma; // bianma包大小
ret=av_write_frame(pOutFormatContext, &OutPack);
}
frame_index++;
}
av_free_packet(&OutPack);
// av_free(IInPack);
av_free(OutFrame);
}
av_write_trailer(pOutFormatContext);
for(i=0; i<(pOutFormatContext->nb_streams); i++)
{
av_freep(&pOutFormatContext->streams[i]->codec);
av_freep(&pOutFormatContext->streams[i]);
}
lk_error:
delete pEnCodeBuf;
av_free(pOutCodec);
av_free(pOutputFmt);
avcodec_close(pInputCodecContext);
av_free(pInputFormatContext);
av_free(pOutFormatContext);
return 0;
}
上码 求帮助