/// <summary> /// Creates a new file that will be used to store an M-Maze behavior session /// </summary> /// <param name="rat_name">The rat name that is being run in the M-Maze</param> public void CreateFile(string rat_name, int width, int height) { lock (_writer_object_lock) { //If no rat name is defined, we will define a default name here if (string.IsNullOrEmpty(rat_name)) { rat_name = "_UNDEFINED_ANIMAL_NAME_"; } //Figure out the path to the saved file var path = MMazeConfiguration.GetInstance().SavePath; var rat_path = rat_name + "/videos/"; var fully_qualified_path = path + rat_path; //Create the path if it does not already exist Directory.CreateDirectory(fully_qualified_path); //Create a file name for this new session var file_time = DateTime.Now.ToFileTime(); string file_name = rat_name + "-" + file_time.ToString(); string file_name_with_extension = file_name + ".avi"; //Combine the path and file name to get a full path string fully_qualified_path_and_file = path + rat_path + file_name_with_extension; //Create the new handle to the new file _writer = new Accord.Video.FFMPEG.VideoFileWriter(); _writer.Open(fully_qualified_path_and_file, width, height); //Keep track of when the last frame was written to this video //Initialize this to the current time for the new video being written. _last_call = DateTime.Now; } }
private void button11_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { Image <Gray, Byte> last = null; Image <Gray, float> flowX = null; Image <Gray, float> flowY = null; Image <Gray, byte> draw = null; MKeyPoint[] keys; Accord.Video.FFMPEG.VideoFileWriter wr = new Accord.Video.FFMPEG.VideoFileWriter(); VideoProcessig videoProcessig = new VideoProcessig(openFileDialog1.FileName, (map) => { Image <Gray, Byte> image = new Image <Gray, byte>(map); if (last == null) { last = image; flowX = new Image <Gray, float>(map.Size); flowY = new Image <Gray, float>(map.Size); draw = new Image <Gray, byte>(map.Size); wr.Open("result.avi", map.Width, map.Height); return(map); } else { CvInvoke.CalcOpticalFlowFarneback(last, image, flowX, flowY, 0.5, 3, 10, 3, 5, 1.5, Emgu.CV.CvEnum.OpticalflowFarnebackFlag.Default); last = image; } CvInvoke.AccumulateSquare(flowX, flowY); CvInvoke.Canny(flowY.Convert <byte>(FloatToByte), draw, 40, 50); var result = draw.Bitmap; wr.WriteVideoFrame(result); return(result); }); videoProcessig.Show(); } byte FloatToByte(float val) { return((byte)val); } }
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); }