static void Main(string[] args)
        {
            var endOfRec = DateTime.Now.Add(TimeSpan.FromSeconds(30));
            using (var writer = new AviWriter("test.avi")
            {
                FramesPerSecond = 15,
                EmitIndex1 = true
            })
            {
                var stream = writer.AddVideoStream();

                stream.Width = Screen.PrimaryScreen.WorkingArea.Width;
                stream.Height = Screen.PrimaryScreen.WorkingArea.Height;
                stream.Codec = KnownFourCCs.Codecs.Uncompressed;
                stream.BitsPerPixel = BitsPerPixel.Bpp32;

                var googleChrome = new TestCase();

                Task.Factory.StartNew(() =>
                {
                    googleChrome.Scenario("http://www.google.com", "что посмотреть сегодня?");
                });

                var buffer = new byte[Screen.PrimaryScreen.WorkingArea.Width * Screen.PrimaryScreen.WorkingArea.Height * 4];
                while (!TestCase.isFinished)
                {
                    GetScreenshot(buffer);
                    stream.WriteFrame(true, buffer, 0, buffer.Length);
                }
            }

            Console.WriteLine("Execution Done");
        }
        private void StartRecordingThread(string videoFileName, ISeleniumTest scenario, int fps)
        {
            fileName = videoFileName;

            using (writer = new AviWriter(videoFileName)
            {
                FramesPerSecond = fps,
                EmitIndex1 = true
            })
            {
                stream = writer.AddVideoStream();
                this.ConfigureStream(stream);

                while (!scenario.IsFinished || forceStop)
                {
                    GetScreenshot(buffer);

                    //Thread safety issues
                    lock (locker)
                    {
                        stream.WriteFrame(true, buffer, 0, buffer.Length);
                    }

                }
            }

        }
Beispiel #3
0
        public static void CreateVideo(List<Frame> frames, string outputFile)
        {
            var writer = new AviWriter(outputFile)
            {
                FramesPerSecond = 30,
                // Emitting AVI v1 index in addition to OpenDML index (AVI v2)
                // improves compatibility with some software, including
                // standard Windows programs like Media Player and File Explorer
                EmitIndex1 = true
            };

            // returns IAviVideoStream
            var stream = writer.AddVideoStream();

            // set standard VGA resolution
            stream.Width = 640;
            stream.Height = 480;

            // class SharpAvi.KnownFourCCs.Codecs contains FOURCCs for several well-known codecs
            // Uncompressed is the default value, just set it for clarity
            stream.Codec = KnownFourCCs.Codecs.Uncompressed;

            // Uncompressed format requires to also specify bits per pixel
            stream.BitsPerPixel = BitsPerPixel.Bpp32;

            var frameData = new byte[stream.Width * stream.Height * 4];

            foreach (var item in frames)
            {

                // Say, you have a System.Drawing.Bitmap
                Bitmap bitmap = (Bitmap)item.Image;

                // and buffer of appropriate size for storing its bits
                var buffer = new byte[stream.Width * stream.Height * 4];

                var pixelFormat = PixelFormat.Format32bppRgb;

                // Now copy bits from bitmap to buffer
                var bits = bitmap.LockBits(new Rectangle(0, 0, stream.Width, stream.Height), ImageLockMode.ReadOnly, pixelFormat);

                //Marshal.Copy(bits.Scan0, buffer, 0, buffer.Length);

                Marshal.Copy(bits.Scan0, buffer, 0, buffer.Length);

                bitmap.UnlockBits(bits);

                // and flush buffer to encoding stream
                stream.WriteFrame(true, buffer, 0, buffer.Length);

            }

            writer.Close();
        }
