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;
}
}