c#生成AVI自动设置压缩格式,不调用AVISaveOptions
工作中遇到生成AVI视频的项目,代码中会调用AVISaveOptions来设置压缩格式,针对单个文件还好说,但是批量生成视频的时候,每一个都要设置格式,
体验不是很好,经过查询资料问题得到解决
最开始的代码:
AVICOMPRESSOPTIONS opts = new AVICOMPRESSOPTIONS(); opts.fccType = _fccType; opts.fccHandler = 0; opts.dwKeyFrameEvery = 0; opts.dwQuality = 0; opts.dwFlags = 0; opts.dwBytesPerSecond = 0; opts.lpFormat = new IntPtr(0); opts.cbFormat = 0; opts.lpParms = new IntPtr(0); opts.cbParms = 0; opts.dwInterleaveEvery = 0; AVICOMPRESSOPTIONS* p = &opts; AVICOMPRESSOPTIONS** pp = &p; IntPtr x = _ps; IntPtr* ptr_ps = &x; AVISaveOptions(0, 0, 1, ptr_ps, pp); int hr = AVIMakeCompressedStream(out _psCompressed, _ps, ref opts, 0);
修改后的代码
opts.fccType = 0; opts.fccHandler = (int)0x6376736d; opts.dwKeyFrameEvery = 0; opts.dwQuality = 10000; opts.dwFlags = 8; opts.dwBytesPerSecond = 0; opts.lpFormat = new IntPtr(0); opts.cbFormat = 0; opts.lpParms = new IntPtr(0); opts.cbParms = 4; opts.dwInterleaveEvery = 0; AVICOMPRESSOPTIONS* p = &opts; AVICOMPRESSOPTIONS** pp = &p; IntPtr x = _ps; IntPtr* ptr_ps = &x; opts.lpParms = Marshal.AllocHGlobal(sizeof(int)); //AVISaveOptions(0, 0, 1, ptr_ps, pp); int hr = AVIMakeCompressedStream(out _psCompressed, _ps, ref opts, 0);
思路就是,Debug模式下,跟踪设置完压缩格式后
AVICOMPRESSOPTIONS结构体中各元素的值,其中lpParms需要利用
Marshal.AllocHGlobal(sizeof(int))来获取,其余根据监视的值设定即可