Captura系列教程--C#嵌入

Author:duanyduan CreateTime:2022-04-27

Captura 介绍

一款开源,免费,无广告的视频录制,截屏等强大的软件。

命令行程序操作

/// <summary>
/// 命令行操作帮助类
/// </summary>
public class CMDRunHelper
{
    static Process pro;			//为了适配通过发送'q'命令终止录屏
     public static void RunCmdOnce(string batPath,string command)
    {
        try
            {
                using (pro = new Process())
                {
                    pro.StartInfo.WorkingDirectory = path;
                    pro.StartInfo.FileName = "cmd.exe";
                    pro.StartInfo.Arguments = "/k " + command;
                    pro.StartInfo.UseShellExecute = false;
                    pro.StartInfo.RedirectStandardInput = true;
                    pro.StartInfo.RedirectStandardOutput = true;
                    pro.StartInfo.RedirectStandardError = true;
                    pro.StartInfo.CreateNoWindow = true;
                    pro.StartInfo.Verb = "runas";
                    pro.Start();
                   pro.WaitForExitAsync();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
    }
     public static bool RunCmd(string command)
    {
        bool result = false;
        try
        {
            using (Process p = new Process())
            {
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
                p.StandardInput.WriteLine(command.TrimEnd('&') + "&exit");
                p.StandardInput.AutoFlush = true;
                var output = p.StandardOutput.ReadToEnd();
                var error = p.StandardError.ReadToEnd();
                if (!string.IsNullOrWhiteSpace(error))
                {
                    return false;
                }
                p.WaitForExit();
                result = true;
            }
        }
        catch (Exception ex)
        {

        }
        return result;
    }
     public static void InputCommand(string command)
    {
        pro.StandardInput.WriteLine("command);//向CMD窗口发送输入命令
        pro.Close();
    }
    
}

Captura操作

需要准备以下文件(可参考上一节内容)

captura-cli.exe

ffmpeg.exe

public class CapturaHelper
 {
  private static string _basePath = $@"E:\Luncher\viedo";
  private static string _baseSavePath = @$"D:\Video\{DateTime.Now.ToString("yyyy-MM-dd")}";

 /// <summary>
    /// 开始录屏
    /// </summary>
    public static void StartCaptura()
    {
        //ShutdownCaptura();
        _fileName = Guid.NewGuid().ToString().Substring(0, 4);
        if (!Directory.Exists(_baseSavePath))
        { 
            Directory.CreateDirectory(_baseSavePath);
        }

        string baseFile = $"{Guid.NewGuid().ToString()}.mp4";
        string command = @$"captura-cli start  --cursor --keys --clicks  --delay=1000 --source screen:0 -f {_baseSavePath}\{baseFile} -y";
        CMDTools.CMDRunHelper.RunCmdOnce(_basePath,command);

    }
     /// <summary>
    /// 关闭录屏
    /// </summary>
    public static void ShutdownCaptura()
    {
        CMDTools.CMDRunHelper.InputCommand("q");
        CMDTools.CMDRunHelper.RunCmd("taskkill /f /t /im captura-cli.exe");
    }
     
 }

录制结果展示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值