csharp 使用FFMPEG截取屏幕截图并从视频生成图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 使用FFMPEG截取屏幕截图并从视频生成图像相关的知识,希望对你有一定的参考价值。

/// <summary>
/// file means video file with extension ex: video.mp4
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public static string GenerateThumbFromVideo(string file)
{
	string thumbUploadDirectory = "", directory = "", ApplicationPath = "";
	string thumb = "";
	thumbUploadDirectory = @"";
	if (ApplicationPath != "/")
	{
		thumbUploadDirectory += ApplicationPath + "/video";
		file = ApplicationPath + file;
	}
	else
	{
		thumbUploadDirectory += "/video";
	}

	directory = HttpContext.Current.Server.MapPath(thumbUploadDirectory);

	try
	{
		string ffmpegFilePath = "~" + ApplicationPath + "/video/ffmpeg/ffmpeg.exe";
		FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath(file));
		string filename = Path.GetFileNameWithoutExtension(fi.Name);
		string extension = Path.GetExtension(fi.Name);
		Random random = new Random();
		int rand = random.Next(1, 9999999);
		string newfilename = "";
		if (ApplicationPath != "/")
			newfilename = ApplicationPath + "/video/" + filename.replace() + "___(" + rand.ToString() + ").jpg";
		else
			newfilename = "/video/" + filename.replace() + "___(" + rand.ToString() + ").jpg";

		var processInfo = new ProcessStartInfo();
		processInfo.FileName = "\"" + HttpContext.Current.Server.MapPath(ffmpegFilePath) + "\"";
		processInfo.Arguments = string.Format("-ss {0} -i {1} -f image2 -vframes 1 -y {2}", 5, "\"" + HttpContext.Current.Server.MapPath(file) + "\"", "\"" + HttpContext.Current.Server.MapPath(newfilename) + "\"");
		processInfo.CreateNoWindow = true;
		processInfo.UseShellExecute = false;
		using (var process = new Process())
		{
			process.StartInfo = processInfo;
			process.Start();
			process.WaitForExit();
			thumb = newfilename;
		}
	}
	catch (Exception ex)
	{
		string error = ex.Message;
	}

	return thumb;
}

以上是关于csharp 使用FFMPEG截取屏幕截图并从视频生成图像的主要内容,如果未能解决你的问题,请参考以下文章

如何使用嵌入视频从 WKWebView 截取屏幕截图?

将屏幕截图捕获为图像然后使用 ffmpeg 将图像转换为视频文件的时间应该是啥?

如何利用ffmpeg将一小段视频截取成图片

想要使用ffmpeg来完成如下两个功能: 1.视频截图 2.视频分割功能

7 ffmpeg 截图 水印,动图

使用 Java 代码在 Android 上捕获屏幕截图 [重复]