private void H264Encode(string[] paths, int fps)
        {
            var firstFrame = new Bitmap(paths[0]);

            // AVIに出力するライターを作成
            var aviPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test.avi";
            var aviFile = System.IO.File.OpenWrite(aviPath);
            var writer  = new H264Writer(aviFile, firstFrame.Width, firstFrame.Height, fps);

            // H264エンコーダーを作成
            var encoder = new OpenH264Lib.Encoder("openh264-1.7.0-win32.dll");

            // 1フレームエンコードするごとにライターに書き込み
            OpenH264Lib.Encoder.OnEncodeCallback onEncode = (data, length, frameType) =>
            {
                var keyFrame = (frameType == OpenH264Lib.Encoder.FrameType.IDR) || (frameType == OpenH264Lib.Encoder.FrameType.I);
                writer.AddImage(data, keyFrame);
                Console.WriteLine("Encord {0} bytes, KeyFrame:{1}", length, keyFrame);
            };

            // H264エンコーダーの設定
            encoder.Setup(firstFrame.Width, firstFrame.Height, 5000000, fps, 2.0f, onEncode);

            // 1フレームごとにエンコード実施
            for (int i = 0; i < paths.Length; i++)
            {
                var bmp = new Bitmap(paths[i]);
                encoder.Encode(bmp, i);
                bmp.Dispose();
            }

            writer.Close();

            MessageBox.Show(string.Format("{0}\n is created.", aviPath));
        }
Exemplo n.º 2
0
        public void Start()
        {
            _h264Encoder = new OpenH264Lib.Encoder(@"lib\openh264-1.8.0-win32.dll");

            OpenH264Lib.Encoder.OnEncodeCallback onEncode = (data, length, frameType) =>
            {
                var keyFrame = (frameType == OpenH264Lib.Encoder.FrameType.IDR) || (frameType == OpenH264Lib.Encoder.FrameType.I);
                //writer.AddImage(data, keyFrame);
                if (ReceivedYUVFrame != null)
                {
                    ReceivedYUVFrame((uint)_stopwatch.ElapsedMilliseconds, data, keyFrame);
                }
                Console.WriteLine("Encord {0} bytes, KeyFrame:{1}", length, keyFrame);
            };

            _h264Encoder.Setup(960, 480, 5000000, 15, 2.0f, onEncode);

            _stopwatch = new Stopwatch();
            _stopwatch.Start();

            Task.Run(() =>
            {
                while (true)
                {
                    Bitmap bitmap = ConvertToBitmap(@"C:\Users\hamow\OneDrive\Pictures\aa.jpg");
                    _h264Encoder.Encode(bitmap, _stopwatch.ElapsedMilliseconds);
                    SpinWait.SpinUntil(() => false, 50);
                }
            });
        }
        private void Screencast()
        {
            //Write data
            Task.Factory.StartNew(() => SharedData(datas));

            //Read screen
            Task.Factory.StartNew(() =>
            {
                while (IsRun())
                {
                    graphics.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, screen.Bounds.Size);
                }
                graphics?.Dispose();
            });

            //Encode img
            Task.Factory.StartNew(() =>
            {
                while (IsRun())
                {
                    lock (lockEncode)
                    {
                        encoder.Encode(bmp);
                    }
                }
            });

            //ReadData
            Task.Factory.StartNew(ReadData);
        }
Exemplo n.º 4
0
 public void addBitmapFrame(byte[] data, int frameNumber)
 {
     lock (this)
     {
         byte[] copy_buf = new byte[data.Length];
         Array.Copy(data, 0, copy_buf, 0, data.Length);
         var bmp = new Bitmap(new MemoryStream(copy_buf));
         encoder.Encode(bmp, frameNumber);
         bmp.Dispose();
     }
 }
