Ejemplo n.º 1
0
        private byte[] Encode(string path = "temp", int scale = 1)
        {
            try
            {
                if (!Directory.Exists($"{path}/temp"))
                {
                    Directory.CreateDirectory($"{path}/temp");
                }
                else
                {
                    Cleanup();
                }

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                for (int i = 0; i < Flipnote.FrameCount; i++)
                {
                    try
                    {
                        PPMRenderer.GetFrameImage(Flipnote.Frames[i]).SaveAsPng($"{path}/temp/frame_{i}.png");
                    }
                    catch (Exception e)
                    {
                    }
                }

                var frames = Directory.EnumerateFiles($"{path}/temp").ToArray();
                Utils.NumericalSort(frames);

                File.WriteAllBytes($"{path}/temp/audio.wav", Flipnote.Audio.GetWavBGM(Flipnote));

                var a = FFMpegArguments
                        .FromDemuxConcatInput(frames, options => options
                                              .WithFramerate(Flipnote.Framerate))
                        .AddFileInput($"{path}/temp/audio.wav", false)
                        .OutputToFile($"{path}/{Flipnote.CurrentFilename}.mp4", true, o =>
                {
                    o.Resize(256 * scale, 192 * scale)
                    .WithVideoCodec(VideoCodec.LibX264)
                    .ForcePixelFormat("yuv420p")
                    .ForceFormat("mp4");
                });

                a.ProcessSynchronously();

                var mp4 = File.ReadAllBytes($"{path}/{Flipnote.CurrentFilename}.mp4");

                Cleanup();

                return(mp4);
            }
            catch (Exception e)
            {
                Cleanup();
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return(null);
            }
        }