/// <summary>Add an existing wave audio stream to the file</summary> /// <param name="waveData">The new stream's data</param> /// <param name="streamInfo">Header info for the new stream</param> /// <param name="streamFormat">The new stream' format info</param> /// <param name="streamLength">Length of the new stream</param> public void AddAudioStream(IntPtr waveData, Avi.AVISTREAMINFO streamInfo, Avi.PCMWAVEFORMAT streamFormat, int streamLength) { IntPtr aviStream; int result = Avi.AVIFileCreateStream(aviFile, out aviStream, ref streamInfo); if (result != 0) { throw new Exception("Exception in AVIFileCreateStream: " + result.ToString()); } result = Avi.AVIStreamSetFormat(aviStream, 0, ref streamFormat, Marshal.SizeOf(streamFormat)); if (result != 0) { throw new Exception("Exception in AVIStreamSetFormat: " + result.ToString()); } result = Avi.AVIStreamWrite(aviStream, 0, streamLength, waveData, streamLength, Avi.AVIIF_KEYFRAME, 0, 0); if (result != 0) { throw new Exception("Exception in AVIStreamWrite: " + result.ToString()); } result = Avi.AVIStreamRelease(aviStream); if (result != 0) { throw new Exception("Exception in AVIStreamRelease: " + result.ToString()); } }
/// <summary>Create a new stream</summary> private void CreateStreamWithoutFormat() { int scale = 1; double rate = FrameRate; GetRateAndScale(ref rate, ref scale); Avi.AVISTREAMINFO strhdr = new Avi.AVISTREAMINFO { fccType = Avi.mmioStringToFOURCC("vids", 0), fccHandler = Avi.mmioStringToFOURCC("CVID", 0), dwFlags = 0, dwCaps = 0, wPriority = 0, wLanguage = 0, dwScale = scale, dwRate = (int)rate, dwStart = 0, dwLength = 0, dwInitialFrames = 0, dwSuggestedBufferSize = FrameSize, dwQuality = -1, dwSampleSize = 0, rcFrame = { top = 0, left = 0, bottom = (uint)Height, right = (uint)Width }, dwEditCount = 0, dwFormatChangeCount = 0, szName = new UInt16[64] }; int result = Avi.AVIFileCreateStream(aviFile, out aviStream, ref strhdr); if (result != 0) { throw new Exception("Exception in AVIFileCreateStream: " + result); } }
/// <summary>Add an existing wave audio stream to the file</summary> /// <param name="newStream">The stream to add</param> /// <param name="startAtFrameIndex"> /// The index of the video frame at which the sound is going to start. /// '0' inserts the sound at the beginning of the video. /// </param> public void AddAudioStream(AudioStream newStream, int startAtFrameIndex) { Avi.AVISTREAMINFO streamInfo = new Avi.AVISTREAMINFO(); Avi.PCMWAVEFORMAT streamFormat = new Avi.PCMWAVEFORMAT(); int streamLength = 0; IntPtr rawData = newStream.GetStreamData(ref streamInfo, ref streamFormat, ref streamLength); IntPtr waveData = rawData; if (startAtFrameIndex > 0) { //not supported //streamInfo.dwStart = startAtFrameIndex; double framesPerSecond = GetVideoStream().FrameRate; double samplesPerSecond = newStream.CountSamplesPerSecond; double startAtSecond = startAtFrameIndex / framesPerSecond; int startAtSample = (int)(samplesPerSecond * startAtSecond); waveData = InsertSilence(startAtSample - 1, waveData, streamLength, ref streamInfo); } IntPtr aviStream; int result = Avi.AVIFileCreateStream(aviFile, out aviStream, ref streamInfo); if (result != 0) { throw new Exception("Exception in AVIFileCreateStream: " + result.ToString()); } result = Avi.AVIStreamSetFormat(aviStream, 0, ref streamFormat, Marshal.SizeOf(streamFormat)); if (result != 0) { throw new Exception("Exception in AVIStreamSetFormat: " + result.ToString()); } result = Avi.AVIStreamWrite(aviStream, 0, streamLength, waveData, streamLength, Avi.AVIIF_KEYFRAME, 0, 0); if (result != 0) { throw new Exception("Exception in AVIStreamWrite: " + result.ToString()); } result = Avi.AVIStreamRelease(aviStream); if (result != 0) { throw new Exception("Exception in AVIStreamRelease: " + result.ToString()); } Marshal.FreeHGlobal(waveData); }