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









share|improve this question
























  • 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















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









share|improve this question
























  • 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













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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)