.Net 获取视频封面

做项目过程中有需求需要在上传视频的时候获取视频的封面,可以通过ffmpeg这个exe文件给上传的视频抽帧。具体代码如下:

string videoPath = Request.Form["videoPath"];
string uploadPath = AppSettingUtil.AppSettings["UploadFilePath"];
string videoFilePath = Server.MapPath("~/" + uploadPath + videoPath);
string ffmpegPath = Server.MapPath(AppSettingUtil.AppSettings["ffmpeg"]);
if (!File.Exists(ffmpegPath)) return new AjaxResult("ffmpeg不存在");
string imagePath = DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.Ticks + ".png";
string imageSavePath = Server.MapPath("~/" + uploadPath + "" + imagePath);
string cmd = string.Format("-i {0} -ss 00:00:01 -vframes 1 {1}", videoFilePath, imageSavePath);
ProcessStartInfo startInfo = new ProcessStartInfo(ffmpegPath)
{
    WindowStyle = ProcessWindowStyle.Hidden,
    Arguments = cmd
};
try
{
    Process proc = Process.Start(startInfo);
    if (proc != null) proc.WaitForExit();
}
catch (Exception ex)
{
    return new AjaxResult(ex.Message);
}
if (File.Exists(imageSavePath))
    return new AjaxResult
    {
        IsSuccess = true,
        Data = imagePath
    };
return new AjaxResult("视频截图失败");

参考地址
ffmpeg 下载地址
Create a thumbnail image every X seconds of the video – FFmpeg

猜你喜欢

转载自www.cnblogs.com/blogcore/p/12459700.html