Created
September 26, 2013 21:59
-
-
Save kad1r/6721178 to your computer and use it in GitHub Desktop.
Create a thumbnail image from video with ffmpeg with .net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
@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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?