Convert video to GIF using ffMPEG
up vote
1
down vote
favorite
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 suggestiions 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;
}
}
c# performance .net video
add a comment |
up vote
1
down vote
favorite
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 suggestiions 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;
}
}
c# performance .net video
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.
– Sugar
7 hours ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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 suggestiions 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;
}
}
c# performance .net video
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 suggestiions 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;
}
}
c# performance .net video
c# performance .net video
edited 7 hours ago
200_success
127k15148410
127k15148410
asked 7 hours ago
Vitaliy
1183
1183
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.
– Sugar
7 hours ago
add a comment |
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.
– Sugar
7 hours ago
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.
– Sugar
7 hours ago
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.
– Sugar
7 hours ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208083%2fconvert-video-to-gif-using-ffmpeg%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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.
– Sugar
7 hours ago