Example #1
0
 public void openVideoWriter()
 {
     Accord.Math.Rational rationalFrameRate = new Accord.Math.Rational(30);
     colorWriter.Open(colorVideoPath, Width, Height, rationalFrameRate, VideoCodec.MPEG4, bitRate);
     frameCount = 0;
 }
Example #2
0
        private async Task loadVideoAsync(string path)
        {
            var progressHandler = new Progress <string>(value =>
            {
                status_txt.Text = value;
            });
            var progress = progressHandler as IProgress <string>;
            int tooFewFramesDelivered = 0;

            bool failed = false;

            await Task.Run(() =>
            {
                long frameCount;
                try
                {
                    VideoFileReader reader = new VideoFileReader();
                    reader.Open(path);

                    /*
                     * Console.WriteLine("width:  " + reader.Width);
                     * Console.WriteLine("height: " + reader.Height);
                     * Console.WriteLine("fps:    " + reader.FrameRate);
                     * Console.WriteLine("codec:  " + reader.CodecName);
                     * Console.WriteLine("length:  " + reader.FrameCount);
                     */

                    frameCount = reader.FrameCount;

                    int currentFrame = 0;

                    loadedVideo = new LinearAccessByteImage[frameCount];
                    frameRate   = reader.FrameRate;


                    while (true)
                    {
                        using (Bitmap videoFrame = reader.ReadVideoFrame())
                        {
                            if (videoFrame == null)
                            {
                                break;
                            }

                            loadedVideo[currentFrame] = Helpers.BitmapToLinearAccessByteArray(videoFrame);

                            if (currentFrame % 1000 == 0)
                            {
                                progress.Report("Loading video: " + currentFrame + "/" + frameCount + " frames");
                            }


                            currentFrame++;
                            // process the frame here
                        }
                    }

                    reader.Close();

                    // If the video delivered less frames than it promised (can happen for whatever reason) then we chip off the last parts of the array
                    if (currentFrame < frameCount)
                    {
                        tooFewFramesDelivered = (int)frameCount - currentFrame;
                        Array.Resize <LinearAccessByteImage>(ref loadedVideo, currentFrame);
                    }
                }
                catch (Exception e)
                {
                    failed = true;
                    MessageBox.Show(e.Message);
                }
            });

            if (failed)
            {
                return;
            }
            if (tooFewFramesDelivered > 0)
            {
                status_txt.Text = "Completed loading video. Video reader delivered " + tooFewFramesDelivered + " fewer frame(s) than it announced. Consider redoing the input video.";
            }
            else
            {
                status_txt.Text = "Completed loading video.";
            }
            videoIsLoaded = true;
            processVideo_button.IsEnabled            = true;
            saveSortedVideo_button.IsEnabled         = false;
            saveSortedFrameList_button.IsEnabled     = false;
            audio2spectrumLoadAudio_button.IsEnabled = true;
        }
Example #3
0
        private async Task vfssppCreate(string pathSource, string pathDestination)
        {
            vfssppExport_button.IsEnabled = false;
            var progressHandler = new Progress <string>(value =>
            {
                status_txt.Text = value;
            });
            var progress = progressHandler as IProgress <string>;
            int tooFewFramesDelivered = 0;

            bool failed = false;

            await Task.Run(() =>
            {
                long frameCount;
                long actualFrameCount;
                try
                {
                    VideoFileReader reader = new VideoFileReader();
                    reader.Open(pathSource);

                    /*
                     * Console.WriteLine("width:  " + reader.Width);
                     * Console.WriteLine("height: " + reader.Height);
                     * Console.WriteLine("fps:    " + reader.FrameRate);
                     * Console.WriteLine("codec:  " + reader.CodecName);
                     * Console.WriteLine("length:  " + reader.FrameCount);
                     */

                    actualFrameCount = frameCount = reader.FrameCount;

                    int currentFrame = 0;

                    LinearAccessByteImage loadedFrame;
                    frameRate = reader.FrameRate;

                    VFSSPPFile vfssppWriter = new VFSSPPFile(pathDestination, reader.Width, reader.Height, reader.FrameCount, reader.Width *reader.Height * 3);


                    while (true)
                    {
                        using (Bitmap videoFrame = reader.ReadVideoFrame())
                        {
                            if (videoFrame == null)
                            {
                                break;
                            }

                            loadedFrame = Helpers.BitmapToLinearAccessByteArray(videoFrame);


                            vfssppWriter.writeImage(loadedFrame);

                            if (currentFrame % 1000 == 0)
                            {
                                progress.Report("Converting video to VFSSPP: " + currentFrame + "/" + frameCount + " frames");
                            }


                            currentFrame++;
                            // process the frame here
                        }
                    }

                    reader.Close();

                    // If the video delivered less frames than it promised (can happen for whatever reason) then we chip off the last parts of the array
                    if (currentFrame < frameCount)
                    {
                        tooFewFramesDelivered = (int)frameCount - currentFrame;
                        actualFrameCount      = currentFrame;
                        vfssppWriter.amendFrameCount(actualFrameCount);
                    }
                    vfssppWriter.finalizeFile();
                }
                catch (Exception e)
                {
                    failed = true;
                    MessageBox.Show(e.Message);
                }
            });

            if (failed)
            {
                return;
            }
            if (tooFewFramesDelivered > 0)
            {
                status_txt.Text = "Completed converting video to VFSSPP. Video reader delivered " + tooFewFramesDelivered + " fewer frame(s) than it announced. Consider redoing the input video.";
            }
            else
            {
                status_txt.Text = "Completed converting video to VFSSPP.";
            }
            vfssppExport_button.IsEnabled = true;
        }
