/// <summary> /// Save a stream of image frames as an AVI video file. /// </summary> /// <param name="frames">Ordered list of frames at 30 fps.</param> /// <param name="width">Frame width.</param> /// <param name="height">Frame height.</param> /// <param name="file">Output filename.</param> public static void SaveAsAvi(List <Color[]> frames, int width, int height, string file) { if (frames.Count == 0) { return; } using (VideoWriter writer = new VideoWriter(file, new Size(width, height), 30, true, VideoCodec.FromName("MJPG"))) { writer.Open(); foreach (Color[] frame in frames) { Bgr <byte>[,] bitmap = new Bgr <byte> [height, width]; int h = 0; for (int k = 0; k < height; k++) { for (int i = 0; i < width; i++) { bitmap[k, i] = new Bgr <byte>(frame[h].B, frame[h].G, frame[h].R); h++; } } writer.Write(bitmap.Lock()); } writer.Close(); } }
/// <summary> /// Uses the dotImaging library to compile a video from frames. Video should be an .avi to guarantee it working. /// </summary> /// <param name="imagePaths">Path to images to compile from.</param> /// <param name="videoPath">The path to the new video.</param> public void imagesToVideo(List <String> imagePaths, String videoPath) { Bitmap im = new Bitmap(imagePaths[0]); int width = im.Width; int height = im.Height; ImageStreamWriter videoWriter = new VideoWriter(videoPath, new DotImaging.Primitives2D.Size(width, height)); foreach (String frame in imagePaths) { IImage image = ImageIO.LoadUnchanged(frame); videoWriter.Write(image); image.Dispose(); } videoWriter.Close(); }
/// <summary> /// Creates video out of the latest screenshots made for the current Tag. /// </summary> public static void mkVideo() { var screenSize = ScreenSize; var videoSize = new DotImaging.Primitives2D.Size(screenSize.X, screenSize.Y); string videoTarget = DirectoryName + @"\" + Tag + "_" + TimeStamp + ".avi"; Console.WriteLine(string.Format("Writing video file: {0}", videoTarget)); ImageDirectoryCapture images = new ImageDirectoryCapture(DirectoryName, Tag + "_*.jpeg"); if (images.Length == 0) { return; } ImageStreamWriter videoWriter = new VideoWriter(videoTarget, videoSize, Configuration.VideoFPS); List <string> toDelete = new List <string>(); while (images.Position < images.Length) { string f = images.CurrentImageName; Console.WriteLine(string.Format(" frame: {0}", f)); IImage image = images.Read(); videoWriter.Write(image); toDelete.Add(f); } videoWriter.Close(); Console.WriteLine("END writing video"); Console.WriteLine("Removing frame files"); foreach (string f in toDelete) { if (f == null) { continue; } File.Delete(f); Console.WriteLine(string.Format("Deleted file: {0}", f)); } Console.WriteLine("Done Removing frame files"); }
internal static void Main() { // The exit code of the sample application. int exitCode = 0; // Check if VideoWriter is supported and all required DLLs are available. if (!VideoWriter.IsSupported) { Console.WriteLine("VideoWriter is not supported at the moment. Please install the pylon Supplementary Package for MPEG-4 which is available on the Basler website."); // Return with error code 1. Environment.Exit(1); } try { // Create a camera object that selects the first camera device found. // More constructors are available for selecting a specific camera device. using (Camera camera = new Camera()) { // Open the connection to the camera device. camera.Open(); // Print the model name of the camera. Console.WriteLine("Using camera {0}.", camera.CameraInfo[CameraInfoKey.ModelName]); // Optional: Depending on your camera or computer, you may not be able to save // a video without losing frames. Therefore, we limit the resolution: camera.Parameters[PLCamera.Width].SetValue(640, IntegerValueCorrection.Nearest); camera.Parameters[PLCamera.Height].SetValue(480, IntegerValueCorrection.Nearest); camera.Parameters[PLCamera.PixelFormat].TrySetValue(PLCamera.PixelFormat.Mono8); // We also increase the number of memory buffers to be used while grabbing. camera.Parameters[PLCameraInstance.MaxNumBuffer].SetValue(20); // Create and open the VideoWriter. using (VideoWriter writer = new VideoWriter()) { // Set a quality of 90 for the video (value range is 1 to 100). writer.Parameters[PLVideoWriter.Quality].SetValue(90); // This will create a compressed video file. writer.Create(videoFilename, 25, camera); // Start grabbing. camera.StreamGrabber.Start(countOfImagesToGrab); Console.WriteLine("Please wait. Images are being grabbed."); while (camera.StreamGrabber.IsGrabbing) { // Wait for an image and then retrieve it. A timeout of 5000 ms is used. IGrabResult grabResult = camera.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException); using (grabResult) { // Image grabbed successfully? if (grabResult.GrabSucceeded) { // Write the image to the video file. writer.Write(grabResult); } else { Console.WriteLine("Error: {0} {1}", grabResult.ErrorCode, grabResult.ErrorDescription); } } } // Stop grabbing. camera.StreamGrabber.Stop(); // Close the video file. writer.Close(); } // Close the connection to the camera device. camera.Close(); } } catch (Exception e) { // Error handling. Console.Error.WriteLine("Exception: {0}", e.Message); exitCode = 1; } finally { // Comment the following two lines to disable waiting on exit. Console.Error.WriteLine("\nPress enter to exit."); Console.ReadLine(); } Environment.Exit(exitCode); }
public API_AForge_Video close() { VideoWriter.Close(); return(this); }