Click here to Skip to main content
15,897,718 members
Articles / Desktop Programming / MFC
Article

Copy a period of AVI chunks

Rate me:
Please Sign up or sign in to vote.
3.89/5 (5 votes)
22 May 20061 min read 42.4K   757   36   6
How to copy a period of AVI video by copying the chunk data.

Introduction

Generally, we have two mothods to copy a period of AVI video.

  • Use AVIStreamGetFrame and AVIStreamWriteData. The disadvantage of this method is it will cost a long time and generate a very big output file (uncompressed), especially when copying a long period.
  • Use EditStreamCopy and EditStreamPaste. This can give the user a chance to select an encoder, and use this encoder to compress the output file. Still, the output file is always more than 10 times the same period of the input file, and also this will cost a very long time.

How to Copy AVI Chunks

Basicly, the format of AVI files should like this:

RIFF                   RIFF HEADER
|-AVI                  AVI CHUNK  
  |-hdrl               MAIN AVI HEADER
  | |- ......          OTHER HEADER INFORMATION
  |-movi               MOVIE DATA
  | |-chunk1           chunk data
  | |- ......          chunk data
  | |-chunkn           chunk data
  |-idx1               AVI INDEX
    |-[index data]     DATA

So, we can copy the chunks of certain periods to the output file by the following steps:

  • Copy header.
  • Copy chunks.
  • Reconstruct index data.
  • Correct some data of the header, such as the file length and the data length.

Advantages and Disadvantages

This methed can run very fast, and the size of the output files corresponds to the input file. Still the disadvantages are obvious too. This class depends on the codec and may not work for some kinds of AVI files.

How to Use

The only code required to use this class is:
CCopyAVIChunk copier;
copier.CopyFile("C:\\tests\\test1.avi", "C:\\tests\\test2.avi", 123, 765);

Comments

I use this class for certain projects, so I only care if it can work with "those" kind of AVI files. Still I have no chance to test for general AVI files. If you are interested in this class and finds it can't work, please feel free to contact me at yinglong_w@hotmail.com.

Version History

  • Version 1.0 - posted May 23, 2006 to CodeProject.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questioncombining avi video files Pin
sukumarvenkata18-May-08 19:09
sukumarvenkata18-May-08 19:09 
as per my requirement i have to combine multiple avi video files to make a
single avi video file...i did it in following way....i had taken two avi
video files..nam1.avi and name2.avi...i got the individual avistreams from
both these files by using
AVIFileOpen and AVIFileGetStream functions....n after i followed AVISaveV
which builds a sigle file from indidvidual streams....and if i combine
nam1.avi(2sec play)
name2.avi( 2sec play) the target combined file has to be play for 4sec
duration...but it is playing for 2 sec only...n regarding size it's ok.. the
target file size is sum of two i/p file..i got this...can any one help me to
fix this problem...
and i paste my code below..


cout<<"my code starts"<<endl;


TCHAR szFile[MAX_PATH];

AVIFileInit();

cout<<"avi file initialized succesfully"<<endl;

PAVIFILE hFile,hFile2;

strcpy(szFile,"D:\\Avi\\patient1\\name2.avi");

cout<<szFile<<endl;

if(AVIFileOpen(&hFile,szFile ,OF_READ, NULL)==AVIERR_OK )

cout<<"avi file opened successfully"<<endl;
else
cout<<"not opened"<<endl;
// TODO: code your application's behavior here.
/*CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;*/

AVIFILEINFO obj;

if(AVIFileInfo(hFile,&obj,sizeof(AVIFILEINFO))==AVIERR_OK )

cout<<"avi file inf is received it contains"<< obj.dwStreams
<<"stream"<<endl;

PAVISTREAM ppavi,ppavi2;

if(AVIFileGetStream(hFile,&ppavi,streamtypeVIDEO,0)==AVIERR_OK )

cout<<"got stream interface "<<endl;

cout<<"_____________________________________________"<<endl;

if(AVIFileOpen(&hFile2,"D:\\Avi\\patient1\\name1.avi" ,OF_READ,
NULL)==AVIERR_OK )

cout<<"avi file 2 opened successfully"<<endl;

if(AVIFileGetStream(hFile2,&ppavi2,streamtypeVIDEO,0)==AVIERR_OK )

cout<<"got stream2 interface "<<endl;

PAVISTREAM pStreams[2]= { NULL, NULL };

cout<<"_____________________________________________"<<endl;

pStreams[0]=ppavi;

pStreams[1]=ppavi2;

PAVIFILE hFile3;


if(AVIMakeFileFromStreams(&hFile3,2,pStreams)==AVIERR_OK)
cout<<"avis are combined"<<endl;

//AVICOMPRESSOPTIONS gaAVIOptions[2];

if(AVISaveV("D:\\Avi\\target.avi",NULL,NULL,2,pStreams, NULL)==AVIERR_OK)

cout<<"avi combine has done"<<endl;

sukumar

Questionhow to create parser filters. Pin
amiya das27-Sep-07 0:15
amiya das27-Sep-07 0:15 
GeneralNo souce code for the main function Pin
Super Garrison11-Sep-07 12:02
Super Garrison11-Sep-07 12:02 
GeneralCant compile in Visual Studio 2005 Pin
Zysen_8-Apr-07 18:11
Zysen_8-Apr-07 18:11 
GeneralHi Pin
naraimha27-Dec-06 18:30
naraimha27-Dec-06 18:30 
GeneralDemuxing MP3 Pin
Jacquers10-Jul-06 8:10
Jacquers10-Jul-06 8:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.