Exemplo n.º 5
0
        private void Stream(Socket socket, int framerate)
        {
            VideoHandle videoHandle = new VideoHandle();

            System.Drawing.Size size = new System.Drawing.Size(1920, 1080);
            encoder.Setup(size.Width, size.Height, 5120 * 1000, framerate, 1f, (data, length, FrameType) =>
            {
                socket.Send(BitConverter.GetBytes(length));
                socket.Send(data);
            }
                          );
            while (!abort)
            {
                int index = 0;
                try
                {
                    Invoke((Action) delegate { index = CameraSelect.SelectedIndex; });
                }
                catch
                {
                    break;
                }
                if (index == 0)
                {
                    videoHandle.GetFrames(VideoCapType.TYPE_SCREEN);
                }
                else
                {
                    videoHandle.GetFrames(VideoCapType.TYPE_WEBCAM, index - 1);
                }

                if (videoHandle.frame != null)
                {
                    Bitmap resized = ImageSerialize.ResizeImg((Bitmap)videoHandle.frame.Clone(), size);
                    encoder.Encode(resized);
                    resized.Dispose();
                }
                Thread.Sleep(1000 / framerate);
            }
        }
Exemplo n.º 6
0
        private void BtnTestA_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog()
            {
                Multiselect = true
            };

            dialog.Filter = "Image Files (*.bmp, *.png, *.jpg)|*.bmp;*.png;*.jpg";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                using (OpenH264Lib.Encoder encoder = new OpenH264Lib.Encoder(dllPath))
                    using (OpenH264Lib.Decoder decoder = new OpenH264Lib.Decoder(dllPath))
                    {
                        // 1フレームエンコードするごとにライターに書き込み
                        OpenH264Lib.Encoder.OnEncodeCallback onEncode = (data, length, frameType) =>
                        {
                            bool   keyFrame = (frameType == OpenH264Lib.Encoder.FrameType.IDR) || (frameType == OpenH264Lib.Encoder.FrameType.I);
                            Bitmap image    = decoder.Decode(data.Take(length).ToArray(), length);
                            Image  old      = pbxScreen.Image;
                            pbxScreen.Image = image;
                            if (old != null)
                            {
                                old.Dispose();
                            }
                            Application.DoEvents();
                        };
                        for (int i = 0; i < dialog.FileNames.Length; i++)
                        {
                            using (Bitmap bmp = new Bitmap(dialog.FileNames[i]))
                            {
                                if (i == 0)
                                {
                                    encoder.Setup(bmp.Width, bmp.Height, 5000000, (float)nudFps.Value, 2.0f, onEncode);
                                }
                                encoder.Encode(bmp, nudFps.Value == 0 ? 0.5f : i / (float)nudFps.Value);
                            }
                        }
                    }
            }
        }
Exemplo n.º 7
0
        private void H264Encode(string[] paths, int fps)
        {
            var firstFrame = new Bitmap(paths[0]);

            // AVIに出力するライターを作成(create AVI writer)
            var aviPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test.avi";
            var aviFile = System.IO.File.OpenWrite(aviPath);
            var writer  = new H264Writer(aviFile, firstFrame.Width, firstFrame.Height, fps);

            // H264エンコーダーを作成(create H264 encoder)
            var encoder = new OpenH264Lib.Encoder("openh264-1.7.0-win32.dll");

            // 1フレームエンコードするごとにライターに書き込み(write frame data for each frame encoded)
            OpenH264Lib.Encoder.OnEncodeCallback onEncode = (data, length, frameType) =>
            {
                var keyFrame = (frameType == OpenH264Lib.Encoder.FrameType.IDR) || (frameType == OpenH264Lib.Encoder.FrameType.I);
                writer.AddImage(data, keyFrame);
                Console.WriteLine("Encord {0} bytes, KeyFrame:{1}", length, keyFrame);
            };

            // H264エンコーダーの設定(encoder setup)
            int   bps = 5000 * 1000;       // target bitrate. 5Mbps.
            float keyFrameInterval = 2.0f; // insert key frame interval. unit is second.

            encoder.Setup(firstFrame.Width, firstFrame.Height, bps, fps, keyFrameInterval, onEncode);

            // 1フレームごとにエンコード実施(do encode)
            for (int i = 0; i < paths.Length; i++)
            {
                var bmp = new Bitmap(paths[i]);
                encoder.Encode(bmp);
                bmp.Dispose();
            }

            writer.Close();

            MessageBox.Show(string.Format("{0}\n is created.", aviPath));
        }