#include
#include
#ifdef _WIN32
#include
#else
typedef long DWORD;
#endif
#include "avilib.h"
/*
avi拆分
by notsobad
使用avilib进行avi文件的读写
avi文件的拆分
10秒一个文件
just a simple test
*/
#define TIME_PER_FILE 30 //10s per file
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;
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);
if (AVI_seek_start( avifile )) //寻找文件开始
{
printf("bad seek start\n");
return -2;
}
int frame_per_file=(int)(framerate*TIME_PER_FILE);
int i=0;
int file_list[30];
int start_name=10000;
char file_name[50];
int iskeyframe;//if it's keyframe
for (frame = 0; frame < frames;frame++)//read and write avi per fream
{
if((frame%frame_per_file)==0)
{
if(frame!=0) AVI_close(pAviHandle); //free the avi
//printf("frame_per_file is %d\n",frame_per_file);
file_list[i]=start_name+TIME_PER_FILE*i;
sprintf(file_name,"%d.avi",file_list[i]);//get the file name
//printf("file %d from frame %d name is %s\n",i,frame,file_name);
i++;
pAviHandle = AVI_open_output_file(file_name); /* */
if(!pAviHandle)
{
printf("avi file open failed \n");
return -1;
}
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文件头
}//end if
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
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
} //end for
free(vidbuf);
free(audbuf);
AVI_close(avifile);
return 1;
}
阅读(4384) | 评论(2) | 转发(0) |