-
-
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; | |
} | |
} | |
} |
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
Great stuff mate. Exactly what I was looking for. Surprised how fast the image generation was. Thanks for sharing. :)