private void GenerateVideo(ISampleSource source)
        {
            Console.WriteLine("Initializing video writer...");
            using (var writer = new VideoWriter(options.OutputFile.FullName))
            {
                writer.Width           = 1920;
                writer.Height          = 1080;
                writer.Fps             = options.Fps;
                writer.AudioSampleRate = source.WaveFormat.SampleRate;
                writer.Open();

                double totalFrames = FrameMath.CalculateTotalFrames(source, options);

                Console.WriteLine("Initializing renderer...");
                using (var renderer = new GLRenderer(false))
                {
                    var wallpaper = Image.FromFile(options.WallpaperFile.FullName) as Bitmap;
                    wallpaperTexture = renderer.LoadTexture(wallpaper);
                    effectChain.Initialize(source.ToMono(), options);

                    Console.WriteLine("Generating video...");
                    float[] sampleBuffer = new float[writer.AudioSamplesPerFrame];

                    var frameNumber = 0;
                    while (true)
                    {
                        if (writer.WriteVideo)
                        {
                            renderer.Clear();
                            RenderFrame(renderer, new Frame(frameNumber, TimeSpan.FromSeconds(frameNumber / (double)options.Fps)));

                            var frame = renderer.Snapshot();
                            writer.WriteVideoFrame(frame);

                            if (frameNumber % 60 == 0)
                            {
                                ProgressHandler?.Invoke(Math.Round(frameNumber / totalFrames * 100.0, 2));
                            }

                            frameNumber++;
                        }
                        else
                        {
                            var read = source.Read(sampleBuffer, 0, sampleBuffer.Length);
                            if (read > 0)
                            {
                                writer.WriteAudioFrame(sampleBuffer);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    ProgressHandler?.Invoke(Math.Round(frameNumber / totalFrames * 100.0, 2));
                }
            }
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            var success = FFmpegLoader.Load(FFmpegPath);

            if (!success)
            {
                Console.WriteLine("Could not load FFmpeg");
                return;
            }

            Console.WriteLine($"Loaded FFmpeg v{FFmpegLoader.FFmpegVersion}");

            var bitmap = (Bitmap)Image.FromFile(ImagePath);

            var audio = CodecFactory.Instance.GetCodec(AudioPath)
                        .ToSampleSource();


            using (var writer = new VideoWriter(OutputPath))
            {
                writer.Open();

                float[] audioData = new float[writer.AudioSamplesPerFrame];

                while (true)
                {
                    if (writer.WriteVideo)
                    {
                        // Write a video frame
                        writer.WriteVideoFrame(bitmap);
                    }
                    else
                    {
                        // Write an audio frame
                        int read = audio.Read(audioData, 0, audioData.Length);
                        if (read <= 0)
                        {
                            break;
                        }

                        writer.WriteAudioFrame(audioData);
                    }
                }
            }
        }
        private void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            WriteableBitmap depthWBitmap = null;
            WriteableBitmap colorWBitmap = null;
            Bitmap          colorBitmap  = null;
            Bitmap          depthBitmap  = null;

            MultiSourceFrame frame = e.FrameReference.AcquireFrame();

            if (frame != null)
            {
                using (DepthFrame depthFrame = frame.DepthFrameReference.AcquireFrame())
                {
                    if (depthFrame != null)
                    {
                        _frameParser.ParseToBitmaps(depthFrame, out depthBitmap, out depthWBitmap);

                        if (_selectedCamera == ECameraType.Depth)
                        {
                            if (_selectedCamera == ECameraType.Depth)
                            {
                                bodyViewer.UpdateFrameCounter();
                                bodyViewer.KinectImage = depthWBitmap;
                            }
                        }
                    }
                }

                if (_selectedCamera == ECameraType.Color)
                {
                    using (ColorFrame colorFrame = frame.ColorFrameReference.AcquireFrame())
                    {
                        if (colorFrame != null)
                        {
                            _frameParser.ParseToBitmaps(colorFrame, out colorBitmap, out colorWBitmap);

                            if (_selectedCamera == ECameraType.Color)
                            {
                                bodyViewer.UpdateFrameCounter();
                                bodyViewer.KinectImage = colorWBitmap;
                            }
                        }
                    }
                }

                using (BodyFrame bodyFrame = frame.BodyFrameReference.AcquireFrame())
                {
                    if (bodyFrame != null)
                    {
                        BodyFrameWrapper bodyFrameWrapper = new BodyFrameWrapper(bodyFrame);

                        if (bodyFrameWrapper.TrackedBodies.Any())
                        {
                            bodyViewer.RenderBodies(bodyFrameWrapper.TrackedBodies, _selectedCamera);

                            if (_recordingStarted)
                            {
                                _recordedBodyFrames.Add(bodyFrameWrapper);
                                if (depthWBitmap != null)
                                {
                                    _depthVideoWriter.WriteVideoFrame(depthBitmap);

                                    //writing colorframes does work but should be deactivated by default
                                    //_colorVideoWriter.WriteVideoFrame(colorBitmap);
                                }
                                else
                                {
                                    Logger.Instance.LogMessage("DepthVideoFrame missing at: " + _recordedBodyFrames.Count.ToString());
                                }
                            }
                        }
                    }
                    else
                    {
                        bodyViewer.DeleteUntrackedBodies(null);
                    }
                }
            }
        }