我想在我的网站上为视频显示缩略图,我希望能够从视频中获取单帧(特定时间点)并将它们显示为缩略图。
我已经尝试了这个http://ramcrishna.blogspot.com/2008/09/playing-videos-like-youtube-and.html但是无法工作。
使用.NET C#是否可以实现呢?
我想在我的网站上为视频显示缩略图,我希望能够从视频中获取单帧(特定时间点)并将它们显示为缩略图。
我已经尝试了这个http://ramcrishna.blogspot.com/2008/09/playing-videos-like-youtube-and.html但是无法工作。
使用.NET C#是否可以实现呢?
FFMpeg是一个可以用来在某个位置提取视频帧的好工具。 你可以像上面提到的那样调用ffmpeg.exe,或者只需使用现有的.NET包装器(例如.NET视频转换器(它是免费的)),只需要一行代码即可获得缩略图:
var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
ffMpeg.GetVideoThumbnail(pathToVideoFile, thumbJpegStream,5);
您可以编写代码来执行FFmpeg,生成缩略图文件。然后打开该文件,以便根据需要进行使用。
这是一些示例代码:
public static Bitmap GetThumbnail(string video, string thumbnail)
{
var cmd = "ffmpeg -itsoffset -1 -i " + '"' + video + '"' + " -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320x240 " + '"' + thumbnail + '"';
var startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = "/C " + cmd
};
var process = new Process
{
StartInfo = startInfo
};
process.Start();
process.WaitForExit(5000);
return LoadImage(thumbnail);
}
static Bitmap LoadImage(string path)
{
var ms = new MemoryStream(File.ReadAllBytes(path));
return (Bitmap)Image.FromStream(ms);
}
WaitForExit(5000)
? - Parimal RajPath.GetFullPath()
运行这两个参数,然后将每个规范路径与已知安全基本路径进行比较。如果您想拒绝UNC路径(例如\\evilserver\foo
),则还应使用Uri.IsUnc
。 - Polynomial对于不想在商业软件中使用FFMpeg的人,我有一个旧解决方案在这里:
ShellFile shellFile = ShellFile.FromFilePath(VideoFileName);
Bitmap bm = shellFile.Thumbnail.Bitmap;
然后您将获得一个可以用于绘图的位图对象。如果您想要一个文件,只需执行以下操作:
bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
如果您想在Xaml绑定中使用一个BitmapImage,请将Bitmap转换为BitmapImage。以下是一个例子:
如果您想在Xaml绑定中使用一个BitmapImage,请将Bitmap转换为BitmapImage。以下是一个例子:
public static BitmapImage ConvertBitmapToBitmapImage(Bitmap bitmap)
{
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
BitmapImage image = new BitmapImage();
image.BeginInit();
ms.Seek(0, SeekOrigin.Begin);
image.StreamSource = ms;
image.EndInit();
return image;
}
Xabe.FFmpeg - 免费(仅限非商业用途),开源且跨平台的库。提供流畅的 FFmpeg API。在 Xabe.F 中生成视频缩略图。
string output = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + FileExtensions.Png);
IConversionResult result = await Conversion.Snapshot(Resources.Mp4WithAudio, output, TimeSpan.FromSeconds(0))
.Start();
需要像其他答案中一样使用FFmpeg可执行文件,但你可以通过下载来获取它
FFmpeg.GetLatestVersion();
完整文档在此处 - Xabe.FFmpeg 文档
[HttpPost]
[Route("UploadImages")]
public HttpResponseMessage Post()
{
HttpResponseMessage response = new HttpResponseMessage();
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
var docfiles = new List<string>();
foreach (string file in httpRequest.Files)
{
var postedFile = httpRequest.Files[file];
var filePath1 = HttpContext.Current.Server.MapPath("~/ImgFolder/" + postedFile.FileName);
Stream strm = postedFile.InputStream;
CreateThumbnail(strm, postedFile.FileName);
Compressimage(strm, filePath1, postedFile.FileName);
}
response = Request.CreateResponse(HttpStatusCode.Created, docfiles);
}
else
{
response = Request.CreateResponse(HttpStatusCode.BadRequest);
}
return response;
}
public static void **CreateThumbnail**(Stream sourcePath, string filename)
{
Image image = Image.FromStream(sourcePath);
Image thumb = image.GetThumbnailImage(120, 120, () => false, IntPtr.Zero);
var filePath1 = HttpContext.Current.Server.MapPath("~/Thumbnail/" + filename);
thumb.Save(filePath1 + filename);
}
public static void Compressimage(Stream sourcePath, string targetPath, String filename)
{
try
{
using (var image = Image.FromStream(sourcePath))
{
float maxHeight = 900.0f;
float maxWidth = 900.0f;
int newWidth;
int newHeight;
string extension;
Bitmap originalBMP = new Bitmap(sourcePath);
int originalWidth = originalBMP.Width;
int originalHeight = originalBMP.Height;
if (originalWidth > maxWidth || originalHeight > maxHeight)
{
// To preserve the aspect ratio
float ratioX = (float)maxWidth / (float)originalWidth;
float ratioY = (float)maxHeight / (float)originalHeight;
float ratio = Math.Min(ratioX, ratioY);
newWidth = (int)(originalWidth * ratio);
newHeight = (int)(originalHeight * ratio);
}
else
{
newWidth = (int)originalWidth;
newHeight = (int)originalHeight;
}
Bitmap bitMAP1 = new Bitmap(originalBMP, newWidth, newHeight);
Graphics imgGraph = Graphics.FromImage(bitMAP1);
extension = Path.GetExtension(targetPath);
if (extension == ".png" || extension == ".gif")
{
imgGraph.SmoothingMode = SmoothingMode.AntiAlias;
imgGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
imgGraph.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
bitMAP1.Save(targetPath, image.RawFormat);
bitMAP1.Dispose();
imgGraph.Dispose();
originalBMP.Dispose();
}
else if (extension == ".jpg")
{
imgGraph.SmoothingMode = SmoothingMode.AntiAlias;
imgGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
imgGraph.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
Encoder myEncoder = Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
bitMAP1.Save(targetPath, jpgEncoder, myEncoderParameters);
bitMAP1.Dispose();
imgGraph.Dispose();
originalBMP.Dispose();
}
}
}
catch (Exception)
{
throw;
}
}
public static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}