1
\$\begingroup\$

Looking for the best way to speed up or maybe move from FFMpegConverter to another application for converting video to GIF, because it takes much time, will be much appreciate for code review and suggestions to improve converting performance.

   public static byte[] ConvertToGif(string url, string watermarkImagePath, string documentsPath)
        {
            try
            {
                const double height = 340;
                const double width = 600;
                double minimizedHeight, minimizedWidth;

                var converter = new FFMpegConverter();
                var ffProbe = new FFProbe();

                var videoInfo = ffProbe.GetMediaInfo(url);
                var videoStream = videoInfo.Streams.First(x => (x.Width != -1 && x.Height != -1));

                var ratio = (double)videoStream.Height / (double)videoStream.Width;
                if (videoStream.Tags.Any(x => x.Key == "rotate"))
                {
                    ratio = (double)videoStream.Width / (double)videoStream.Height;
                    if (videoStream.Height > videoStream.Width)
                    {
                        minimizedWidth = width;
                        minimizedHeight = minimizedWidth * ratio;
                    }
                    else
                    {
                        minimizedHeight = height;
                        minimizedWidth = minimizedHeight / ratio;
                    }
                }
                else
                {
                    if (videoStream.Height > videoStream.Width)
                    {
                        minimizedHeight = height;
                        minimizedWidth = minimizedHeight / ratio;
                    }
                    else
                    {
                        minimizedWidth = width;
                        minimizedHeight = minimizedWidth * ratio;
                    }
                }
                var identity = Guid.NewGuid().ToString();
                converter.Invoke($"-i {url}" +//input file
                    $" -i {watermarkImagePath}" +//input image
                    " -filter_complex \"overlay = (main_w - overlay_w) / 2:(main_h - overlay_h) / 2\" " +//image place params
                    $"-s {(int)minimizedWidth}x{(int)minimizedHeight} -t 6 -r 8" +//size, time and framerate
                    $" {documentsPath}\\output_{identity}.gif");//output file


                var returnedBytes = File.ReadAllBytes($"{documentsPath}\\output_{identity}.gif");
                File.Delete($"{documentsPath}\\output_{identity}.gif");
                return returnedBytes;
            }
            catch (Exception ex)
            {
                Log.Error($"Video url: {url} " + ex.ToString());
                throw;
            }
        }
\$\endgroup\$
  • 1
    \$\begingroup\$ I think it's not really problem with your code. Converting video to gif is the most consuming operation because decoding video frames, transforming to size and quality and encoding it to gif frames aren't fast at all. You probably can make some improvements by making target image dimensions less and some ajustment with size/ratio/framerate, but it for sure will change final quality and image size. \$\endgroup\$ – Sugar Nov 20 '18 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.