create thumbnail from video URL in C#

Hashem Aboonajmi picture Hashem Aboonajmi · Jan 30, 2015 · Viewed 9.3k times · Source

I want to generate thumbnail from a video URL in C#. I have searched a lot to find a neat way but no success. I have used Nreco and MediaToolKit but none of them extract thumbnail image. using ffmpeg also has mumbo jumbos which didn't worked!

using NReco:

var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
string thumbnailJPEGpath = "http://localhost:81882/content/hashem.jpeg";
ffMpeg.GetVideoThumbnail(videoUrl,thumbnailJPEGpath);

using ffmpeg:

try
        {
            System.Diagnostics.Process ffmpeg;

            string video;
            string thumb;

            video = Server.MapPath("~/Content/Movies/bye.mp4");
            thumb = Server.MapPath("~/Content/frame.jpg");

            ffmpeg = new System.Diagnostics.Process();

            ffmpeg.StartInfo.Arguments = " -i " + video + " -ss 00:00:07 -vframes 1 -f image2 -vcodec mjpeg " + thumb;
            ffmpeg.StartInfo.FileName = Server.MapPath("~/Content/ffmpeg.exe");
            ffmpeg.Start();
            ffmpeg.WaitForExit();
            ffmpeg.Close();
        }
        catch (Exception ex)
        {
            Response.Write("Error: " + ex.Message);
        }

to consider video files are not local and I have only a direct link to the file: e.g.:http://phytonord.com/Film-Series/hana/26.mp4

does anyone has any solution? any sample code that works?

Answer

Hashem Aboonajmi picture Hashem Aboonajmi · Feb 5, 2015

Using NReco to Create Video Thumbnail:

I found my problem:

1: I have not used Server.MapPath. I had just entered relativePath.

2: it is not necessary the Video file be local it can be hosted someWhere else. NReco just download the desired portion of video then extract the Thumbnail. your Video file should be in localHost directory of your local server. I mean if your site is under development and video file is in your local Computer folder it WON'T Work because NReco requires byte range support in HTTP response Header file.

Unacceptable Link: "http://localhost:81882/content/AVSEQ01.mp4"

so for my local test, I have moved my video file to Local IIS directory : ‪C:\inetpub\wwwroot\AVSEQ01.mp4

//sample remote video file
//string videoUrl = "http://phytonord.com/Film-Series/peik%20sahar/1.mp4";

 //local video file
string localVideoFile = "http://localhost/AVSEQ01.mp4"
var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
string thumbnailJPEGpath = Server.MapPath("~/Content/videoThumb.jpg");
ffMpeg.GetVideoThumbnail(videoUrl, thumbnailJPEGpath);