因为要发布文章,所以想到录一段视频。但找了好些时间,专业一些的都要注册。免费版本有时间限制,而且还有水印,所以想用其他办法,网上找了下,找到一个折衷的办法:
首先,你的系统得是WIN10,然后按WIN+G快捷键,就会出来游戏录制界面,按中间的圆点就可以开始录制了,注意桌面不支持。录完后按停止键。录好的视频保存在“\视频\捕获”文件夹下。
录好的视频是mp4格式,我们还需要将其转为gif格式,才能嵌到我们的文章里。这里要用到一个类(引用自C#使用FFmpeg 将视频格式转换成Gif图片示例 - 天马3798 - 博客园),网上已经写好了,我们拿来用一下(其实就是将DOS命令写到文件里面封装了一下,mysql数据库服务器也可以用这种方法来做)。下面是用C#写的代码,使用之前先下载ffmpeg,然后设置好ffmpeg路径和源的路径,以及保存gif的路径,分辨率看自己的电脑改下:
using System;
using System.Diagnostics;
namespace togif
{
class Program
{
public static void Main(string[] args)
{
// TODO: Implement Functionality Here
GifDemo gif=new GifDemo();
gif.ConvertVideoToGif ();
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
class GifDemo
{
//文件名不支持空格
public static string ffmpegtool = @"C:\Users\Administrator\Documents\SharpDevelop_Projects\MP4toGIF\togif\bin\Debug\ffmpeg.exe";
public static string imgFile = @"C:\Users\Administrator\Documents\SharpDevelop_Projects\MP4toGIF\togif\bin\Debug\mygif.gif";
public static string sourceFile = @"C:\Users\Administrator\Documents\SharpDevelop_Projects\MP4toGIF\togif\bin\Debug\test.mp4";
public void ConvertVideoToGif()
{
Process p = new Process();//建立外部调用进程
//要调用外部程序的绝对路径
p.StartInfo.FileName = ffmpegtool;
//转化gif动画
string strArg = "-i " + sourceFile + " -y -s 800x450 -f gif " + imgFile;
//string strArg = " -i " + sourceFile + " -y -f gif -vframes 50 " + imgFile;
// string strArg = " -i " + sourceFile + " -y -f gif -ss 0:20 -dframes 10 -frames 50 " + imgFile;
p.StartInfo.Arguments = strArg;
p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...)
p.StartInfo.CreateNoWindow = false;//不创建进程窗口
p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
p.Start();//启动线程
p.BeginErrorReadLine();//开始异步读取
p.WaitForExit();//阻塞等待进程结束
p.Close();//关闭进程
p.Dispose();//释放资源
}
private void Output(object sendProcess, DataReceivedEventArgs output)
{
if (!String.IsNullOrEmpty(output.Data))
{
//处理方法...
Console.WriteLine(output.Data);
}
}
}
}
生成exe文件后会自动转换,等文件执行完,gif文件就转好了。
添加图片就可以看到动画了。