将图像文件制作成AVI视频

本文介绍了一种使用AVIFileFunctions库将指定文件夹内的图片批量转换为AVI格式视频的方法。通过设置视频分辨率、帧率等参数,可以实现高质量视频的输出。

利用AVIFile Functions将文件夹中的图片制作成AVI视频。


  
// ======================================================================================
//
// 功能 : 将目录中的图像件制用成AVI视频
// 参数 : videoWidth、videoHeight代表输出的视频的宽度和高度
// pAviName 代表输出的视频的文件名
// pDir代表存放图像文件的路径. 此文件夹下的图像文件的尺寸需要一致
// bpp 代表图像中每像素位数
// 作者 : perit
// ======================================================================================
void ImageToAVI( LPCTSTR pAviName, LPCTSTR pDir, int videoWidth, int videoHeight, int bpp )
{
if ( NULL == pAviName || NULL == pDir )
return ;

// 初始化AVI 库
AVIFileInit();

//
// Create AVI file
//
PAVIFILE pAviFile = NULL;
HRESULT hr
= AVIFileOpen( & pAviFile, pAviName, OF_WRITE | OF_CREATE, NULL );
if ( 0 != hr )
{
ASSERT( FALSE );
return ;
}

//
// Create AVI video stream
//
AVISTREAMINFO strhdr;
memset(
& strhdr, 0 , sizeof ( strhdr ) );

strhdr.fccType
= streamtypeVIDEO;
strhdr.fccHandler
= 0 ;
strhdr.dwScale
= 1 ;
strhdr.dwRate
= 25 ;

// Calc image size
UINT pitch = ( videoWidth * bpp + 31 ) / 32 * 4 ;
UINT biSizeImage
= ( videoWidth * bpp + 31 ) / 32 * 4 * videoHeight;
strhdr.dwSuggestedBufferSize
= biSizeImage;

SetRect(
& strhdr.rcFrame, 0 , 0 , videoWidth, videoHeight );

// And create the stream;
PAVISTREAM pAviStream = NULL;
hr
= AVIFileCreateStream( pAviFile, & pAviStream, & strhdr );
if ( 0 != hr )
{
ASSERT( FALSE );

AVIFileRelease( pAviFile );
pAviFile
= NULL;

return ;
}

//
// Set stream format
//
BITMAPINFOHEADER bih;
memset(
& bih, 0 , sizeof ( BITMAPINFOHEADER ) );

bih.biBitCount
= bpp;
bih.biClrImportant
= 0 ;
bih.biClrUsed
= 0 ;
bih.biCompression
= BI_RGB;
bih.biPlanes
= 1 ;
bih.biSize
= 40 ;
bih.biXPelsPerMeter
= 0 ;
bih.biYPelsPerMeter
= 0 ;
bih.biWidth
= videoWidth;
bih.biHeight
= videoHeight;
bih.biSizeImage
= biSizeImage;

hr
= AVIStreamSetFormat( pAviStream, 0 , & bih, sizeof ( bih ) );
if ( 0 != hr )
{
ASSERT( FALSE );

AVIStreamClose( pAviStream );
pAviStream
= NULL;

AVIFileRelease( pAviFile );
pAviFile
= NULL;
}

//
// 遍历每一个图像,制作成视频中的一帧帧图像
//
TCHAR imageDir[ MAX_PATH ];
memset( imageDir,
0 , sizeof ( imageDir ) );
sprintf( imageDir,
" %s\\*.* " , pDir );

CFileFind finder;
BOOL bFind
= finder.FindFile( imageDir );
int nFrames = 0 ;

// 图像数据缓冲区
BYTE * pData = new BYTE[ biSizeImage ];
if ( pData )
{
memset( pData,
0 , biSizeImage );
}

while ( bFind )
{
bFind
= finder.FindNextFile();

if ( ! finder.IsDots() && ! finder.IsDirectory() )
{
// 获取图像文件绝对路径
CString str = finder.GetFilePath();

//
// Open Image and get image info & data
//
CImage img;
if ( FAILED( img.Load( str ) ) )
{
TRACE(
" Warning : fail to load file %s!!!\n " , str );
continue ;
}

//
// 读取图像数据,更新AVI视频帧
//
if ( pData )
{
int w = min( videoWidth, img.GetWidth() );
int h = min( videoHeight, img.GetHeight() );

for ( int i = 0 ; i < h; i ++ )
for ( int j = 0 ; j < w; j ++ )
{
COLORREF clr
= img.GetPixel( j, i );

pData[ i
* pitch + j * ( bpp / 8 ) + 0 ] = GetBValue( clr );
pData[ i
* pitch + j * ( bpp / 8 ) + 1 ] = GetGValue( clr );
pData[ i
* pitch + j * ( bpp / 8 ) + 2 ] = GetRValue( clr );
}

hr
= AVIStreamWrite( pAviStream, nFrames, 1 , pData, biSizeImage, AVIIF_KEYFRAME, NULL, NULL );
ASSERT(
0 == hr );
}

nFrames
++ ;
}
}

if ( pData )
{
delete [] pData;
pData
= NULL;
}

if ( pAviStream )
{
AVIStreamClose( pAviStream );
pAviStream
= NULL;
}

if ( pAviFile )
{
AVIFileRelease( pAviFile );
pAviFile
= NULL;
}

AVIFileExit();
}

例子程序下载

http://files.cnblogs.com/Perit/tempAVI.rar

转载于:https://www.cnblogs.com/Perit/archive/2011/05/26/2058881.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值