Beispiel #4
0
        void Record()
        {
            int  FrameRate = (int)FpsUpDown.Value;
            Size RSize     = RecordPanel.Size;

            int       RndNum            = new Random().Next();
            string    OutFile           = Path.GetFullPath(string.Format("rec_{0}.avi", RndNum));
            string    OutFileCompressed = Path.GetFullPath(string.Format("rec_{0}.webm", RndNum));
            AviWriter Writer            = new AviWriter(OutFile);

            Writer.FramesPerSecond = FrameRate;
            Writer.EmitIndex1      = true;

            IAviVideoStream VStream = Writer.AddVideoStream(RSize.Width, RSize.Height, BitsPerPixel.Bpp24);

            VStream.Codec        = KnownFourCCs.Codecs.Uncompressed;
            VStream.BitsPerPixel = BitsPerPixel.Bpp24;

            Bitmap   Bmp = new Bitmap(RSize.Width, RSize.Height);
            Graphics G   = Graphics.FromImage(Bmp);

            Stopwatch SWatch = new Stopwatch();

            SWatch.Start();

            while (!AbortRecording)
            {
                G.CopyFromScreen(DesktopLocation.Add(RecorderOffset), Point.Empty, RSize);
                //G.DrawString("Text Embedding", SystemFonts.CaptionFont, Brushes.Red, new PointF(0, 0));
                Bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
                byte[] Data = Bmp.ToByteArray();
                VStream.WriteFrame(true, Data, 0, Data.Length);
                while ((float)SWatch.ElapsedMilliseconds / 1000 < 1.0f / FrameRate)
                {
                    ;
                }
                SWatch.Restart();
            }

            G.Dispose();
            Writer.Close();

            if (WebmCheckBox.Checked)
            {
                Program.FFMPEG("-i \"{0}\" -c:v libvpx -b:v 1M -c:a libvorbis \"{1}\"", OutFile, OutFileCompressed);
                File.Delete(OutFile);
            }
        }
Beispiel #5
0
        //create the video locally
        public async Task <string> CreateVideoAsync(List <byte[]> images, int frameRate, int width, int height)
        {
            guid = Guid.NewGuid();
            string fileName            = guid.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Millisecond + ".avi";
            string compressedVideoName = null;
            string path = Path.Combine(webHostEnvironment.WebRootPath, "videos", fileName);

            try
            {
                var writer = new AviWriter(path)
                {
                    FramesPerSecond = frameRate,
                    EmitIndex1      = true
                };
                var stream = writer.AddVideoStream();
                stream.Width        = width;
                stream.Height       = height;
                stream.Codec        = KnownFourCCs.Codecs.Uncompressed;
                stream.BitsPerPixel = BitsPerPixel.Bpp32;

                foreach (var image in images)
                {
                    //convert the byte array of the frame to bitmap and flip it upside down
                    var bm = aviVideoServices.ToBitmap(image);

                    //reduce the frames size to match the video size
                    var rbm = aviVideoServices.ReduceBitmap(bm, width, height);

                    //convert the bitmap to byte array
                    byte[] fr = aviVideoServices.BitmapToByteArray(rbm);

                    //write the frame to the video
                    await stream.WriteFrameAsync(true, fr, 0, fr.Length);
                }

                writer.Close();

                //compress the video
                compressedVideoName = compressService.CompressAndConvertVideo(fileName);
                return(compressedVideoName);
            }

            finally
            {
                File.Delete(path);
            }
        }
        private void RenderVideo()
        {
            try
            {
                if (!TrackResults)
                {
                    return;
                }


                Log.Debug($"Generated all images, now rendering movie.");

                //var files = Directory.EnumerateFiles(@"D:\Temp\Light\", "*.bmp");
                //files = files.OrderBy(s => s);

                //int fps = (int) (RenderingTasks.Count()/10f); // Movie should last 5 seconds
                int fps = 10;

                var writer = new AviWriter(@"D:\Temp\Light\test.avi")
                {
                    FramesPerSecond = fps,
                    // Emitting AVI v1 index in addition to OpenDML index (AVI v2)
                    // improves compatibility with some software, including
                    // standard Windows programs like Media Player and File Explorer
                    EmitIndex1 = true
                };

                var stream = writer.AddVideoStream();
                stream.Width  = GetWidth();
                stream.Height = GetHeight();
                stream.Codec  = KnownFourCCs.Codecs.Uncompressed;

                stream.BitsPerPixel = BitsPerPixel.Bpp32;

                Log.Debug($"Waiting for image rendering of {RenderingTasks.Count} images to complete");
                foreach (var renderingTask in RenderingTasks)
                {
                    renderingTask.RunSynchronously();
                    Bitmap image = renderingTask.Result;
                    //}

                    //foreach (var file in files)
                    //{
                    lock (_imageSync)
                    {
                        //Bitmap image = (Bitmap) Image.FromFile(file);
                        //image = new Bitmap(image, stream.Width, stream.Height);

                        byte[] imageData = (byte[])ToByteArray(image, ImageFormat.Bmp);

                        if (imageData == null)
                        {
                            Log.Warn($"No image data for file.");
                            continue;
                        }

                        if (imageData.Length != stream.Height * stream.Width * 4)
                        {
                            imageData = imageData.Skip(imageData.Length - (stream.Height * stream.Width * 4)).ToArray();
                        }

                        // fill frameData with image

                        // write data to a frame
                        stream.WriteFrame(true,            // is key frame? (many codecs use concept of key frames, for others - all frames are keys)
                                          imageData,       // array with frame data
                                          0,               // starting index in the array
                                          imageData.Length // length of the data
                                          );
                    }
                }

                writer.Close();
            }
            catch (Exception e)
            {
                Log.Error("Rendering movie", e);
            }
        }
