private void SetupVideo(DisplayFormat videoFormat, VideoCompressor compressor, int fps) { int colorDepth = Bitmap.GetPixelFormatSize(videoFormat.PixelFormat); int width = videoFormat.Width; int height = videoFormat.Height; // Calculate pitch int bytesPerPixel = colorDepth / 8; int pitch = width * bytesPerPixel; int pitch_factor = 4; if (pitch % pitch_factor != 0) { pitch = pitch + pitch_factor - pitch % pitch_factor; } // Create AVI Stream Avi32Interop.AVISTREAMINFO asf = new Avi32Interop.AVISTREAMINFO(); asf.dwRate = fps; asf.dwSuggestedBufferSize = pitch * height * bytesPerPixel; asf.dwScale = 1; asf.fccType = Avi32Interop.streamtypeVIDEO; asf.szName = null; asf.rcFrame = new Avi32Interop.RECT(0, 0, width, height); int hr = Avi32Interop.AVIFileCreateStream(this.pAviFile, out this.pVideoStream, ref asf); if (hr != 0) { throw new AviException("AVIFileCreateStream", hr); } // Set stream format Avi32Interop.BITMAPINFOHEADER bih = new Avi32Interop.BITMAPINFOHEADER(); bih.biBitCount = (ushort)colorDepth; bih.biCompression = 0; // BI_RGB bih.biHeight = videoFormat.Height; bih.biPlanes = 1; bih.biSize = (uint)Marshal.SizeOf(bih); bih.biSizeImage = (uint)(pitch * height * (colorDepth / 8)); bih.biWidth = videoFormat.Width; if (compressor != null && !compressor.Equals(VideoCompressor.None)) { // Setup compressor this.SetupVideoCompressor(compressor); hr = Avi32Interop.AVIStreamSetFormat(this.pAviCompressedStream, 0, ref bih, Marshal.SizeOf(bih)); } else { hr = Avi32Interop.AVIStreamSetFormat(this.pVideoStream, 0, ref bih, Marshal.SizeOf(bih)); } if (hr != 0) { throw new AviException("AVIStreamSetFormat", hr); } }
private void SetupAudio(SoundFormat audioFormat, AcmEncoder audioEncoder) { IntPtr pwfx = audioFormat.ToPtr(); try { Avi32Interop.AVISTREAMINFO asi = new Avi32Interop.AVISTREAMINFO(); asi.fccType = Avi32Interop.streamtypeAUDIO; asi.dwScale = audioFormat.BlockAlign; asi.dwRate = audioFormat.AverageBytesPerSecond; asi.dwStart = 0; asi.dwLength = -1; asi.dwInitialFrames = 0; asi.dwSuggestedBufferSize = 0; asi.dwQuality = -1; asi.dwSampleSize = audioFormat.BlockAlign; int hr = Avi32Interop.AVIFileCreateStream(this.pAviFile, out this.pAudioStream, ref asi); if (hr != 0) { throw new AviException("AVIStreamSetFormat", hr); } hr = Avi32Interop.AVIStreamSetFormat(this.pAudioStream, 0, pwfx, audioFormat.ToalSize); if (hr != 0) { throw new AviException("AVIStreamSetFormat", hr); } if (audioEncoder != null) { audioEncoder.Open(); } this.audioFormat = audioFormat; this.audioEncoder = audioEncoder; } finally { Marshal.FreeHGlobal(pwfx); } }