WPF使用Accord實現(xiàn)屏幕錄制功能
WPF 使用 Accord 實現(xiàn)屏幕錄制
框架使用.NET4
Visual Studio 2022
WPF 實現(xiàn)調(diào)用 FFmpeg 實現(xiàn)屏幕錄制
WPF 實現(xiàn)調(diào)用 WindowsAPI 實現(xiàn)屏幕錄制
此篇使用 Accord
實現(xiàn)屏幕錄制,此次使用了三個庫Accord
、Accord.Video
、Accord.FFMPEG
Accord
: 開源的機器學(xué)習(xí)框架,它提供了一系列用于數(shù)據(jù)處理、圖像處理、機器學(xué)習(xí)和統(tǒng)計分析的工具和庫。
Accord.Video
:提供了處理視頻數(shù)據(jù),并提供了對視頻文件的讀取、處理和分析功能。
Accord.FFMPEG
: 提供了對 FFmpeg
功能的封裝和集成,提供音視頻處理功能,如視頻轉(zhuǎn)碼、格式轉(zhuǎn)換、流媒體處理等。
使用的版本為 3.8.0
實現(xiàn)代碼
1)新增 AccordHelper
代碼如下:
- 定義
ScreenCaptureStream
對象用于捕獲屏幕內(nèi)容。 - 定義了一個
VideoFileWriter
對象用于將捕獲到的屏幕內(nèi)容寫入視頻文件。 - 在
Start()
方法中,創(chuàng)建ScreenCaptureStream
對象,并指定捕獲屏幕的區(qū)域為整個屏幕
。 - 然后再創(chuàng)建了一個
VideoFileWriter
對象,并指定了輸出視頻文件的路徑
、寬度
、高度
、幀率
、視頻編解碼器
和比特率
。 - 設(shè)置捕獲屏幕幀的間隔為
40
毫秒。 - 設(shè)置視頻比特率為
1200 * 1000
,如果設(shè)置較高的比特率視頻會更加清晰,但是文件也會相對增大。 - 注冊了
NewFrame
事件處理程序,當(dāng)有新的幀捕獲時,將該幀就寫入視頻文件。 - 在
Stop()
方法中,檢查screenShot
和videoWriter
是否為null
,如果不為null
,則分別停止屏幕捕獲和關(guān)閉視頻寫入。
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="開始錄制" 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實現(xiàn)屏幕錄制功能的詳細(xì)內(nèi)容,更多關(guān)于WPF屏幕錄制的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解C#實現(xiàn)在Excel單元格中應(yīng)用多種字體格式
在Excel中,可對單元格中的字符串設(shè)置多種不同樣式。本文,將以C#及VB.NET代碼為例,介紹如何在Excel同一個單元格中應(yīng)用多種字體樣式,感興趣的可以了解一下2022-05-05C#中Kestrel和IIS服務(wù)器下的同步與異步配置
本篇文章主要講解什么是Kestrel和IIS服務(wù)器和特點,以及他們?nèi)绾闻渲猛脚c異步,具有一定的參加價值,感興趣的可以了解一下2023-08-08