本文通过往cmd控制台输入命令的方式实现视频的录制,具体的命令可以参考文章https://blog.csdn.net/zhoubotong2012/article/details/79338093的前半部分
测试环境:
window 7 64位
vistual studio 2012
本文测试的Demo下载:https://gitee.com/zxy15914507674/shared_resource_name/blob/master/ffmpegDemo.rar
步骤:
1 下载ffmpeg.exe工具(一定要看准位数,本机测试下载的是64位的)
下载地址:http://www.ffmpeg.org/download.html
 
然后打开新的界面,如下图:
 
下载完毕后,找到里面的ffmpeg.exe工具
2 打开cmd窗口,并切换到ffmpeg.exe目录,查看计算机有哪些可用的视频采集设备和音频采集设备,输入命令:
ffmpeg -list_devices true -f dshow -i dummy
如下图:
 
3 查看视频采集设备支持的分辨率、帧率及像素格式等信息,
输入命令:ffmpeg -list_options true -f dshow -i video=”HP HD Camera”
 
输入命令后,输出如下图:
 
4 合成视频的命令:
ffmpeg -f dshow -s 320*180 -i video=”HP HD Camera” -f dshow -i audio=”麦克风 (USB Microphone)” -vcodec mpeg4 -acodec aac -strict -2 myVideo.mp4
其中,HP HD Camera是我测试电脑的视频采集设备名称,麦克风 (USB Microphone) 是我测试电脑的声音采集设备的名称,mpeg4是视频编码格式(或填libx264),aac是声音编码格式,myVideo.mp4是录制完后的名称
输入该命令后,会在控制台输出录制的信息,输入q键结束录制
5 相关介绍已经介绍完毕,下面使用C#代码实现,采用winform,工程名称为ffmpegDemo
5.1 winform界面如下图:
 
5.2 把工具ffmepg.exe拷贝到bin/Debug目录下
5.3 新建一个名为ffmpegManager的类,用来处理视频录制的业务逻辑,编辑如下:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ffmpegDemo
{
    public class ffmpegManager
    {
        Process p;
        public ffmpegManager()
        {
            Init();
        }
        /// <summary>
        /// 初始化操作
        /// </summary>
        private void Init() {
            p = new Process();
            //设置要启动的应用程序
            p.StartInfo.FileName = “cmd.exe”;
            //是否使用操作系统shell启动
            p.StartInfo.UseShellExecute = false;
            // 接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardInput = true;
            //输出信息
            p.StartInfo.RedirectStandardOutput = true;
            // 输出错误
            p.StartInfo.RedirectStandardError = true;
            //不显示程序窗口
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
            p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
            //启动程序
            p.Start();
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();
        }
        /// <summary>
        /// 录制视频
        /// </summary>
        /// <param name=”fileName”>保存视频的名称</param>
public void RecordVideo(string videoDeviceName,string audioDeviceName,string fileName) {
            //可以根据需要修改命令中的参数
            //完整命令:ffmpeg -f dshow -s 320*180 -i video=”HP HD Camera” -f dshow -i audio=”麦克风 (USB Microphone)” -vcodec mpeg4 -acodec aac -strict -2 myVideo.mp4
            string command = “ffmpeg -f dshow -s 320*180 -i video=\””+videoDeviceName+”\” -f dshow -i audio=\””+audioDeviceName+”\” -vcodec mpeg4 -acodec aac -strict -2 “+fileName;
            //向cmd窗口发送输入信息
            p.StandardInput.WriteLine(command);
p.StandardInput.AutoFlush = true;
}
        //停止录制
        public void StopRecord() {
            //输入q命令停止录制
            p.StandardInput.WriteLine(“q”);
}
        private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                Console.WriteLine(e.Data);
            }
        }
        private void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                Console.WriteLine(e.Data);
            }
        }
    }
}
5.3  在Form.cs中输入代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ffmpegDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //注册按钮点击事件
            this.btnStartRecord.Click += new System.EventHandler(this.btnStartRecord_Click);
            this.btnStopRecord.Click += new System.EventHandler(this.btnStopRecord_Click);
        }
        ffmpegManager manager;
        private void btnStartRecord_Click(object sender, EventArgs e)
        {
            string fileName = this.textBox1.Text.Trim();
            if (fileName.Length == 0)
            {
                MessageBox.Show(“请输入文件名”);
                return;
            }
            manager = new ffmpegManager();
            manager.RecordVideo(“HP HD Camera”, “麦克风 (USB Microphone)”, fileName);
            this.lblTipMessage.Text = “正在录制…”;
            //禁用录制按钮
            this.btnStartRecord.Enabled = false;
            //启用停止录制按钮
            this.btnStopRecord.Enabled = true;
        }
        private void btnStopRecord_Click(object sender, EventArgs e)
        {
            manager.StopRecord();
            this.lblTipMessage.Text = “”;
            //启用录制按钮
            this.btnStartRecord.Enabled = true;
            this.btnStopRecord.Enabled = false;
        }
    }
}
5.4 运行结果如下图(当然我这里项目属性设为控制台项目,所以就能看到对应的输出信息):
 
设置项目属性设为控制台项目只需要鼠标右键项目,然后选择属性,
 
————————————————
版权声明:本文为CSDN博主「zxy2847225301」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zxy13826134783/article/details/106404191