如何将Kinect中的音频和视频录制到唯一文件中?

时间:2014-08-04 13:55:20

标签: c# audio video kinect recording

我正在尝试编写一个程序来录制Kinect传感器的音频和视频。

所以我尝试编写这段代码:

这是从kinect传感器录制音频的代码

        public void recordAudio()
        {
            //_lastRecordedFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_wav.wav";
            ////Start recording audio on new thread
            var t = new Thread(new ParameterizedThreadStart((RecordAudio)));
            t.Start(this.kinect);
        }

        private void RecordAudio(object kinectSensor)
        {

            //KinectSensor _sensor = (KinectSensor)kinectSensor;
            RecordAudio(this.kinect);
        }

        private void RecordAudio(KinectSensor kinectSensor)
        {

            if (kinectSensor == null)
            {
                return;
            }

            int recordingLength = (int)_amountOfTimeToRecord * 2 * 16000;
            byte[] buffer = new byte[1024];


            using (FileStream _fileStream = new FileStream(_lastRecordedFileName, FileMode.Create))
            {
                int totalCount = 0;
                WriteWavHeader(_fileStream, recordingLength);
                //Start capturing audio                               
                using (Stream audioStream = kinectSensor.AudioSource.Start())
                {
                    //Simply copy the data from the stream down to the file
                    int count = 0;
                    while ((count = audioStream.Read(buffer, 0, buffer.Length)) > 0 && totalCount < recordingLength)
                    {
                        _fileStream.Write(buffer, 0, count);
                        totalCount += count;
                    }
                }
                //WriteWavHeader(_fileStream, totalCount);
            }


            if (FinishedRecording != null)
            {
                FinishedRecording(null, null);
            }
        }

        static void WriteString(Stream stream, string s)
        {
            byte[] bytes = Encoding.ASCII.GetBytes(s);
            stream.Write(bytes, 0, bytes.Length);
        }

        /// <summary>
        /// A bare bones WAV file header writer
        /// </summary>        
        static void WriteWavHeader(Stream stream, int dataLength)
        {
            //We need to use a memory stream because the BinaryWriter will close the underlying stream when it is closed
            using (var memStream = new MemoryStream(64))
            {
                int cbFormat = 18; //sizeof(WAVEFORMATEX)
                WAVEFORMATEX format = new WAVEFORMATEX()
                {
                    wFormatTag = 1,
                    nChannels = 1,
                    nSamplesPerSec = 16000,
                    nAvgBytesPerSec = 32000,
                    nBlockAlign = 2,
                    wBitsPerSample = 16,
                    cbSize = 0
                };

                using (var bw = new BinaryWriter(memStream))
                {
                    //RIFF header
                    WriteString(memStream, "RIFF");
                    bw.Write(dataLength + cbFormat + 4); //File size - 8
                    WriteString(memStream, "WAVE");
                    WriteString(memStream, "fmt ");
                    bw.Write(cbFormat);

                    //WAVEFORMATEX
                    bw.Write(format.wFormatTag);
                    bw.Write(format.nChannels);
                    bw.Write(format.nSamplesPerSec);
                    bw.Write(format.nAvgBytesPerSec);
                    bw.Write(format.nBlockAlign);
                    bw.Write(format.wBitsPerSample);
                    bw.Write(format.cbSize);

                    //data header
                    WriteString(memStream, "data");
                    bw.Write(dataLength);
                    memStream.WriteTo(stream);
                }
            }
        }

这是从kinect传感器录制视频的代码

        /// <summary>
        /// il metodo una volta invocato provvederà alla registrazione del video
        /// prendendo in INPUT il file Name passato alla classe in fase di inizializzazione.
        /// </summary>
        public void recordVideo()
        {
            try
            {
                if (writer != null)
                {
                    // create new video file
                    writer.Open(this.nomeFile, 640, 480, 25, VideoCodec.MPEG4);
                }
            }
            catch (Exception e)
            {
                log.Error("LIBRERIA ACQUISIZIONE VIDEO ",e);
            }

        }

        /// <summary>
        /// Metodo utilizzato per registrare il video.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ColorImageReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            try
            {
                using (ColorImageFrame imageFrame = e.OpenColorImageFrame())
                {
                    //viene fatto un doppio controllo sul writer.Open, in quanto può capitare che al primo IF
                    //il writer sia ancora aperto mentre all'atto della scrittura nel file video il writer sia già chiuso
                    if (imageFrame != null && writer.IsOpen)
                    {
                        Bitmap image = ImageToBitmap(imageFrame);
                        if(image!=null && writer.IsOpen)
                            writer.WriteVideoFrame(image);
                        // writer.Close();
                    }
                }
            }
            catch (AccessViolationException exc)
            {
                log.Error("LIBRERIA ACQUISIZIONE VIDEO ", exc);
            }
            catch (Exception exc)
            {
                log.Error("LIBRERIA ACQUISIZIONE VIDEO ", exc);
            }

        }

        /// <summary>
        /// Il metodo consente di fermare la registrazione video,
        /// può essere invocato senza problemi in quanto verificherà se il flusso video è
        /// attivo e nel caso eseguirà lo stop
        /// </summary>
        public void stopRecord()
        {
            if (writer != null)
                writer.Close();
        }

        /// <summary>
        /// ImageToBitmap converte l'immagine in Bitmap image da
        /// poter poi passare al writer per creare il video
        /// </summary>
        /// <param name="Image"></param>
        /// <returns></returns>
        Bitmap ImageToBitmap(ColorImageFrame Image)
        {
            try
            {
                byte[] pixeldata = new byte[Image.PixelDataLength];
                Image.CopyPixelDataTo(pixeldata);
                Bitmap bmap = new Bitmap(Image.Width, Image.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                BitmapData bmapdata = bmap.LockBits(
                    new System.Drawing.Rectangle(0, 0, Image.Width, Image.Height),
                    ImageLockMode.WriteOnly,
                    bmap.PixelFormat);
                IntPtr ptr = bmapdata.Scan0;
                Marshal.Copy(pixeldata, 0, ptr, Image.PixelDataLength);
                bmap.UnlockBits(bmapdata);
                return bmap;
            }
            catch (Exception e)
            {
                return null;
            }
        }
    }

代码发现我将音频和视频文件录制到两个不同的文件中。现在我想将这些文件组合或将音频和视频录制到一个独特的文件中。这可能吗?

为了录制视频,我使用了FFOrgeVideo.dll。

0 个答案:

没有答案