Exemplo n.º 1
0
        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());
            }
        }
Exemplo n.º 2
0
        public AudioStream(int aviFile, IntPtr aviStream)
        {
            this.aviFile   = aviFile;
            this.aviStream = aviStream;

            int size = Marshal.SizeOf(waveFormat);

            Avi.AVIStreamReadFormat(aviStream, 0, ref waveFormat, ref size);
            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(aviStream);
        }
Exemplo n.º 3
0
        public AviManager CopyTo(String newFileName, float startAtSecond, float stopAtSecond)
        {
            AviManager newFile = new AviManager(newFileName, false);

            try {
                VideoStream videoStream = GetVideoStream();

                int startFrameIndex = (int)(videoStream.FrameRate * startAtSecond);
                int stopFrameIndex  = (int)(videoStream.FrameRate * stopAtSecond);

                videoStream.GetFrameOpen();
                Bitmap      bmp       = videoStream.GetBitmap(startFrameIndex);
                VideoStream newStream = newFile.AddVideoStream(false, videoStream.FrameRate, bmp);
                for (int n = startFrameIndex + 1; n <= stopFrameIndex; n++)
                {
                    bmp = videoStream.GetBitmap(n);
                    newStream.AddFrame(bmp);
                }
                videoStream.GetFrameClose();


                AudioStream waveStream = GetWaveStream();

                Avi.AVISTREAMINFO streamInfo   = new Avi.AVISTREAMINFO();
                Avi.PCMWAVEFORMAT streamFormat = new Avi.PCMWAVEFORMAT();
                int    streamLength            = 0;
                IntPtr ptrRawData = waveStream.GetStreamData(
                    ref streamInfo,
                    ref streamFormat,
                    ref streamLength);

                int startByteIndex = (int)(startAtSecond * (float)(waveStream.CountSamplesPerSecond * streamFormat.nChannels * waveStream.CountBitsPerSample) / 8);
                int stopByteIndex  = (int)(stopAtSecond * (float)(waveStream.CountSamplesPerSecond * streamFormat.nChannels * waveStream.CountBitsPerSample) / 8);

                IntPtr ptrWavePart = new IntPtr(ptrRawData.ToInt32() + startByteIndex);

                byte[] rawData = new byte[stopByteIndex - startByteIndex];
                Marshal.Copy(ptrWavePart, rawData, 0, rawData.Length);
                Marshal.FreeHGlobal(ptrRawData);

                streamInfo.dwLength = rawData.Length;
                streamInfo.dwStart  = 0;

                IntPtr unmanagedRawData = Marshal.AllocHGlobal(rawData.Length);
                Marshal.Copy(rawData, 0, unmanagedRawData, rawData.Length);
                newFile.AddAudioStream(unmanagedRawData, streamInfo, streamFormat, rawData.Length);
                Marshal.FreeHGlobal(unmanagedRawData);
            } catch (Exception ex) {
                newFile.Close();
                throw ex;
            }

            return(newFile);
        }
Exemplo n.º 4
0
        private Avi.AVISTREAMINFO GetStreamInfo(IntPtr aviStream)
        {
            Avi.AVISTREAMINFO streamInfo = new Avi.AVISTREAMINFO();
            int result = Avi.AVIStreamInfo(StreamPointer, ref streamInfo, Marshal.SizeOf(streamInfo));

            if (result != 0)
            {
                throw new Exception("Exception in VideoStreamInfo: " + result.ToString());
            }
            return(streamInfo);
        }
Exemplo n.º 5
0
        public void SetInfo(Avi.AVISTREAMINFO info)
        {
            int result = Avi.EditStreamSetInfo(editableStream, ref info, Marshal.SizeOf(info));

            if (result != 0)
            {
                throw new Exception("Exception in SetInfo: " + result.ToString());
            }

            frameRate = info.dwRate / info.dwScale;
        }
Exemplo n.º 6
0
        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)
            {
                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);
        }