Beispiel #7
0
 public void FileWriterLoop()
 {
     try
     {
         writer = new AviWriter(fileName)
         {
             FramesPerSecond = frameRate,
             // Emitting AVI v1 index in addition to OpenDML index (AVI v2)
             // improves compatibility with some software, including
             // standard Windows programs like Media Player and File Explorer
             EmitIndex1 = true
         };
         var stream = writer.AddVideoStream();
         //var codecs = Mpeg4VideoEncoderVcm.GetAvailableCodecs();
         //UniLog.Log(codecs.ToString());
         //FourCC selectedCodec = KnownFourCCs.Codecs.MotionJpeg;
         //var encoder = new Mpeg4VideoEncoderVcm(width, height,
         //                                    frameRate, // frame rate
         //                                    0, // number of frames, if known beforehand, or zero
         //                                    70 // quality, though usually ignored :(
         //                                    );
         //var encoder = new SingleThreadedVideoEncoderWrapper(() => new Mpeg4VideoEncoderVcm(width, height,
         //                                    frameRate, // frame rate
         //                                    0, // number of frames, if known beforehand, or zero
         //                                    70, // quality, though usually ignored :(
         //                                    selectedCodec // codecs preference
         //    ));
         //var stream = writer.AddEncodingVideoStream(encoder, width: width, height: height);
         //var stream = writer.AddMpeg4VideoStream(width, height, frameRate, 0, 70, selectedCodec, false);
         //var stream = writer.AddMotionJpegVideoStream(width, height, 70);
         stream.Height       = height;
         stream.Width        = width;
         stream.Codec        = KnownFourCCs.Codecs.Uncompressed;
         stream.BitsPerPixel = BitsPerPixel.Bpp32;
         byte[] frame;
         while (true)
         {
             //UniLog.Log("writerloop");
             if (should_finish)
             {
                 writer.Close();
                 break;
             }
             if (framesQueue.TryDequeue(out frame))
             {
                 //UniLog.Log("Writing");
                 //Bitmap bmp = ToBitmap(frame);
                 //Bitmap bmpReduced = ReduceBitmap(bmp, width, height);
                 //System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width,height,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                 //var bits = bitmap.LockBits(new Rectangle(0, 0, width,height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                 //Marshal.Copy(frame, 0, bits.Scan0, frame.Length);
                 //bitmap.UnlockBits(bits);
                 //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                 //using (var gr = Graphics.FromImage(bmp))
                 //    gr.DrawImage(bitmap, new Rectangle(0, 0, width, height));
                 //var buffer = new byte[width * height * 4];//(widght - _ - height - _ - 4);
                 //var bits2 = bmp.LockBits(new Rectangle(0, 0, stream.Width, stream.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                 //Marshal.Copy(bits2.Scan0, buffer, 0, buffer.Length);
                 //bitmap.UnlockBits(bits2);
                 stream.WriteFrame(true, frame, 0, frame.Length);
             }
         }
         //loop_finished = true;
         writtingThreadFinnishEvent.Set();
     } catch (Exception e)
     {
         UniLog.Log("OwO: " + e.Message);
         UniLog.Log(e.StackTrace);
     } finally
     {
         writtingThreadFinnishEvent.Set();
     }
 }
Beispiel #8
0
        public static void Decode(string filename, Stream fs, bool swapped, long framePosition)
        {
            char progress = ' ';

            var aviWriter = new AviWriter(filename + ".avi")
            {
                EmitIndex1 = true, FramesPerSecond = 18
            };

            IAviVideoStream videoStream = aviWriter.AddVideoStream(144, 80, BitsPerPixel.Bpp24);

            videoStream.Codec = KnownFourCCs.Codecs.Uncompressed;
            IAviAudioStream audioStream = aviWriter.AddAudioStream(2, 17784, 8);

            fs.Position = framePosition;
            byte[] frameBuffer = new byte[19760];
            fs.Read(frameBuffer, 0, frameBuffer.Length);

            int audioStart = swapped ? 9 : 8;

            byte[] frameMarkerToUse = swapped ? SwappedFrameMarker : FrameMarker;
            byte[] frameMaskToUse   = swapped ? SwappedFrameMask : FrameMask;

            if (swapped)
            {
                frameBuffer = Swapping.SwapBuffer(frameBuffer);
            }

            var outFs = new MemoryStream();

            for (int i = 9; i <= frameBuffer.Length; i += 10)
            {
                switch ((i / 10) % 4)
                {
                case 0:
                    progress = '-';

                    break;

                case 1:
                    progress = '\\';

                    break;

                case 2:
                    progress = '|';

                    break;

                case 3:
                    progress = '/';

                    break;
                }

                Console.Write($"\r{Localization.ExtractingAudio}", progress);
                outFs.WriteByte(frameBuffer[i]);
            }

            byte[] videoFrame = Color.DecodeFrame(frameBuffer);
            videoStream.WriteFrame(true, videoFrame, 0, videoFrame.Length);
            audioStream.WriteBlock(outFs.ToArray(), 0, (int)outFs.Length);

            int totalFrames = 1;

            framePosition += 19760;
            byte[] buffer = new byte[frameMarkerToUse.Length];

            while (framePosition + 19760 < fs.Length)
            {
                switch (totalFrames % 4)
                {
                case 0:
                    progress = '-';

                    break;

                case 1:
                    progress = '\\';

                    break;

                case 2:
                    progress = '|';

                    break;

                case 3:
                    progress = '/';

                    break;
                }

                Console.Write($"\r{Localization.LookingForMoreFrames}", progress);

                for (int i = 0; i < buffer.Length; i++)
                {
                    buffer[i] &= frameMaskToUse[i];
                }

                if (!buffer.SequenceEqual(frameMarkerToUse))
                {
                    Console.Write("\r                                      \r");
                    Console.WriteLine(Localization.FrameAndNextAreNotAligned, totalFrames);
                    long expectedFramePosition = framePosition;

                    while (framePosition < fs.Length)
                    {
                        fs.Position = framePosition;
                        fs.Read(buffer, 0, buffer.Length);

                        for (int i = 0; i < buffer.Length; i++)
                        {
                            buffer[i] &= frameMaskToUse[i];
                        }

                        if (buffer.SequenceEqual(frameMarkerToUse))
                        {
                            Console.Write("\r                                      \r");

                            fs.Position = framePosition;
                            frameBuffer = new byte[19760];
                            fs.Read(frameBuffer, 0, frameBuffer.Length);

                            if (swapped)
                            {
                                frameBuffer = Swapping.SwapBuffer(frameBuffer);
                            }

                            outFs = new MemoryStream();

                            for (int i = 9; i <= frameBuffer.Length; i += 10)
                            {
                                switch ((i / 10) % 4)
                                {
                                case 0:
                                    progress = '-';

                                    break;

                                case 1:
                                    progress = '\\';

                                    break;

                                case 2:
                                    progress = '|';

                                    break;

                                case 3:
                                    progress = '/';

                                    break;
                                }

                                Console.Write($"\r{Localization.ExtractingAudio}", progress);
                                outFs.WriteByte(frameBuffer[i]);
                            }

                            videoFrame = Color.DecodeFrame(frameBuffer);
                            videoStream.WriteFrame(true, videoFrame, 0, videoFrame.Length);
                            audioStream.WriteBlock(outFs.ToArray(), 0, (int)outFs.Length);

                            totalFrames++;
                            Console.Write("\r                                      \r");

                            Console.WriteLine(Localization.FrameFoundAtPosition, framePosition, totalFrames,
                                              framePosition - expectedFramePosition);

                            Console.
                            WriteLine(framePosition % 2352 == 0 ? Localization.FrameIsAtSectorBoundary : Localization.FrameIsNotAtSectorBoundary,
                                      totalFrames);

                            framePosition += 19760;

                            break;
                        }

                        framePosition++;
                    }

                    continue;
                }

                if (framePosition % 2352 == 0)
                {
                    Console.Write("\r                                      \r");
                    Console.WriteLine(Localization.FrameIsAtSectorBoundary, totalFrames);
                }

                Console.Write("\r                                      \r");
                fs.Position = framePosition;
                frameBuffer = new byte[19760];
                fs.Read(frameBuffer, 0, frameBuffer.Length);

                if (swapped)
                {
                    frameBuffer = Swapping.SwapBuffer(frameBuffer);
                }

                outFs = new MemoryStream();

                for (int i = 9; i <= frameBuffer.Length; i += 10)
                {
                    switch ((i / 10) % 4)
                    {
                    case 0:
                        progress = '-';

                        break;

                    case 1:
                        progress = '\\';

                        break;

                    case 2:
                        progress = '|';

                        break;

                    case 3:
                        progress = '/';

                        break;
                    }

                    Console.Write($"\r{Localization.ExtractingAudio}", progress);
                    outFs.WriteByte(frameBuffer[i]);
                }

                videoFrame = Color.DecodeFrame(frameBuffer);
                videoStream.WriteFrame(true, videoFrame, 0, videoFrame.Length);
                audioStream.WriteBlock(outFs.ToArray(), 0, (int)outFs.Length);

                totalFrames++;
                fs.Position = framePosition;
                fs.Read(buffer, 0, buffer.Length);
                framePosition += 19760;
            }

            Console.Write("\r                                      \r");
            Console.WriteLine(Localization.FramesFound, totalFrames);

            outFs.Close();
            aviWriter.Close();
        }
