Skip to content

Instantly share code, notes, and snippets.

@kad1r
Created September 26, 2013 21:59
Show Gist options
  • Save kad1r/6721178 to your computer and use it in GitHub Desktop.
Save kad1r/6721178 to your computer and use it in GitHub Desktop.
Create a thumbnail image from video with ffmpeg with .net
using System;
using System.Diagnostics;
using System.IO;
using System.Web;
namespace WebApp.HelperClass
{
public class CreateThumbFromVideo
{
/// <summary>
/// created by ~kad1r
/// send files without mappath
/// like "/video/videolink1.avi"
/// function puts mappath itself
/// download ffmpeg.exe - http://www.speedyshare.com/mRbUN/ffmpeg.rar
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public static string generateThumb(string file)
{
string thumb = "";
try
{
FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath(file));
string filename = Path.GetFileNameWithoutExtension(fi.Name);
Random random = new Random();
int rand = random.Next(1, 9999999);
string newfilename = "/video/" + filename + "___(" + rand.ToString() + ").jpg";
var processInfo = new ProcessStartInfo();
processInfo.FileName = "\"" + HttpContext.Current.Server.MapPath("/video/ffmpeg.exe") + "\"";
processInfo.Arguments = string.Format("-ss {0} -i {1} -f image2 -vframes 1 -y {2}", 5, "\"" + HttpContext.Current.Server.MapPath(file) + "\"", "\"" + HttpContext.Current.Server.MapPath(newfilename) + "\"");
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
using (var process = new Process())
{
process.StartInfo = processInfo;
process.Start();
process.WaitForExit();
thumb = newfilename;
}
}
catch (Exception ex)
{
string error = ex.Message;
}
return thumb;
}
}
}
@GleamTech
Copy link

Full disclosure: I work for GleamTech

Very nice however using ffmpeg.exe via Process class is cumbersome and error prone. It's also not flexible, for example if you have a video stream and not a file on disk, it will be very hard to pass that stream to ffmpeg.exe. What if you could do it as easy as follows:

using (var videoThumbnailer = new VideoThumbnailer(@"C:\Video.mp4"))
using (var thumbnail = videoThumbnailer.GenerateThumbnail(500))
    thumbnail.Save(@"C:\Thumbnail1.jpg", ImageFormat.Jpeg);

Yes, this is possible with VideoUltimate library from GleamTech. You can generate thumbnails for any video file format on the planet and it's fully ASP.NET compatible. GenerateThumbnail method smartly generates a meaningful thumbnail for the video by seeking to a sensible time position and avoiding blank frames. So it's fully automatic and provides you good thumbnails out of the box. First parameter (500 pixels in above sample) is the maximum width or height of the thumbnail, aspect ratio is preserved. It's also possible to overlay the duration of the video on the bottom-right corner by passing true to the second parameter, similar to how YouTube does.

@UdaraAlwis
Copy link

Great stuff mate. Exactly what I was looking for. Surprised how fast the image generation was. Thanks for sharing. :)

@mohammad983
Copy link

Thanks for sharing your useful code. I am a beginner. I want to get 10 thumbnails from a video. could you help me how can I do that?

@kad1r
Copy link
Author

kad1r commented Nov 26, 2020

@mohammad983,
You can get the video duration by using this simple code ffprobe -i input.file -show_format | grep duration. This will give you the frame in seconds and you need to divide it to 60 to get in minutes. But this convertion is optional. After you get the video duration if you want to get 10 thumbnails divide it to 10 and with foreach loop you can use my code.
In my code as you can see there is an option which is 5. That means get the thumbnail after 5 seconds.
string.Format("-ss {0} -i {1} -f image2 -vframes 1 -y {2}", **5**, "\"" + HttpContext.Current.Server.MapPath(file) + "\"", "\"" + HttpContext.Current.Server.MapPath(newfilename) + "\"");

@r-confianza
Copy link

NOT WORKING

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment