📄 writeavi.cpp
字号:
这是VC的例子WriteAVI的代码,好好读读吧
/**************************************************************************
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
* PURPOSE.
*
* Copyright (C) 1992 - 1997 Microsoft Corporation. All Rights Reserved.
*
**************************************************************************/
/****************************************************************************
*
* WRITEAVI.C
*
* Creates the file OUTPUT.AVI, an AVI file consisting of a rotating clock
* face. This program demonstrates using the functions in AVIFILE.DLL
* to make writing AVI files simple.
*
* This is a stripped-down example; a real application would have a user
* interface and check for errors.
*
***************************************************************************/
#define STRICT
#define INC_OLE2
#include <windows.h>
#include <windowsx.h>
#include <memory.h>
#include <mmsystem.h>
#include <vfw.h>
#include "writeavi.h"
//---------------------------------------------------------------------------
// Defines
//---------------------------------------------------------------------------
// Our movie is 160x120 and 15 frames long
//
#define BITMAP_X 160
#define BITMAP_Y 120
#define N_FRAMES 15
#define TEXT_HEIGHT 20
#define AVIIF_KEYFRAME 0x00000010L // this frame is a key frame.
#define BUFSIZE 260
#define LPLPBI LPBITMAPINFOHEADER *
//---------------------------------------------------------------------------
// Function declarations
//---------------------------------------------------------------------------
static void FreeFrames(LPLPBI) ;
static void MakeFrames(LPLPBI, UINT, UINT, UINT) ;
static HANDLE MakeDib(HBITMAP, UINT);
//---------------------------------------------------------------------------
// A quick lookup table for Sin and Cos values
//---------------------------------------------------------------------------
//
static int aSin[N_FRAMES] = {
0, 40, 74, 95, 99,
86, 58, 20, -20, -58,
-86, -99, -95, -74, -40,
} ;
static int aCos[N_FRAMES] = {
100, 91, 66, 30, -10,
-49, -80, -97, -97, -80,
-50, -10, 30, 66, 91,
} ;
//----------------------------------------------------------------------------
//
// We don't have a window, we just pop up a dialog
// box, write the file, and quit
//
int PASCAL WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR szCmdLine,
int sw)
{
LPBITMAPINFOHEADER alpbi[N_FRAMES];
int i;
AVISTREAMINFO strhdr;
PAVIFILE pfile = NULL;
PAVISTREAM ps = NULL, psCompressed = NULL, psText = NULL;
char szText[BUFSIZE];
int iLen;
AVICOMPRESSOPTIONS opts;
AVICOMPRESSOPTIONS FAR * aopts[1] = {&opts};
HRESULT hr;
DWORD dwTextFormat;
WORD wVer;
char szTitle[BUFSIZE];
char szMessage[BUFSIZE];
/* first let's make sure we are running on 1.1 */
wVer = HIWORD(VideoForWindowsVersion());
if (wVer < 0x010a){
/* oops, we are too old, blow out of here */
LoadString(hInstance, IDS_APPERR, szTitle, BUFSIZE );
LoadString(hInstance, IDS_VFWTOOOLD, szMessage, BUFSIZE );
MessageBeep(MB_ICONHAND);
MessageBox(NULL, szMessage, szTitle, MB_OK|MB_ICONSTOP);
return FALSE;
}
alpbi[0] = NULL;
LoadString(hInstance, IDS_APPNAME, szTitle, BUFSIZE );
LoadString(hInstance, IDS_INTRO, szMessage, BUFSIZE );
if (MessageBox(NULL, szMessage, szTitle, MB_OKCANCEL) == IDCANCEL)
return 0;
//
// Set up the bitmaps for the file in an array
//
MakeFrames(alpbi, 8, BITMAP_X, BITMAP_Y);
AVIFileInit();
//
// Open the movie file for writing....
//
LoadString(hInstance, IDS_FILENAME, szTitle, BUFSIZE );
hr = AVIFileOpen(&pfile, // returned file pointer
szTitle, // file name
OF_WRITE | OF_CREATE, // mode to open file with
NULL); // use handler determined
// from file extension....
if (hr != AVIERR_OK)
goto error;
// Fill in the header for the video stream....
// The video stream will run in 15ths of a second....
_fmemset(&strhdr, 0, sizeof(strhdr));
strhdr.fccType = streamtypeVIDEO;// stream type
strhdr.fccHandler = 0;
strhdr.dwScale = 1;
strhdr.dwRate = 15; // 15 fps
strhdr.dwSuggestedBufferSize = alpbi[0]->biSizeImage;
SetRect(&strhdr.rcFrame, 0, 0, // rectangle for stream
(int) alpbi[0]->biWidth,
(int) alpbi[0]->biHeight);
// And create the stream;
hr = AVIFileCreateStream(pfile, // file pointer
&ps, // returned stream pointer
&strhdr); // stream header
if (hr != AVIERR_OK) {
goto error;
}
_fmemset(&opts, 0, sizeof(opts));
if (!AVISaveOptions(NULL, 0, 1, &ps, (LPAVICOMPRESSOPTIONS FAR *) &aopts))
goto error;
hr = AVIMakeCompressedStream(&psCompressed, ps, &opts, NULL);
if (hr != AVIERR_OK) {
goto error;
}
hr = AVIStreamSetFormat(psCompressed, 0,
alpbi[0], // stream format
alpbi[0]->biSize + // format size
alpbi[0]->biClrUsed * sizeof(RGBQUAD));
if (hr != AVIERR_OK) {
goto error;
}
// Fill in the stream header for the text stream....
// The text stream is in 60ths of a second....
_fmemset(&strhdr, 0, sizeof(strhdr));
strhdr.fccType = streamtypeTEXT;
strhdr.fccHandler = mmioFOURCC('D', 'R', 'A', 'W');
strhdr.dwScale = 1;
strhdr.dwRate = 60;
strhdr.dwSuggestedBufferSize = sizeof(szText);
SetRect(&strhdr.rcFrame, 0, (int) alpbi[0]->biHeight,
(int) alpbi[0]->biWidth, (int) alpbi[0]->biHeight + TEXT_HEIGHT);
// ....and create the stream.
hr = AVIFileCreateStream(pfile, &psText, &strhdr);
if (hr != AVIERR_OK) {
goto error;
}
dwTextFormat = sizeof(dwTextFormat);
hr = AVIStreamSetFormat(psText, 0, &dwTextFormat, sizeof(dwTextFormat));
if (hr != AVIERR_OK) {
goto error;
}
//
// Now write out each video frame, along with a text label.
// The video frames are 2/3 of a second apart, which is 10
// in the video time scale and 40 in the text stream's time scale.
//
for (i = 0; i < N_FRAMES; i++) {
hr = AVIStreamWrite(psCompressed, // stream pointer
i * 10, // time of this frame
1, // number to write
(LPBYTE) alpbi[i] + // pointer to data
alpbi[i]->biSize +
alpbi[i]->biClrUsed * sizeof(RGBQUAD),
alpbi[i]->biSizeImage, // size of this frame
AVIIF_KEYFRAME, // flags....
NULL,
NULL);
if (hr != AVIERR_OK)
break;
// Make some text to put in the file ...
LoadString(hInstance, IDS_TEXTFORMAT, szMessage, BUFSIZE );
iLen = wsprintf(szText, szMessage, (int)(i + 1));
// ... and write it as well.
hr = AVIStreamWrite(psText,
i * 40,
1,
szText,
iLen + 1,
AVIIF_KEYFRAME,
NULL,
NULL);
if (hr != AVIERR_OK)
break;
}
error:
//
// Now close the file
//
if (ps)
AVIStreamClose(ps);
if (psCompressed)
AVIStreamClose(psCompressed);
if (psText)
AVIStreamClose(psText);
if (pfile)
AVIFileClose(pfile);
AVIFileExit();
FreeFrames(alpbi);
if (hr != NOERROR) {
LoadString(hInstance, IDS_APPERR, szTitle, BUFSIZE );
LoadString(hInstance, IDS_WRITEERR, szMessage, BUFSIZE );
MessageBox(NULL, szMessage, szTitle, MB_OK);
}
return 0;
}
//
// Fill an array of LPBI's with the frames for this movie
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -