WPF使用Accord實(shí)現(xiàn)屏幕錄制功能
WPF 使用 Accord 實(shí)現(xiàn)屏幕錄制
框架使用.NET4
Visual Studio 2022
WPF 實(shí)現(xiàn)調(diào)用 FFmpeg 實(shí)現(xiàn)屏幕錄制
WPF 實(shí)現(xiàn)調(diào)用 WindowsAPI 實(shí)現(xiàn)屏幕錄制
此篇使用 Accord
實(shí)現(xiàn)屏幕錄制,此次使用了三個(gè)庫(kù)Accord
、Accord.Video
、Accord.FFMPEG
Accord
: 開(kāi)源的機(jī)器學(xué)習(xí)框架,它提供了一系列用于數(shù)據(jù)處理、圖像處理、機(jī)器學(xué)習(xí)和統(tǒng)計(jì)分析的工具和庫(kù)。
Accord.Video
:提供了處理視頻數(shù)據(jù),并提供了對(duì)視頻文件的讀取、處理和分析功能。
Accord.FFMPEG
: 提供了對(duì) FFmpeg
功能的封裝和集成,提供音視頻處理功能,如視頻轉(zhuǎn)碼、格式轉(zhuǎn)換、流媒體處理等。
使用的版本為 3.8.0
實(shí)現(xiàn)代碼
1)新增 AccordHelper
代碼如下:
- 定義
ScreenCaptureStream
對(duì)象用于捕獲屏幕內(nèi)容。 - 定義了一個(gè)
VideoFileWriter
對(duì)象用于將捕獲到的屏幕內(nèi)容寫(xiě)入視頻文件。 - 在
Start()
方法中,創(chuàng)建ScreenCaptureStream
對(duì)象,并指定捕獲屏幕的區(qū)域?yàn)?code>整個(gè)屏幕。 - 然后再創(chuàng)建了一個(gè)
VideoFileWriter
對(duì)象,并指定了輸出視頻文件的路徑
、寬度
、高度
、幀率
、視頻編解碼器
和比特率
。 - 設(shè)置捕獲屏幕幀的間隔為
40
毫秒。 - 設(shè)置視頻比特率為
1200 * 1000
,如果設(shè)置較高的比特率視頻會(huì)更加清晰,但是文件也會(huì)相對(duì)增大。 - 注冊(cè)了
NewFrame
事件處理程序,當(dāng)有新的幀捕獲時(shí),將該幀就寫(xiě)入視頻文件。 - 在
Stop()
方法中,檢查screenShot
和videoWriter
是否為null
,如果不為null
,則分別停止屏幕捕獲和關(guān)閉視頻寫(xiě)入。
using Accord.Math; using Accord.Video; using Accord.Video.FFMPEG; using System; using System.Windows; namespace DesktopRecord.Helper { public class AccordHelper { static ScreenCaptureStream screenStream; static VideoFileWriter videoWriter; public static void Start() { var workArea = SystemParameters.WorkArea.Size; var width = (int)workArea.Width; var height = (int)workArea.Height; var rectangle = new System.Drawing.Rectangle(0, 0, width, height); screenStream = new ScreenCaptureStream(rectangle); videoWriter = new VideoFileWriter(); var filePath = $"{Environment.CurrentDirectory}/DesktopRecord_{DateTime.Now.ToString("yyyyMMddHHmmss")}.avi"; var framerate = new Rational(1000, screenStream.FrameInterval); var videoBitRate = 1200 * 1000; videoWriter.Open(filePath, width, height, framerate, VideoCodec.MSMPEG4v3, videoBitRate); screenStream.FrameInterval = 40; screenStream.NewFrame += ScreenStream_NewFrame; screenStream.Start(); } private static void ScreenStream_NewFrame(object sender, NewFrameEventArgs eventArgs) { if (videoWriter == null) return; videoWriter.WriteVideoFrame(eventArgs.Frame); } public static void Stop() { if (screenStream != null) { screenStream.Stop(); screenStream = null; } if (videoWriter != null) { videoWriter.Close(); videoWriter.Dispose(); videoWriter = null; } } } }
2)新增 CommOptionView.xaml
代碼如下:
<UserControl x:Class="DesktopRecord.View.CommOptionView" 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:local="clr-namespace:DesktopRecord.View" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wd="https://github.com/WPFDevelopersOrg/WPFDevelopers" d:DesignHeight="450" d:DesignWidth="800" mc:Ignorable="d"> <Grid> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal"> <Button Margin="0,0,5,0" Command="{Binding RecordCommand}" Content="開(kāi)始錄制" Style="{StaticResource WD.SuccessPrimaryButton}" /> <Button Margin="5,0,0,0" wd:Loading.IsShow="{Binding IsShow}" wd:Loading.LoadingType="Normal" Command="{Binding RecordStopCommand}" Content="停止錄制" Style="{StaticResource WD.DangerPrimaryButton}" /> </StackPanel> </Grid> </UserControl>
3)修改 MainWindow.xaml
代碼如下:
<TabItem Height="35" Header="WindowsAPI 錄制"> <view:CommOptionView> <view:CommOptionView.DataContext> <vm:MainVM RecordEnums="WindowsAPI" /> </view:CommOptionView.DataContext> </view:CommOptionView> </TabItem> <TabItem Height="35" Header="Accord 錄制"> <view:CommOptionView> <view:CommOptionView.DataContext> <vm:MainVM RecordEnums="Accord" /> </view:CommOptionView.DataContext> </view:CommOptionView> </TabItem>
效果圖
以上就是WPF使用Accord實(shí)現(xiàn)屏幕錄制功能的詳細(xì)內(nèi)容,更多關(guān)于WPF屏幕錄制的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解C#實(shí)現(xiàn)在Excel單元格中應(yīng)用多種字體格式
在Excel中,可對(duì)單元格中的字符串設(shè)置多種不同樣式。本文,將以C#及VB.NET代碼為例,介紹如何在Excel同一個(gè)單元格中應(yīng)用多種字體樣式,感興趣的可以了解一下2022-05-05C#中Kestrel和IIS服務(wù)器下的同步與異步配置
本篇文章主要講解什么是Kestrel和IIS服務(wù)器和特點(diǎn),以及他們?nèi)绾闻渲猛脚c異步,具有一定的參加價(jià)值,感興趣的可以了解一下2023-08-08C#中三種Timer計(jì)時(shí)器的詳細(xì)用法
這篇文章介紹了C#中三種Timer計(jì)時(shí)器的詳細(xì)用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05C#使用前序遍歷、中序遍歷和后序遍歷打印二叉樹(shù)的方法
這篇文章主要介紹了C#使用前序遍歷、中序遍歷和后序遍歷打印二叉樹(shù)的方法,涉及C#遍歷二叉樹(shù)的相關(guān)技巧,需要的朋友可以參考下2015-04-04C#通過(guò)流寫(xiě)入數(shù)據(jù)到文件的方法
這篇文章主要介紹了C#通過(guò)流寫(xiě)入數(shù)據(jù)到文件的方法,涉及C#通過(guò)字節(jié)流讀寫(xiě)文件的相關(guān)技巧,需要的朋友可以參考下2015-07-07