WPF調(diào)用ffmpeg實(shí)現(xiàn)屏幕錄制
WPF 實(shí)現(xiàn)調(diào)用 ffmpeg 實(shí)現(xiàn)屏幕錄制
框架使用.NET4
Visual Studio 2022
需要去 ffmpeg[2] 官網(wǎng)下載 Windows
解壓進(jìn)入 ffmpeg-4.1.1-win32-static\bin\
或者 下載 ffmpeg.exe[3] 拷貝到運(yùn)行目錄下的ffmpeg
文件夾下 DesktopRecord.exe
就可以進(jìn)行錄屏,結(jié)束錄屏后視頻保存在運(yùn)行程序根目錄下。
使用參數(shù)命令進(jìn)行錄屏 "-f gdigrab -framerate 30 -offset_x 0 -offset_y 0 -video_size 1920x1080 -i desktop -c:v libx264 -preset ultrafast -crf 0 " + DateTime.Now.ToString("yyyyMMddHHmmss") + "_DesktopRecord.mp4"
-f gdigrab
: 設(shè)定視頻輸入來源為Windows
桌面畫面捕獲;-framerate 30
: 設(shè)置幀率為30fps
;-offset_x 0 -offset_y 0
: 設(shè)置捕獲起始坐標(biāo)為(0, 0)
;-video_size 1920x1080
: 設(shè)置視頻分辨率為1920x1080
;-i desktop
: 指示從桌面捕獲視頻流;-c:v libx264
: 使用libx264
編碼器進(jìn)行視頻壓縮;-preset ultrafast
: 設(shè)定視頻壓縮速度為最快;-crf 0
: 設(shè)置視頻壓縮質(zhì)量無限制(CRF
為0
表示最高質(zhì)量);" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_DesktopRecord.mp4"
: 指定視頻輸出文件名為yyyyMMddHHmmss_DesktopRecord.mp4
。
實(shí)現(xiàn)代碼
1)創(chuàng)建 FFmpegHelper.cs
代碼如下:
using?System; using?System.Diagnostics; using?System.IO; using?System.Runtime.InteropServices; namespace?DesktopRecord.Helper { ????public?class?FFmpegHelper ????{ ????????#region?模擬控制臺信號需要使用的api ????????[DllImport("kernel32.dll")] ????????static?extern?bool?GenerateConsoleCtrlEvent(int?dwCtrlEvent,?int?dwProcessGroupId); ????????[DllImport("kernel32.dll")] ????????static?extern?bool?SetConsoleCtrlHandler(IntPtr?handlerRoutine,?bool?add); ????????[DllImport("kernel32.dll")] ????????static?extern?bool?AttachConsole(int?dwProcessId); ????????[DllImport("kernel32.dll")] ????????static?extern?bool?FreeConsole(); ????????#endregion ????????//?ffmpeg進(jìn)程 ????????static?Process?_process; ????????//?ffmpeg.exe實(shí)體文件路徑 ????????static?string?ffmpegPath?=?Path.Combine(AppDomain.CurrentDomain.BaseDirectory,?"ffmpeg.exe"); ????????///?<summary> ????????///?功能:?開始錄制 ????????///?</summary> ????????public?static?bool?Start() ????????{ ????????????if(!File.Exists(ffmpegPath)) ????????????????return?false; ????????????var?processInfo?=?new?ProcessStartInfo ????????????{ ????????????????FileName?=?ffmpegPath, ????????????????Arguments?=?"-f?gdigrab?-framerate?30?-offset_x?0?-offset_y?0?-video_size?1920x1080?-i?desktop?-c:v?libx264?-preset?ultrafast?-crf?0?"?+?DateTime.Now.ToString("yyyyMMddHHmmss")?+?"_DesktopRecord.mp4", ????????????????UseShellExecute?=?false, ????????????????RedirectStandardInput?=?true, ????????????????RedirectStandardOutput?=?true, ????????????????CreateNoWindow?=?true ????????????}; ????????????_process?=?new?Process?{?StartInfo?=?processInfo?}; ????????????_process.Start(); ????????????return?true; ????????} ????????///?<summary> ????????///?功能:?停止錄制 ????????///?</summary> ????????public?static?void?Stop() ????????{ ????????????if?(_process?==?null)?return; ????????????AttachConsole(_process.Id); ????????????SetConsoleCtrlHandler(IntPtr.Zero,?true); ????????????GenerateConsoleCtrlEvent(0,?0); ????????????FreeConsole(); ????????????_process.StandardInput.Write("q"); ????????????if?(!_process.WaitForExit(10000)) ????????????{ ????????????????_process.Kill(); ????????????} ????????} ????} }
2)創(chuàng)建 MainWindow.xaml
代碼如下:
<wd:Window ????x:Class="DesktopRecord.View.MainWindow" ????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ????xmlns:d="http://schemas.microsoft.com/expression/blend/2008" ????xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" ????xmlns:vm="clr-namespace:DesktopRecord.ViewModel" ????xmlns:wd="https://github.com/WPFDevelopersOrg/WPFDevelopers" ????Title="屏幕錄制" ????Width="525" ????Height="200" ????Icon="/screen.ico" ????ResizeMode="CanMinimize" ????WindowStartupLocation="CenterScreen" ????mc:Ignorable="d"> ????<wd:Window.DataContext> ????????<vm:MainVM?/> ????</wd:Window.DataContext> ????<Grid> ????????<TabControl> ????????????<TabItem?Header="ffmpeg?錄制"> ????????????????<StackPanel ????????????????????HorizontalAlignment="Center" ????????????????????VerticalAlignment="Center" ????????????????????Orientation="Horizontal"> ????????????????????<Button ????????????????????????Margin="0,0,5,0" ????????????????????????Command="{Binding?MyStart}" ????????????????????????Content="{Binding?MyTime}" ????????????????????????Style="{StaticResource?WD.SuccessPrimaryButton}"?/> ????????????????????<Button ????????????????????????Margin="5,0,0,0" ????????????????????????Command="{Binding?MyStop}" ????????????????????????Content="停止錄制" ????????????????????????Style="{StaticResource?WD.DangerPrimaryButton}"?/> ????????????????</StackPanel> ????????????</TabItem> ????????</TabControl> ????</Grid> </wd:Window>
3)創(chuàng)建 MainVM.cs
代碼如下:
using?DesktopRecord.Helper; using?System; using?System.Diagnostics; using?System.Threading.Tasks; using?System.Windows.Input; using?System.Windows.Threading; using?WPFDevelopers.Controls; using?WPFDevelopers.Helpers; namespace?DesktopRecord.ViewModel { ????public?class?MainVM?:?ViewModelBase ????{ ????????private?DispatcherTimer?tm?=?new?DispatcherTimer(); ????????public?int?currentCount?=?0; ????????private?string?myTime?=?"開始錄制"; ????????public?string?MyTime ????????{ ????????????get?{?return?myTime;?} ????????????set ????????????{ ????????????????myTime?=?value; ????????????????NotifyPropertyChange("MyTime"); ????????????} ????????} ????????private?bool?isStart?=?true; ????????public?bool?IsStart ????????{ ????????????get?{?return?isStart;?} ????????????set ????????????{ ????????????????isStart?=?value; ????????????????NotifyPropertyChange("IsStart"); ????????????} ????????} ????????private?bool?_isShow; ????????public?bool?IsShow ????????{ ????????????get?{?return?_isShow;?} ????????????set ????????????{ ????????????????_isShow?=?value; ????????????????NotifyPropertyChange("IsShow"); ????????????} ????????} ????????private?ICommand?myStart; ????????public?ICommand?MyStart ????????{ ????????????get ????????????{ ????????????????return?myStart????(myStart?=?new?RelayCommand(p?=> ????????????????{ ????????????????????App.Current.MainWindow.WindowState?=?System.Windows.WindowState.Minimized; ????????????????????if?(!FFmpegHelper.Start()) ????????????????????{ ????????????????????????App.Current.MainWindow.WindowState?=?System.Windows.WindowState.Normal; ????????????????????????MessageBox.Show("未找到?【ffmpeg.exe】,請下載",?"錯(cuò)誤",?System.Windows.MessageBoxButton.OK,?System.Windows.MessageBoxImage.Error); ????????????????????????return; ????????????????????} ????????????????????tm.Tick?+=?tm_Tick; ????????????????????tm.Interval?=?TimeSpan.FromSeconds(1); ????????????????????tm.Start(); ????????????????????IsStart?=?false; ???????????????},?a?=> ????????????????{ ????????????????return?IsStart; ????????????????})); ????????????} ????????} ????????private?void?tm_Tick(object?sender,?EventArgs?e) ????????{ ????????????currentCount++; ????????????MyTime?=?"錄制中("?+?currentCount?+?"s)"; ????????} ????????///?<summary> ????????///?獲取或設(shè)置 ????????///?</summary> ????????private?ICommand?myStop; ????????///?<summary> ????????///?獲取或設(shè)置 ????????///?</summary> ????????public?ICommand?MyStop ????????{ ????????????get ????????????{ ????????????????return?myStop????(myStop?=?new?RelayCommand(p?=> ???????????????????????????{ ???????????????????????????????var?task?=?new?Task(()?=> ???????????????????????????????{ ???????????????????????????????????FFmpegHelper.Stop(); ???????????????????????????????????MyTime?=?"開始錄制"; ???????????????????????????????????tm.Stop(); ???????????????????????????????????currentCount?=?0; ???????????????????????????????????IsShow?=?true; ???????????????????????????????}); ???????????????????????????????task.ContinueWith(previousTask?=> ???????????????????????????????{ ???????????????????????????????????IsShow?=?false; ???????????????????????????????????IsStart?=?true; ???????????????????????????????????Process.Start(AppDomain.CurrentDomain.BaseDirectory); ???????????????????????????????},?TaskScheduler.FromCurrentSynchronizationContext()); ???????????????????????????????task.Start(); ???????????????????????????},?a?=> ????????????{ ????????????????return?!IsStart; ????????????})); ????????????} ????????}
效果圖
以上就是WPF調(diào)用ffmpeg實(shí)現(xiàn)屏幕錄制的詳細(xì)內(nèi)容,更多關(guān)于WPF屏幕錄制的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
c#單例模式(Singleton)的6種實(shí)現(xiàn)
這篇文章主要介紹了c#單例模式(Singleton)的6種實(shí)現(xiàn) ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12C#中datagridview的EditingControlShowing事件用法實(shí)例
這篇文章主要介紹了C#中datagridview的EditingControlShowing事件用法,實(shí)例分析了datagridview的EditingControlShowing事件的定義與使用技巧,需要的朋友可以參考下2015-06-06C#使用Fody實(shí)現(xiàn)監(jiān)控方法執(zhí)行時(shí)間
這篇文章主要為大家詳細(xì)介紹了C#如何使用Fody實(shí)現(xiàn)監(jiān)控方法執(zhí)行時(shí)間,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下2023-11-11