Beispiel #9
0
        private bool StartRecording()
        {
            int captureWidth = (int)(m_Capture.CaptureBounds.Width * m_Capture.Scale),
                captureHeight = (int)(m_Capture.CaptureBounds.Height * m_Capture.Scale),
                codecSelectedIndex = comboBox_videoCodec.SelectedIndex,
                codecQuality = Convert.ToInt32(textBox_recordQuality.Text);

            try
            {
                m_AviWriter = new AviWriter(m_AviFilePath)
                {
                    FramesPerSecond = m_Capture.FramesPerSecond,
                    EmitIndex1 = true,
                };

                if (codecSelectedIndex == 0)
                {
                    m_AviVideoStream = m_AviWriter.AddVideoStream(captureWidth, captureHeight);
                }
                else if (codecSelectedIndex == 1)
                {
                    m_AviVideoStream = m_AviWriter.AddMotionJpegVideoStream(captureWidth, captureHeight, codecQuality);
                }
                else
                {
                    var codecs = Mpeg4VideoEncoderVcm.GetAvailableCodecs();
                    var encoder = new Mpeg4VideoEncoderVcm(captureWidth, captureHeight, m_Capture.FramesPerSecond, 0, codecQuality, codecs[codecSelectedIndex - 2].Codec);
                    m_AviVideoStream = m_AviWriter.AddEncodingVideoStream(encoder);
                }


                if (checkBox_recordAudio.Checked)
                {
                    m_AviAudioStream = m_AviWriter.AddAudioStream(m_Recorder.WaveFormat.Channels, m_Recorder.WaveFormat.SampleRate, m_Recorder.WaveFormat.BitsPerSample);
                }
            }
            catch
            {
                Debug.Log("Failed to Start Recording.");
                return false;
            }

            return true;
        }