This article
shows how to easily create thumbnails from a video.
To do this, the
free FFMpeg package is installed in the C# project and then the ffmpeg .exe
program is executed as a process.
The application
takes place in Asp Core . Net5, MVC and can run on Windows servers or Linux as
a cross platform.
Create files from the video
The video files
must be located on the web server. Here .mp4 files in the user directory
Code Asp Core
.Net5 MVC in _classes public shared
using System.Diagnostics; //ProcessStartInfo
public static class Video_Methods
{
public static void Video_create_Thumbnail(string Video_Path, string output_Image_Path, int new_Width, string sPath_FFMpeg)
{
//---------------<
Video_create_Thumbnail() >---------------
string sPath_FFMpegDir = @"D:\xx\bin\FFMpeg\";
string sInputFile = @"D:\xx\Videos\Video_2618_0.mp4";
string sOutputFile = @"D:\xx\Images\thumb_2618_0.jpg";
var startInfo = new ProcessStartInfo
{
FileName = sPath_FFMpegDir + $"ffmpeg.exe",
Arguments = $"-i " + sInputFile + " -an -vf scale=200x112
" +
sOutputFile + "
-ss 00:00:00 -vframes 1",
CreateNoWindow=true,
UseShellExecute=false,
WorkingDirectory=sPath_FFMpegDir
};
using (var process=new Process { StartInfo=startInfo})
{
process.Start();
process.WaitForExit();
}
//---------------</
Video_create_Thumbnail() >---------------
}
}
|
The command line parameters:
- For video files,
- -ss 5 means “seek to the position five seconds into the media file”.
- -an prevents audio streams from being handled in any way
- -i specifies the input file
- -vframes 1 gets one frame from the input file
- -s specifies the output size
- -y to confirm overwriting an existing file
- The last parameter is the output file; the extension of the thumbnail file tells FFmpeg that we want the file in PNG format.
Alternativ System.Diagnostics.Process p = new System.Diagnostics.Process ( );
p.StartInfo.FileName = "ffmpeg.exe";
p.StartInfo.Arguments = "-i input.avi -b 64k output.flv";
p.Start ( );
Installing FFMpeg
In both processes
you have to download FFMpeg and use it as dll and exe in asp core. Net5 or MVC:
Latest FFMpeg Files
Download and install in Server
https://github.com/BtbN/FFmpeg-Builds/releases
Save FFMpeg to Asp Core
I put the FFMpeg
files in the output /bin path next to Debug und Public