我想要顯示我網站上列出的視頻的縮略圖,我想從視頻中獲取單個幀(從特定時間)並將它們顯示爲縮略圖。獲取C#中的視頻文件的縮略圖圖像#
我試試這個http://ramcrishna.blogspot.com/2008/09/playing-videos-like-youtube-and.html,但不工作。
使用.NET C#可以嗎?
我想要顯示我網站上列出的視頻的縮略圖,我想從視頻中獲取單個幀(從特定時間)並將它們顯示爲縮略圖。獲取C#中的視頻文件的縮略圖圖像#
我試試這個http://ramcrishna.blogspot.com/2008/09/playing-videos-like-youtube-and.html,但不工作。
使用.NET C#可以嗎?
您可以通過編程方式執行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) '? –
@PaRiMaLRaJ你試過這段代碼嗎?你必須傳遞什麼參數'thumbnail'?我甚至創建了一個擴展名爲.bmp的文件,並通過了(C:\ Users \ Public \ Videos \ Sample Videos \ test1 .bmp)縮略圖,但它給我一個錯誤在代碼的最後一行=「參數無效」。你能幫我嗎。在此先感謝 – CodeEngine
此程序是否會運行在服務器/遠程虛擬機上部署的應用程序。 我打算爲用戶在我的網站上上傳的視頻生成縮略圖。位圖應該即時生成。 由於這將在服務器上運行,這種方法會工作嗎?儘管這在我的本地機器上完美工作。 – aditya
FFmpeg的是可用於在一些位置,以提取視頻幀中合適的工具。 如上所述,或只是利用現有的.NET包裝可以調用ffmpeg.exe(如Video converter for .NET(免費)獲得縮略圖只用一行代碼:
var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
ffMpeg.GetVideoThumbnail(pathToVideoFile, thumbJpegStream,5);
謝謝,最好的解決方案。 –
我該如何使用它?我已經通過nuget添加了「Install-Package NReco.Application.Web」,但不能使用這些代碼行。 – gsiradze
您已經安裝了錯誤的軟件包。正確的是「Install-Package NReco.VideoConverter」(NReco.Application.Web是關於ASP.NET應用程序的NReco框架)。 –
[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;
}
HTTP的可能重複:// stackoverflow.com/questions/155314/how-do-i-get-a-video-thumbnail-in-net –
定義「不工作」。那麼解決方案不適合你嗎? – David