#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
extern "C"
{
#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
#include "libavcodec/avcodec.h"
}
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "swscale.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")
#undef exit
/****************************************************************************************************************************************/
#define STREAM_DURATION 5.0
#define STREAM_FRAME_RATE 3.0
#define STREAM_NB_FRAMES ((int)(STREAM_DURATION*STREAM_FRAME_RATE))
#define STREAM_PIX_FMT PIX_FMT_YUV420P //????对于YUV420的结构
AVFrame *picture,*tmp_picture; //AVFrame为结构体,具体的定义在line 846 of file avcodec.h
uint8_t *video_outbuf; //关于uint8_t的解释 网址:http://blog.163.com/luokun_9/blog/static/20818832200761033146831/
int frame_count,video_outbuf_size;
main(int argc,char **argv)
{
int a;
const char *input_file_name="d:/aaa.m4v";
av_register_all(); //初始化的作用 注册库中所有可用的文件格式和编码器 Definition at line 597 of file avformat.h
AVFormatContext *ic; //为结构体,在其中定义了编解码所需要的变量 Definition at line 401 of file avformat.h
/**
* Allocate an AVFormatContext.
* Can be freed with av_free() but do not forget to free everything you
* explicitly allocated as well!
*/
ic=av_alloc_format_context(); // Allocate an AVFormatContext. Definition at line 646 of file avformat.h
if(av_open_input_file(&ic,input_file_name,NULL,0,NULL)!=0) //open input file Definition at line 637 of file avformat.h
{
printf("can not open the file %s\n",input_file_name);
exit(1);
}//检验是否成功的打开文件
if(av_find_stream_info(ic)<0) //Read packets of a media file to get stream information. Definition at line 660 of file avformat.h
{
printf("can not find suitable codec parameters\n");
exit(1);
}//检验视频文件中,是否有数据流,并检验是否能成功取出
dump_format(ic,0,input_file_name,0); //列出输入文件的相关流信息 Definition at line 884 of file avformat.h 最后一位:判断是否是输出,若是输出则该位为1
/******************************************************************************************************************************************/
int i;
int videoindex=-1;
int audioindex=-1;
for(i=0;i<ic->nb_streams;i++)
{
if(ic->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) //指针数组stream[i]的类型是结构体AVStream 表示音频和视频的数据流 Definition at line 295 of file avformat.h
//codec的数据类型是结构体AVCodecContext 表示关于编解码的标准位 Definition at line 800 of file avcodec.h
{
videoindex=i;
printf("video\n");
}
else if(ic->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO)
{
audioindex=i;
printf("audio\n");
}
}
/********************************************************************************************************************************************/
if(videoindex==-1)
{
printf("can not find video stream\n");
exit(1);
}//没有找到视频流
AVCodecContext *vCodecCtx; //结构体AVCodecContext 表示关于编解码的标志位 Definition at line 800 of file avcodec.h
vCodecCtx=ic->streams[videoindex]->codec; //相当于符值的过程,将文件的相关参数赋给对应的新定义的变量
AVCodec *vCodec; //关于音/视频编解码的标志位 Definition at line 2213 of file avcodec.h
vCodec=avcodec_find_decoder(vCodecCtx->codec_id);//获取相应的解码器 Definition at line 2524 of file avcodec.h
if (vCodec==NULL)
{
printf("can not find suitable video decoder\n");
exit(1);
}//没有找到合适的解码器
if (avcodec_open(vCodecCtx,vCodec)<0) //Definition at line 2640 of file avcodec.h
{
printf("can not open the video decoder\n");
exit(1);
}//打开对应的音\视频解码器
/*********************************************************************************************************************************************/
/* if (audioindex==-1)
{
printf("can not find audio stream\n");
exit(1);
}
AVCodecContext *aCodecCtx;
aCodecCtx=ic->streams[audioindex]->codec;
AVCodec *aCodec;
aCodec=avcodec_find_decoder(aCodecCtx->codec_id);
if (aCodec==NULL)
{
printf("can not find suitable audio decoder\n");
exit(1);
}
if(avcodec_open(aCodecCtx,aCodec)<0)
{
printf("can not open the audio decoder\n");
exit(1);
}*/
/**********************************************************************************************************************************************/
const char *output_file_name="d:/23.vob";
AVOutputFormat *fmt; //输出文件的整体结构(为结构体类型,其中包括相应的变量和函数) Definition at line 171 of file avformat.h
AVFormatContext *oc;
AVCodecContext *oVcc,*oAcc; //结构体AVCodecContext 表示关于编解码的标志位 Definition at line 800 of file avcodec.h
AVCodec *oVc,*oAc;
AVStream *video_st,*audio_st; //结构体AVStream 表示音频和视频的数据流 Definition at line 295 of file avformat.h
AVFrame *oVFrame,*oAFrame; //AVFrame为结构体,具体的定义在line 846 of file avcodec.h
double video_pts;
oVFrame=avcodec_alloc_frame(); //函数的作用:为oVFrame分配内存空间
fmt=guess_format(NULL,output_file_name,NULL); //Guess the format based on the filename Definition at line 548 of file avformat.h
//具体见网址:http://wiki.aasimon.org/doku.php?id=ffmpeg:guess_format
if (!fmt)
{
printf("could not deduce output format from outfile extension\n");
exit(0);
}//是否可以判断输出文件的编码格式
oc=av_alloc_format_context(); // Allocate an AVFormatContext. Definition at line 646 of file avformat.h
if (!oc)
{
printf("Memory error!\n");
exit(0);
}
oc->oformat=fmt;
video_st=av_new_stream(oc,0); //为数据类型为AVStream的变量分配内存空间 Definition at line 744 of file avformat.h
if (!video_st)
{
printf("could not alloc video stream\n");
exit(0);
}
oVcc=avcodec_alloc_context(); //为数据类型为AVCodecContext的变量分配内存空间
oVcc=video_st->codec;
oVcc->codec_id=CODEC_ID_MPEG2VIDEO ;
oVcc->codec_type=CODEC_TYPE_VIDEO;
oVcc->bit_rate=2500000;
oVcc->width=352;
oVcc->height=288;
oVcc->time_base=vCodecCtx->time_base;
oVcc->gop_size=vCodecCtx->gop_size;
oVcc->pix_fmt=vCodecCtx->pix_fmt;
oVcc->max_b_frames=vCodecCtx->max_b_frames;
video_st->r_frame_rate=ic->streams[videoindex]->r_frame_rate;
/*************************************************************************************************************************************************/
/* audio_st=av_new_stream(oc,oc->nb_streams);
if (!audio_st)
{
printf("could not alloc audio stream\n");
exit(0);
}
avcodec_get_context_defaults2(audio_st->codec,CODEC_TYPE_AUDIO);
oAcc=avcodec_alloc_context();
oAcc=audio_st->codec;
oAcc->codec_id=CODEC_ID_MP3;
oAcc->codec_type=CODEC_TYPE_AUDIO;
oAcc->bit_rate=aCodecCtx->bit_rate;
oAcc->sample_rate=aCodecCtx->sample_rate;
oAcc->channels=2;*/
/**************************************************************************************************************************************************/
if (av_set_parameters(oc,NULL)<0) //媒体文件相关的信息 Definition at line 819 of file avformat.h
{
printf("Invalid output format parameters\n");
exit(0);
}//设置必要的媒体文件信息的输出参数
strcpy(oc->title,ic->title);
strcpy(oc->author,ic->author);
strcpy(oc->copyright,ic->copyright);
strcpy(oc->comment,ic->comment);
strcpy(oc->album,ic->album);
oc->year=ic->year;
oc->track=ic->track;
strcpy(oc->genre,ic->genre);
dump_format(oc,0,output_f
DVD文件(VOB)的生成代码
需积分: 9 140 浏览量
2009-12-10
17:50:28
上传
评论
收藏 3.4MB RAR 举报
huangjian_w
- 粉丝: 2
- 资源: 8
最新资源
- C语言基础-C语言编程基础之Leetcode编程题解之第33题搜索旋转排序数组.zip
- 基于Python实现的手写数字识别系统源码.zip
- 从网页提取禁止转载的文字
- C语言基础-C语言编程基础之Leetcode编程题解之第32题最长有效括号.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第31题下一个排列.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第30题串联所有单词的子串.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第29题两数相除.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第28题找出字符串中第一个匹配项的下标.zip
- 实验报告模板(1).docx
- C语言基础-C语言编程基础之Leetcode编程题解之第26题删除有序数组中的重复项.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