|
|
STEP 5 - Creating new AVI files from individual samples/framesHERE ARE THE PROJECT FILES FOR STEP 5 AVITutr5.zip By now, you should have a good idea of what is possible with the AVIFile functions. The final sample will demonstrate how to create an AVI file from a sequence of individual bitmap file. If you don't have a video capture device or commercial graphics software you can still prepare a series of bitmaps with the mspaint.exe program included with windows. Just save several different files the same width, height and color depth and you will have something to work with as you experiment with the code here (AVITutr5.zip). You might even want to use the utility we built in step three to create bitmaps from an existing AVI file you have. Then you could mix up the frames any way you want and write a new AVI file with this sample. The only new things in this sample's code are that I create a stream from "scratch" (instead of just copying and existing stream), and use a different method of saving the stream to an AVI file (I don't use AVISave in this sample). The rest of the sample should be fairly easy to understand if you have gone through the other steps before this one. A stream can be created by initializing the AVI_STREAM_INFO UDT and passing it into the AVIFileCreateStream function. When this function returns, the second parameter will contain a valid pointer to a newly allocated stream interface pointer: ' Fill in the header for the video stream Now that the ps variable points to a stream, we can use that to create a compressed stream which contains all the information neccesary to write a file with a codec-compressed video stream: First we let the user decide which codec and settings to use: 'get the compression options from the user Then we pass this information, along with the stream to the AVIMakeCompressedStream function: 'make compressed stream Once this function returns sucessfully, we know that psCompressed points to a valid compressed stream interface which can write out a compressed video stream in the format we specified with the AVI_STREAM_INFO and AVI_COMPRESS_OPTIONS UDTs. All we have to do then is to set the format of the DIBs that the stream will expect us to pass in. This requires a BITMAPINFO UDT which is a variable sized UDT and difficult to use from VB. Fortunately the cDIB class will help us again here. The cDIB class has a function which receives a bmp file from the disk. Earlier in the sub I called this function and loaded the first bitmap from the list in order to get the width, height and size of the data buffer for the AVI_STREAM_INFO UDT: 'Get the first bmp in the list for setting format Now we can use the same UDT to set the format of the video stream. First we will store all the necessary information in a BITMAPINFOHEADER UDT: 'set format of stream according to the bitmap And then pass the UDT along with the stream to the AVIStreamSetFormat function: 'set the format of the compressed stream If this function returns successfully then the compressed stream is fully initialized and ready to be passed some DIBs for writing. You do this by passing it and the stream, along with the position you what it written, to the AVIStreamWrite function. Notice that the cDIB class comes in handy here again because it has a method which returns a direct pointer to the exact bits that you need to pass in to the function: ' Now write out each video frame Once this loop is finished writing the bitmaps, all you have to do is clean up afterwards and release the stream and file handles. The file you created at the beginning of the sub will then contain a valid video stream and you can play it back in the Windows Media Player or other AVI player. Please see the code in the sample for all the cleanup details. When you run the sample, you will have to select the individual bitmaps one at a time then type in a frame rate between 1fps and 30fps before writing the new file. Have fun! Go to the contact information page Go back to the Table of Contents
|