Hashem Aboonajmi
Hashem Aboonajmi

Reputation: 13860

create thumbnail from video URL in C#

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?

Upvotes: 5

Views: 11982

Answers (3)

Hi Ten
Hi Ten

Reputation: 93

Just in case, if some one looking for creating multiple thumb image extracting for sprite image creation using ffmpeg or NReco library.

Using Command line --> ffmpeg -i corpfilm.mp4 -s 280x150 -vf fps=1 thumb/img%03d.jpg

Under the hood NReco.VideoConverter executes ffmpeg with System.Diagnostics.Process API; this means that you can run any command that is possible in the command line.

For this concrete command VideoConverter's API equivalent:

var ffmpeg = new NReco.VideoConverter.FFMpegConverter();
ffmpeg.ConvertMedia(
  "corpfilm.mp4", null,
  " thumb/img%03d.jpg", null,
  new ConvertSettings() {
    CustomOutputArgs = " -s 280x150 -vf fps=1 "
  });

Upvotes: 0

DropAndTrap
DropAndTrap

Reputation: 1630

I can see the question is answered But not written clearly, So here is my answer: First download the DLL

ffMpeg.GetVideoThumbnail(inputFile,outputFile, frameTime);
// Summary:
    //     Extract video frame from local video file at specified position
    //
    // Parameters:
    //   inputFile:
    //     path to local video file
    //
    //   outputFile:
    //     path to thumbnail jpeg file
    //
    //   frameTime:
    //     video position (in seconds)

this is how I did in my project

            var thumbNailMovieImage = PHYSICAL_PATH_UPLOAD_FILE + "/" + folder + "/thumb_" + filenameWithoutExtn + ".jpg";
            var ffMpeg = new FFMpegConverter();
            ffMpeg.GetVideoThumbnail(path, thumbNailMovieImage, 1);

Now run this code and check the location your thumbnail file is created

Upvotes: 0

Hashem Aboonajmi
Hashem Aboonajmi

Reputation: 13860

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);

Upvotes: 4

Related Questions