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 SetScreen(Screen screen)
 {
     this.screen = screen;
     bmp         = new Bitmap(screen.Bounds.Width, screen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
     graphics    = Graphics.FromImage(bmp);
     encoder.Setup(screen.Bounds.Width, screen.Bounds.Height, bps * 1000, fps, keyFrameInterval, OnEncode);
 }
Exemplo n.º 4
0
        private void OnImageCallback3Recive(object sender, ImageEventArgs e)
        {
            try
            {
                logger.Debug($"device: {m_SelectID} OnImageCallback3Recive");
                System.Drawing.Imaging.PixelFormat format = System.Drawing.Imaging.PixelFormat.Format24bppRgb;// .Format32bppRgb; .Format4bppIndexed
                int bitsPerPixel  = ((int)format & 0xff00) >> 8;
                int bytesPerPixel = (bitsPerPixel + 7) / 8;
                int stride        = ((Convert.ToInt16(e.dwWidth) * bytesPerPixel + 3) / 4) * 4;

                using (Bitmap oBitmap = new Bitmap(Convert.ToInt32(e.dwWidth), Convert.ToInt32(System.Math.Abs(e.dwHeight)), stride, format, e.Buffer))//.Format32bppRgb
                {
                    if (_h264Encoder == null)
                    {
                        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)
                            {
                                if (data == null)
                                {
                                    logger.Warn($"onEncode get null data");
                                }
                                try
                                {
                                    ReceivedYUVFrame(m_SelectID, (uint)_stopwatch.ElapsedMilliseconds, data, keyFrame);
                                }
                                catch (Exception ex)
                                {
                                    logger.Error(ex.ToString());
                                }
                            }
                        };

                        _h264Encoder = new OpenH264Lib.Encoder(@"lib\openh264-1.8.0-win32.dll");
                        _h264Encoder.Setup(640, 480, 5000000, 10, 2.0f, onEncode);
                    }
                    var resizedBitmap = Process(oBitmap, oBitmap.Width, oBitmap.Height, 640, 480);
                    this.HasVideo = true;
                    _h264Encoder.Encode(resizedBitmap, _stopwatch.ElapsedMilliseconds);
                }
            }
            catch (Exception ex)
            {
                this.HasVideo = false;
                logger.Error(ex.ToString());
            }
        }
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));
        }
Exemplo n.º 8
0
        public void setup(int width, int height, int bps, float fps, float keyFrameInterval)
        {
            /*
             *          var exe_path = System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\');
             *          // H264エンコーダーを作成
             *          encoder = new OpenH264Lib.Encoder(exe_path + "\\openh264-1.7.0-win32.dll");
             */

            encoder = new OpenH264Lib.Encoder("openh264-1.7.0-win32.dll");
            //encoder = new OpenH264Lib.Encoder("C:\\Users\\ryo\\Desktop\\RDTopSvr0310_Debug\\openh264-1.7.0-win32.dll");
            //encoder = new OpenH264Lib.Encoder("openh264-1.7.0-win32-WRONG.dll");

            // 1フレームエンコードするごとにライターに書き込み
            OpenH264Lib.Encoder.OnEncodeCallback onEncode = (data, length, frameType) => { };

            OpenH264Lib.Encoder.OnEncodeCallback onEncodeProxy = (data, length, frameType) =>
            {
                onEncode(data, length, frameType);
            };


            onEncode = (data, length, frameType) =>
            {
                lock (this)
                {
                    var keyFrame = (frameType == OpenH264Lib.Encoder.FrameType.IDR) || (frameType == OpenH264Lib.Encoder.FrameType.I);
                    Console.WriteLine("Encord {0} bytes, data.Length: {1} bytes, KeyFrame:{2} timestamp:{3} " + frameType.ToString(), length, data.Length, keyFrame, timestamp);

                    encodedDataGenerated(data);
                    timestamp++;
                }
            };

            // H264エンコーダーの設定
            //encoder.Setup(width, height, 5000000, fps, 10.0f, onEncode);
            encoder.Setup(width, height, bps, fps, keyFrameInterval, onEncode);
        }