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?模擬控制臺(tái)信號需要使用的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)文章
快速解決owin返回json字符串多帶了雙引號"多了重string轉(zhuǎn)義字符串
下面小編就為大家?guī)硪黄焖俳鉀Qowin返回json字符串多帶了雙引號"多了重string轉(zhuǎn)義字符串。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08
Unity實(shí)現(xiàn)物體弧線運(yùn)動(dòng)到規(guī)定的坐標(biāo)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)物體以弧線的形式運(yùn)動(dòng)到規(guī)定的坐標(biāo),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
C#利用時(shí)間和隨即字符串創(chuàng)建唯一的訂單編號
本文介紹了利用時(shí)間和隨機(jī)字符串組合生成唯一訂單號的示例,從而保證訂單號不會(huì)重復(fù),希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2016-03-03
C#實(shí)現(xiàn)拆分合并Word表格中的單元格
我們在使用Word制作表格時(shí),由于表格較為復(fù)雜,只是簡單的插入行、列并不能滿足我們的需要。要做一個(gè)完整的表格,很多時(shí)候需要將單元格進(jìn)行拆分或者合并。本文將詳細(xì)為您介紹在Word表格中拆分或合并單元格的思路及方法,希望對大家有所幫助2022-12-12
C#?操作Windows注冊表的實(shí)現(xiàn)方法
本文主要介紹了C#?操作Windows注冊表的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03最新評論

