コード例 #1
0
 private unsafe void EncodeImagesToH264(object state)
 {
     try
     {
         while (isEncodingEvent.WaitOne())
         {
             if (decodedFrameQueue.TryDequeue(out queueFrame))
             {
                 if (queueFrame.channels == 2)
                 {
                     Console.WriteLine(queueFrame.channels + "____");
                     h264Encoder.TryEncodeNextPacket(queueFrame);
                 }
                 else
                 {
                     var sourcePixelFormat      = AVPixelFormat.AV_PIX_FMT_BGR24;
                     var destinationPixelFormat = AVPixelFormat.AV_PIX_FMT_YUV420P; //for h.264
                     using (var vfc = new VideoFrameConverter(videoInfo.SourceFrameSize, sourcePixelFormat, videoInfo.DestinationFrameSize, destinationPixelFormat))
                     {
                         var convertedFrame = vfc.Convert(queueFrame);
                         h264Encoder.TryEncodeNextPacket(convertedFrame);
                     }
                 }
             }
         }
     }
     catch (ObjectDisposedException e)
     {
         Console.WriteLine(e.Message);
     }
 }
コード例 #2
0
        private void DecodeAllFramesToImages(object state)
        {
            try
            {
                using (var decoder = new VideoStreamDecoder(url, videoInputType))
                {
                    videoInfo = decoder.GetVideoInfo();
                    var info = decoder.GetContextInfo();
                    info.ToList().ForEach(x => Console.WriteLine($"{x.Key} = {x.Value}"));
                    var sourceSize             = decoder.FrameSize;
                    var sourcePixelFormat      = hwDeviceType == AVHWDeviceType.AV_HWDEVICE_TYPE_NONE ? decoder.PixelFormat : GetHWPixelFormat(hwDeviceType);
                    var destinationSize        = sourceSize;
                    var destinationPixelFormat = AVPixelFormat.AV_PIX_FMT_BGR24;
                    using (var vfc = new VideoFrameConverter(sourceSize, sourcePixelFormat, destinationSize, destinationPixelFormat))
                    {
                        while (decoder.TryDecodeNextFrame(out var frame))
                        {
                            var convertedFrame = frame;
                            Console.WriteLine(convertedFrame.channels + " : frameChannels");
                            if (frame.channels == 2)
                            {
                                Console.WriteLine(convertedFrame.nb_samples + "nb_a");
                                Console.WriteLine(convertedFrame.pkt_size + "frame packet size");
                            }
                            else
                            {
                                Console.WriteLine(convertedFrame.pkt_size + "frame packet size original");
                                convertedFrame = vfc.Convert(frame);
                                Console.WriteLine(convertedFrame.pkt_size + "frame packet size");
                                Bitmap bitmap = new Bitmap(convertedFrame.width, convertedFrame.height, convertedFrame.linesize[0], System.Drawing.Imaging.PixelFormat.Format24bppRgb, (IntPtr)convertedFrame.data[0]);
                                BitmapToImageSource(bitmap);
                            }

                            if (isEncodingThreadRunning)
                            {
                                decodedFrameQueue.Enqueue(convertedFrame);
                            }
                        }
                    }
                }
            }
            catch (ApplicationException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (ObjectDisposedException e)
            {
                Console.WriteLine(e.Message);
            }
        }