

Animated GIF thumbnails from a video
source link: http://dev.goshoom.net/2021/07/animated-gif-thumbnails-from-a-video/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Animated GIF thumbnails from a video
This blog post isn’t about Dynamics AX / Dynamics 365, although it may occasionally be useful for some AX/D365 developers as well.
Let’s say I have a 30 minutes long movie that I want to share on a website and I want to give people an idea about the content of the video. Therefore I want to create a slideshow of screenshots taken from the movie at regular intervals. An animated GIF is an obvious choice.
I spent quite some time trying to figure out how to do it, because I had little idea. After some research, I came to a conclusion that a reasonable approach is extracting screenshots with FFmpeg and then combining them to a GIF file with ImageMagick.
Because installing those console applications and dealing with them directly would be quite cumbersome, I also looked for some wrapper libraries available via NuGet. I chose these:
- MediaToolkit
- Magick.NET, namely Magick.NET-Q8-AnyCPU package
Now let’s build a simple application to show the code. Create a console application for .NET Framework and add those two NuGet packages.
Add using statements for namespaces that we’ll need in a moment:
using System.IO; using ImageMagick; using MediaToolkit; using MediaToolkit.Model; using MediaToolkit.Options;
The structure of your program will be following:
class Program { static void Main(string[] args) { new Program().Run(); } void Run() { string videoFilePath = @"c:\temp\input.mp4"; List<string> thumbnails = ExtractThumbnails(videoFilePath, 10); if (thumbnails?.Any() == true) { string gifFilePath = Path.Combine( Path.GetDirectoryName(videoFilePath), $"{Path.GetFileName(videoFilePath)}.gif"); CombineThumbnails(gifFilePath, thumbnails); // Delete temporary files thumbnails.ForEach(file => File.Delete(file)); } } }
ExtractThumbnails() takes screenshots from the video as JPG files.
CombineThumbnails() then take these files and put them into an animated GIF.
Finally we delete JPG files, because they aren’t needed anymore.
Here is the implementation of ExtractThumbnails(). The key part is the call of engine.GetThumbnail() at the end.
List<string> ExtractThumbnails(string inputFilePath, int numOfPics) { MediaFile inputFile = new MediaFile { Filename = inputFilePath }; List<string> thumbnails = new List<string>(numOfPics); using (var engine = new Engine()) { engine.GetMetadata(inputFile); if (inputFile.Metadata == null) { throw new InvalidOperationException("Invalid file"); } int duration = (int)inputFile.Metadata.Duration.TotalSeconds; int picDistance = duration / (numOfPics + 1); for (int i = 1; i <= numOfPics; i++) { var options = new ConversionOptions { Seek = TimeSpan.FromSeconds(i * picDistance) }; string outputFilePath = $"{inputFilePath}{i}.jpg"; var outputFile = new MediaFile { Filename = outputFilePath }; engine.GetThumbnail(inputFile, outputFile, options); thumbnails.Add(outputFilePath); } } return thumbnails; }
The last missing piece is the method creating the animated GIF. It’s straightforward, thanks to Magick.NET:
void CombineThumbnails(string gifFilePath, List<string> thumbnails) { using (var collection = new MagickImageCollection()) { for (int i = 0; i < thumbnails.Count; i++) { collection.Add(thumbnails[i]); collection[i].AnimationDelay = 70; collection[i].Resize(800, 600); } collection.Write(gifFilePath); } }
Recommend
-
121
Easy to use online animated GIF maker and image editor. Upload and edit GIF images for free. Create, crop, resize, reverse, optimize and split animated GIFs, cut and resize videos, WebP and APNG animations.
-
6
John Fremlin's blog: Better video thumbnails by facing the factsWaiting for updates: connectedPosted 2010-01-14 17:23:00 GMTToday, I made a nasty little prototype pr...
-
16
Animated GIFs are an image type that contains multiple images with slight differences. These are then played back kind of like a cartoon is. You could even think of it as a flip-book with a stick man that is slightly different on each page. W...
-
34
To try to export an animation from a JavaFX app to an animated GIF, I started on this thread in StackOverflow, then
-
3
-
5
How to Convert a Video to GIF on Linux By Deepesh Sharma Published 8 hours ago In addition to online tools,...
-
3
Getting all the video thumbnails from a YouTube video Wednesday, January 11th, 2023 at 9:16 pm I just wrote myself a
-
12
Find & Share on GIPHY ...
-
7
OS X Screencast to animated GIF This gist shows how to create a GIF screencast using only free OS X tools: QuickTime, ffmpeg, and gifsicle.
-
3
Calculating the Duration of an Animated GIF file » Stuart’s MATLAB Videos I have a function that returns the duration of a...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK