我正在使用以下方法从上传的视频中提取图像并将图像放入文件夹中。
private string GeneratePreviewImageMP4(string FileName, HttpPostedFile file, string ProperPath)
{
string inputfile = System.IO.Path.Combine(Server.MapPath(ProperPath), file.FileName);
string ext = System.IO.Path.GetExtension(FileName);
string thumbpath = AppDomain.CurrentDomain.BaseDirectory + "Reports\\TrainingLibrary\\Videothumbnails\\";
string thumbname = thumbpath + FileName.Replace(ext, "") + ".jpg";
string thumbargs = "-i " + inputfile + " -ss 00:00:25.435 -qscale:v 2 -vframes 1 " + thumbname;
Process thumbproc = new Process();
thumbproc.StartInfo.FileName = "C:\\FFMPEG\\Bin\\ffmpeg.exe";
thumbproc.StartInfo.Arguments = thumbargs;
thumbproc.StartInfo.UseShellExecute = false;
thumbproc.StartInfo.CreateNoWindow = false;
thumbproc.StartInfo.RedirectStandardOutput = false;
try
{
thumbproc.Start();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
thumbproc.WaitForExit();
thumbproc.Close();
return FileName.Replace(ext, ".jpg");
}
问题是,一旦提取图像,我就会被锁定在文件夹之外。没有人对该文件夹拥有管理员权限。我们必须重新启动服务器才能恢复对该文件夹的访问。
这只是在某些时候,大多数时候效果很好,但十分之一会出现问题。
任何人现在为什么会发生这种情况?它是否播放视频以提取图像,但不停止视频?
最佳答案
lock (_aStaticObject)
{
//All the code above
}
警告一句,在 http 请求中使用锁可能会降低性能(您不应弄乱服务器的线程),这就是为什么这种事情属于工作的原因。
关于c# - 从 MP4 视频中提取 JPG 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29104296/