Exemplo n.º 7
0
        public IntPtr GetStreamData(ref Avi.AVISTREAMINFO streamInfo, ref Avi.PCMWAVEFORMAT format, ref int streamLength)
        {
            streamInfo = GetStreamInfo();

            format       = GetFormat();
            streamLength = Avi.AVIStreamLength(aviStream.ToInt32()) * streamInfo.dwSampleSize;
            IntPtr waveData = Marshal.AllocHGlobal(streamLength);

            int result = Avi.AVIStreamRead(aviStream, 0, streamLength, waveData, streamLength, 0, 0);

            if (result != 0)
            {
                throw new Exception("Exception in AVIStreamRead: " + result.ToString());
            }

            return(waveData);
        }
Exemplo n.º 8
0
        public void GetFrameOpen()
        {
            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(StreamPointer);

            Avi.BITMAPINFOHEADER bih = new Avi.BITMAPINFOHEADER();
            bih.biBitCount      = countBitsPerPixel;
            bih.biClrImportant  = 0;
            bih.biClrUsed       = 0;
            bih.biCompression   = 0;
            bih.biPlanes        = 1;
            bih.biSize          = Marshal.SizeOf(bih);
            bih.biXPelsPerMeter = 0;
            bih.biYPelsPerMeter = 0;

            bih.biHeight = 0;
            bih.biWidth  = 0;

            if (bih.biBitCount > 24)
            {
                bih.biBitCount = 32;
            }
            else if (bih.biBitCount > 16)
            {
                bih.biBitCount = 24;
            }
            else if (bih.biBitCount > 8)
            {
                bih.biBitCount = 16;
            }
            else if (bih.biBitCount > 4)
            {
                bih.biBitCount = 8;
            }
            else if (bih.biBitCount > 0)
            {
                bih.biBitCount = 4;
            }

            getFrameObject = Avi.AVIStreamGetFrameOpen(StreamPointer, ref bih);

            if (getFrameObject == 0)
            {
                throw new Exception("Exception in VideoStreamGetFrameOpen!");
            }
        }
Exemplo n.º 9
0
        public VideoStream(int aviFile, IntPtr aviStream)
        {
            this.aviFile   = aviFile;
            this.aviStream = aviStream;

            Avi.BITMAPINFOHEADER bih = new Avi.BITMAPINFOHEADER();
            int size = Marshal.SizeOf(bih);

            Avi.AVIStreamReadFormat(aviStream, 0, ref bih, ref size);
            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(aviStream);

            this.frameRate         = (float)streamInfo.dwRate / (float)streamInfo.dwScale;
            this.width             = (int)streamInfo.rcFrame.right;
            this.height            = (int)streamInfo.rcFrame.bottom;
            this.frameSize         = bih.biSizeImage;
            this.countBitsPerPixel = bih.biBitCount;
            this.firstFrame        = Avi.AVIStreamStart(aviStream.ToInt32());
            this.countFrames       = Avi.AVIStreamLength(aviStream.ToInt32());
        }
Exemplo n.º 10
0
        private void CreateStreamWithoutFormat()
        {
            int    scale = 1;
            double rate  = frameRate;

            GetRateAndScale(ref rate, ref scale);

            Avi.AVISTREAMINFO strhdr = new Avi.AVISTREAMINFO();
            strhdr.fccType               = Avi.mmioStringToFOURCC("vids", 0);
            strhdr.fccHandler            = Avi.mmioStringToFOURCC("CVID", 0);
            strhdr.dwFlags               = 0;
            strhdr.dwCaps                = 0;
            strhdr.wPriority             = 0;
            strhdr.wLanguage             = 0;
            strhdr.dwScale               = (int)scale;
            strhdr.dwRate                = (int)rate;
            strhdr.dwStart               = 0;
            strhdr.dwLength              = 0;
            strhdr.dwInitialFrames       = 0;
            strhdr.dwSuggestedBufferSize = frameSize;
            strhdr.dwQuality             = -1;
            strhdr.dwSampleSize          = 0;
            strhdr.rcFrame.top           = 0;
            strhdr.rcFrame.left          = 0;
            strhdr.rcFrame.bottom        = (uint)height;
            strhdr.rcFrame.right         = (uint)width;
            strhdr.dwEditCount           = 0;
            strhdr.dwFormatChangeCount   = 0;
            strhdr.szName                = new UInt16[64];

            int result = Avi.AVIFileCreateStream(aviFile, out aviStream, ref strhdr);

            if (result != 0)
            {
                throw new Exception("Exception in AVIFileCreateStream: " + result.ToString());
            }
        }
