网上的好多教程,关于FFmpeg录制视频的,要安装Screen Capturer Recorder,
我们其实不需要去安装一大坨的东西
1、还是要下载、安装
但是我们只需要用到其中的2个组件
audio_sniffer是在目录:Screen Capturer Recorder\vendor\virtual-audio
根据系统需要选择相应的组件,我们默认使用非x64的,即x32
安装后提取这两个选中的文件就可以卸载了
在使用之前先干一件事,在2个项目中分别新建一个应用程序清单文件,如果你只有一个项目,就只需要一个应用程序清单文件。把
注释部分的第二行替换 "-->"后面的内容(我已经替换好了)这样,主程序就会以管理员身份运行,以管理员身份重新启动visual studio。目的是注册那两个组件,让FFmpeg识别,组件的名字不要改动。(一定要使用管理员身份)
注册的代码:
Public Class RegDll
Public Function RegRec()
VideoProcess.SharedP("C:\WINDOWS\system32\cmd.exe", "/c /q regsvr32 /u " & Application.StartupPath & "\screen-capture-recorder.dll", True)
VideoProcess.SharedP("C:\WINDOWS\system32\cmd.exe", "/c /q regsvr32 " & Application.StartupPath & "\screen-capture-recorder.dll", True)
VideoProcess.SharedP("C:\WINDOWS\system32\cmd.exe", "/c /q regsvr32 /u " & Application.StartupPath & "\audio_sniffer.dll", True)
VideoProcess.SharedP("C:\WINDOWS\system32\cmd.exe", "/c /q regsvr32 " & Application.StartupPath & "\audio_snifferr.dll", True)
End Function
Public Shared Function SharedP(s As String, Arguments As String, nowindow As Boolean)
Dim p As New Process
p.StartInfo.FileName = s
p.StartInfo.Arguments = Arguments
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardError = True
p.StartInfo.CreateNoWindow = nowindow
p.Start()
p.WaitForExit()
p.StandardError.ReadToEnd()
Return p.StandardError.ReadToEnd()
p.Close()
p.Dispose()
End Function
End Class
注:可以把注册和sharedP 写在同一个类里,我的sharedP写在了VideoProcess类中
主窗体先注册这两个组件,为了在给别人用的时候,可以实现操作简单化,让程序自己注册,可以在Form_Load时注册,即执行RegRec函数(function,可以改成sub)
2、在注册的前提下,我们就不在FFmpeg测试了,肯定有了
FFmpeg -list_devices true -f dshow -i dummy
3、写2个过程,不一定要是函数,因为没有可以返回的值
Public PRec As New Process
Public Function StartRecordVid(ffmpeg As String, out As String)
PRec.StartInfo.FileName = "cmd"
PRec.StartInfo.Arguments = "/c " & ffmpeg & " -f dshow -i audio=" & Chr(34) & "麦克风阵列 (Conexant SmartAudio HD)" & Chr(34) & " -f dshow -i audio=" & Chr(34) & "virtual-audio-capturer" & Chr(34) & " -filter_complex amix=inputs=2:duration=first:dropout_transition=0 -f dshow -i video=" & Chr(34) & "screen-capture-recorder" & Chr(34) & " -pix_fmt yuv420p " & out & " -y"
PRec.StartInfo.UseShellExecute = False
PRec.StartInfo.CreateNoWindow = true
'PRec.StartInfo.RedirectStandardOutput = True
'PRec.StartInfo.RedirectStandardError = True
PRec.StartInfo.RedirectStandardInput = True
PRec.Start()
PRec.EnableRaisingEvents = True
End Function
Public Function EndRecordVid()
PRec.StandardInput.WriteLine("q")
PRec.StandardInput.AutoFlush = True
PRec.Close()
PRec.Dispose()
End Function
输入的时候需要用到 " 可以使用chr(34)代替
值得注意的是:由于在看别人的教程时没有细致考虑,导致先执行StartRecordVid函数时,PRec已经close + dispose了,以至于在调用停止的时候显示StandardInput尚未重定向这个错误
这是错误的代码:
Public Function StartRecordVid(ffmpeg As String, out As String)
PRec.StartInfo.FileName = "cmd"
PRec.StartInfo.Arguments = "/c " & ffmpeg & " -f dshow -i audio=" & Chr(34) & "麦克风阵列 (Conexant SmartAudio HD)" & Chr(34) & " -f dshow -i audio=" & Chr(34) & "virtual-audio-capturer" & Chr(34) & " -filter_complex amix=inputs=2:duration=first:dropout_transition=0 -f dshow -i video=" & Chr(34) & "screen-capture-recorder" & Chr(34) & " -pix_fmt yuv420p " & out & " -y"
PRec.StartInfo.UseShellExecute = False
PRec.StartInfo.CreateNoWindow = true
'PRec.StartInfo.RedirectStandardOutput = True
'PRec.StartInfo.RedirectStandardError = True
PRec.StartInfo.RedirectStandardInput = True
PRec.Start()
PRec.EnableRaisingEvents = True
PRec.Close()
PRec.Dispose()
End Function
Public Function EndRecordVid()
PRec.StandardInput.WriteLine("q")
PRec.StandardInput.AutoFlush = True
End Function
这是一个坑,不要去踩哦
最后:不要忘记把FFmpeg所有的进程全部结束任务,
Public Function EndRecordVid()
PRec.StandardInput.WriteLine("q")
PRec.StandardInput.AutoFlush = True
' PRec.Close()
'PRec.Dispose()
PRec.Kill()
End Function
加入kill指令,以便移动视频,不需要close 和dispose了。