Example #4
0
        private void Form1_Load(object sender, System.EventArgs e)
        {
            // **************************************************
            IsRecording = false;
            // **************************************************

            // **************************************************
            //System.Drawing.Rectangle captureRegion =
            //	System.Windows.Forms.Screen.PrimaryScreen.Bounds;

            System.Drawing.Rectangle captureRegion =
                System.Windows.Forms.Screen.AllScreens
                .ToList()[0]
                .Bounds;

            //CaptureRegionWidth = captureRegion.Width;
            //CaptureRegionHeight = captureRegion.Height;

            CaptureRegionWidth  = (int)(captureRegion.Width * 1.5);
            CaptureRegionHeight = (int)(captureRegion.Height * 1.5);
            // **************************************************

            // https://support.video.ibm.com/hc/en-us/articles/207852117-Internet-connection-and-recommended-encoding-settings

            // **************************************************
            // 24, 30
            int videoFrameRate = 25;

            // 1200 - 4000 KbPS
            //  1200 * 1024
            //int videoBitRate = CaptureRegionWidth * CaptureRegionHeight;
            //int videoBitRate = 200 * 1000;
            int videoBitRate = 1200 * 1000;

            int videoKeyFrameInterval =
                System.Convert.ToInt32(1000 / (double)videoFrameRate);

            int videoFrameSize =
                CaptureRegionWidth * CaptureRegionHeight;

            Accord.Math.Rational videoFrameRateRational =
                new Accord.Math.Rational
                    (numerator: 1000, denominator: videoKeyFrameInterval);
            // **************************************************

            // **************************************************
            timerRecording.Interval = videoKeyFrameInterval;
            // **************************************************

            // **************************************************
            int audioSampleRate = 44100;

            // 320 * 1000
            // 320 * 1024
            // 128 * 1024
            int audioBitRate = 128 * 1000;

            int audioFrameSize = 10 * 4096;
            // **************************************************

            // **************************************************
            string pathName =
                $"D:\\_TEMP\\MOVIE_{ System.DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") }.avi";

            // **************************************************

            // **************************************************
            VideoFileWriter =
                new Accord.Video.FFMPEG.VideoFileWriter
            {
                Width  = CaptureRegionWidth,
                Height = CaptureRegionHeight,

                FrameRate = videoFrameRateRational,
                BitRate   = videoBitRate,

                VideoCodec = Accord.Video.FFMPEG.VideoCodec.H264,
            };

            // visually Lossless
            VideoFileWriter.VideoOptions["crf"]      = "18";
            VideoFileWriter.VideoOptions["preset"]   = "veryfast";
            VideoFileWriter.VideoOptions["tune"]     = "zerolatency";
            VideoFileWriter.VideoOptions["x264opts"] = "no-mbtree:sliced-threads:sync-lookahead=0";
            // **************************************************

            // **************************************************
            System.Guid?audioDeviceGuid = null;

            Accord.DirectSound.AudioDeviceCollection audioDeviceCollection =
                new Accord.DirectSound.AudioDeviceCollection(Accord.DirectSound.AudioDeviceCategory.Capture);

            foreach (Accord.DirectSound.AudioDeviceInfo audioDeviceInfo in audioDeviceCollection)
            {
                audioDeviceGuid = audioDeviceInfo.Guid;

                //System.Windows.Forms.MessageBox.Show
                //	($"Guid: { audioDeviceInfo.Guid } - Description: { audioDeviceInfo.Description }");
            }
            // **************************************************

            // **************************************************
            if (audioDeviceGuid.HasValue)
            {
                AudioCaptureDevice =
                    new Accord.DirectSound.AudioCaptureDevice(device: audioDeviceGuid.Value)
                {
                    SampleRate       = audioSampleRate,
                    DesiredFrameSize = audioFrameSize,
                    Format           = Accord.Audio.SampleFormat.Format32BitIeeeFloat,
                };

                AudioCaptureDevice.AudioSourceError += AudioCaptureDevice_AudioSourceError;
            }
            // **************************************************

            // **************************************************
            var audioCaptureDevices =
                new System.Collections.Generic.List <Accord.DirectSound.AudioCaptureDevice>();

            if (AudioCaptureDevice != null)
            {
                audioCaptureDevices.Add(AudioCaptureDevice);
            }
            // **************************************************

            // **************************************************
            if (audioCaptureDevices.Count > 0)
            {
                AudioSourceMixer =
                    new Accord.Audio.AudioSourceMixer(sources: audioCaptureDevices);

                AudioSourceMixer.NewFrame         += AudioSourceMixer_NewFrame;
                AudioSourceMixer.AudioSourceError += AudioSourceMixer_AudioSourceError;

                VideoFileWriter.FrameSize    = audioFrameSize;
                VideoFileWriter.AudioBitRate = audioBitRate;
                VideoFileWriter.SampleRate   = audioSampleRate;
                VideoFileWriter.AudioCodec   = Accord.Video.FFMPEG.AudioCodec.Aac;

                VideoFileWriter.AudioLayout =
                    AudioSourceMixer.NumberOfChannels == 1 ?
                    Accord.Video.FFMPEG.AudioLayout.Mono :
                    Accord.Video.FFMPEG.AudioLayout.Stereo;
            }
            // **************************************************

            VideoFileWriter.Open(fileName: pathName);
        }