-
-
Save kad1r/6721178 to your computer and use it in GitHub Desktop.
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; | |
} | |
} | |
} |
Great stuff mate. Exactly what I was looking for. Surprised how fast the image generation was. Thanks for sharing. :)
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?
@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) + "\"");
NOT WORKING
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:
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.