用编程改变大众。。。
分类: C/C++
2015-08-17 19:17:30
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
#else
typedef long DWORD;
#endif
#include "avilib.h"
/*
avi转avi
by notsobad
使用avilib进行avi文件的读写
这个例子是用来将一个avi写入另一个avi
just a simple test
*/
int main(int argc, const char **argv)
{
char * vidbuf = malloc(327680);
char * audbuf = malloc(100000);
long framesize;
avi_t *avifile;
avi_t* pAviHandle;
int frame;
int frames;
int framew = 0;
int frameh = 0;
double framerate = 0.0f;
pAviHandle = AVI_open_output_file("test.avi");
if(!pAviHandle)
{
printf("avi file open failed /n");
return -1;
}
avifile = AVI_open_input_file("../vp31.avi", 1); //打开源文件
frames = AVI_video_frames(avifile);
framew = AVI_video_width(avifile);
frameh = AVI_video_height(avifile);
framerate = AVI_frame_rate(avifile);
AVI_set_video(pAviHandle,framew,frameh,framerate,AVI_video_compressor(avifile));//设置video文件头
AVI_set_audio(pAviHandle, AVI_audio_channels(avifile),
AVI_audio_rate(avifile),
AVI_audio_bits(avifile),
AVI_audio_format(avifile),
AVI_audio_mp3rate(avifile));
//设置audio文件头
//framesize=(long)framew*frameh*3/2;
if (AVI_seek_start( avifile )) //寻找文件开始
{
printf("bad seek start/n");
return -2;
}
for (frame = 0; frame < frames;frame++)//read and write avi per fream
{
int iskeyframe;//if it's keyframe
AVI_set_video_position(avifile, frame);//将avi文件移动到第fream帧
long audio_bytes=AVI_audio_size(avifile,frame);//get audo size
AVI_read_frame(avifile,(char *)vidbuf,&iskeyframe);//read fream set video into vidbuf
AVI_read_audio(avifile, (char *)audbuf,audio_bytes);//set audio into audbuf
/*
long AVI_audio_size(avi_t *AVI, long frame)
int AVI_write_audio(avi_t *AVI, char *data, long bytes)
long AVI_read_audio(avi_t *AVI, char *audbuf, long bytes)
*/
AVI_write_frame(pAviHandle,vidbuf,AVI_frame_size(avifile, frame),iskeyframe);//write video fream by fream
AVI_write_audio(pAviHandle,audbuf,audio_bytes);//write audio fream by fream
}
AVI_close(pAviHandle);
AVI_close(avifile);
return 1;
}