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));
                }
            }
        }
Пример #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);
                    }
                }
            }
        }