Exemplo n.º 11
0
        private IntPtr InsertSilence(int countSilentSamples, IntPtr waveData, int lengthWave, ref Avi.AVISTREAMINFO streamInfo)
        {
            int lengthSilence = countSilentSamples * streamInfo.dwSampleSize;

            byte[] silence = new byte[lengthSilence];


            int    lengthNewStream = lengthSilence + lengthWave;
            IntPtr newWaveData     = Marshal.AllocHGlobal(lengthNewStream);


            Marshal.Copy(silence, 0, newWaveData, lengthSilence);


            byte[] sound = new byte[lengthWave];
            Marshal.Copy(waveData, sound, 0, lengthWave);
            IntPtr startOfSound = new IntPtr(newWaveData.ToInt32() + lengthSilence);

            Marshal.Copy(sound, 0, startOfSound, lengthWave);

            Marshal.FreeHGlobal(newWaveData);

            streamInfo.dwLength = lengthNewStream;
            return(newWaveData);
        }
Exemplo n.º 12
0
        public Bitmap GetBitmap(int position)
        {
            if (position > countFrames)
            {
                throw new Exception("Invalid frame position: " + position);
            }

            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(StreamPointer);

            int dib = Avi.AVIStreamGetFrame(getFrameObject, firstFrame + position);

            Avi.BITMAPINFOHEADER bih = new Avi.BITMAPINFOHEADER();
            bih = (Avi.BITMAPINFOHEADER)Marshal.PtrToStructure(new IntPtr(dib), bih.GetType());

            if (bih.biSizeImage < 1)
            {
                throw new Exception("Exception in VideoStreamGetFrame");
            }

            byte[] bitmapData;
            int    address = dib + Marshal.SizeOf(bih);

            if (bih.biBitCount < 16)
            {
                bitmapData = new byte[bih.biSizeImage + Avi.PALETTE_SIZE];
            }
            else
            {
                bitmapData = new byte[bih.biSizeImage];
            }

            Marshal.Copy(new IntPtr(address), bitmapData, 0, bitmapData.Length);

            byte[] bitmapInfo = new byte[Marshal.SizeOf(bih)];
            IntPtr ptr;

            ptr = Marshal.AllocHGlobal(bitmapInfo.Length);
            Marshal.StructureToPtr(bih, ptr, false);
            address = ptr.ToInt32();
            Marshal.Copy(new IntPtr(address), bitmapInfo, 0, bitmapInfo.Length);

            Marshal.FreeHGlobal(ptr);

            Avi.BITMAPFILEHEADER bfh = new Avi.BITMAPFILEHEADER();
            bfh.bfType      = Avi.BMP_MAGIC_COOKIE;
            bfh.bfSize      = (Int32)(55 + bih.biSizeImage);
            bfh.bfReserved1 = 0;
            bfh.bfReserved2 = 0;
            bfh.bfOffBits   = Marshal.SizeOf(bih) + Marshal.SizeOf(bfh);
            if (bih.biBitCount < 16)
            {
                bfh.bfOffBits += Avi.PALETTE_SIZE;
            }

            BinaryWriter bw = new BinaryWriter(new MemoryStream());

            bw.Write(bfh.bfType);
            bw.Write(bfh.bfSize);
            bw.Write(bfh.bfReserved1);
            bw.Write(bfh.bfReserved2);
            bw.Write(bfh.bfOffBits);
            bw.Write(bitmapInfo);
            bw.Write(bitmapData);

            Bitmap   bmp            = (Bitmap)Image.FromStream(bw.BaseStream);
            Bitmap   saveableBitmap = new Bitmap(bmp.Width, bmp.Height);
            Graphics g = Graphics.FromImage(saveableBitmap);

            g.DrawImage(bmp, 0, 0);
            g.Dispose();
            bmp.Dispose();

            bw.Close();
            return(saveableBitmap);
        }