最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WPF調(diào)用ffmpeg實現(xiàn)屏幕錄制

 更新時間:2023年05月24日 15:21:25   作者:WPF開發(fā)者  
這篇文章主要為大家詳細介紹了WPF如何調(diào)用ffmpeg實現(xiàn)屏幕錄制,文中的示例代碼講解詳細,對我們學習或工作有一定幫助,感興趣的小伙伴可以了解一下

WPF 實現(xiàn)調(diào)用 ffmpeg 實現(xiàn)屏幕錄制

框架使用.NET4

Visual Studio 2022

需要去 ffmpeg[2] 官網(wǎng)下載 Windows 解壓進入 ffmpeg-4.1.1-win32-static\bin\ 或者 下載 ffmpeg.exe[3] 拷貝到運行目錄下的ffmpeg 文件夾下 DesktopRecord.exe 就可以進行錄屏,結(jié)束錄屏后視頻保存在運行程序根目錄下。

使用參數(shù)命令進行錄屏 "-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: 設定視頻輸入來源為 Windows 桌面畫面捕獲;
  • -framerate 30: 設置幀率為 30fps;
  • -offset_x 0 -offset_y 0: 設置捕獲起始坐標為 (0, 0);
  • -video_size 1920x1080: 設置視頻分辨率為 1920x1080;
  • -i desktop: 指示從桌面捕獲視頻流;
  • -c:v libx264: 使用 libx264 編碼器進行視頻壓縮;
  • -preset ultrafast: 設定視頻壓縮速度為最快;
  • -crf 0: 設置視頻壓縮質(zhì)量無限制(CRF 為 0 表示最高質(zhì)量);
  • " + DateTime.Now.ToString("yyyyMMddHHmmss") + "_DesktopRecord.mp4": 指定視頻輸出文件名為 yyyyMMddHHmmss_DesktopRecord.mp4。

實現(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進程
????????static?Process?_process;
????????//?ffmpeg.exe實體文件路徑
????????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】,請下載",?"錯誤",?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>
????????///?獲取或設置
????????///?</summary>
????????private?ICommand?myStop;
????????///?<summary>
????????///?獲取或設置
????????///?</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實現(xiàn)屏幕錄制的詳細內(nèi)容,更多關(guān)于WPF屏幕錄制的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#應用ToolStrip控件使用方法

    C#應用ToolStrip控件使用方法

    在本篇文章里小編給大家分享了關(guān)于C#應用ToolStrip控件使用方法和技巧,對此有興趣的朋友們學習下。
    2019-01-01
  • 快速解決owin返回json字符串多帶了雙引號

    快速解決owin返回json字符串多帶了雙引號"多了重string轉(zhuǎn)義字符串

    下面小編就為大家?guī)硪黄焖俳鉀Qowin返回json字符串多帶了雙引號"多了重string轉(zhuǎn)義字符串。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08
  • Unity實現(xiàn)物體弧線運動到規(guī)定的坐標

    Unity實現(xiàn)物體弧線運動到規(guī)定的坐標

    這篇文章主要為大家詳細介紹了Unity實現(xiàn)物體以弧線的形式運動到規(guī)定的坐標,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • C#?引用類型參數(shù)ref詳解

    C#?引用類型參數(shù)ref詳解

    C#中ref和out關(guān)鍵字允許按引用傳遞參數(shù),以修改實參的值,本文給大家介紹C#引用類型參數(shù)ref的相關(guān)知識,感興趣的朋友一起看看吧
    2025-06-06
  • WinForm中異步TCP通信的正確打開方式

    WinForm中異步TCP通信的正確打開方式

    軟件開發(fā)中,網(wǎng)絡通信是實現(xiàn)分布式系統(tǒng)、遠程控制和實時數(shù)據(jù)交換的核心技術(shù)之一,TCP 作為傳輸層中最常用的協(xié)議,以其面向連接、可靠傳輸、字節(jié)流通信的特點,本文將結(jié)合一個完整的WinForm 應用程序?qū)嵗?給大家介紹WinForm中異步TCP通信的正確打開方式
    2025-08-08
  • C# 讀寫ini文件操作實現(xiàn)

    C# 讀寫ini文件操作實現(xiàn)

    本文主要介紹了C# 讀寫ini文件操作實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-01-01
  • C#利用時間和隨即字符串創(chuàng)建唯一的訂單編號

    C#利用時間和隨即字符串創(chuàng)建唯一的訂單編號

    本文介紹了利用時間和隨機字符串組合生成唯一訂單號的示例,從而保證訂單號不會重復,希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2016-03-03
  • 基于WPF實現(xiàn)簡單的文件夾比較工具

    基于WPF實現(xiàn)簡單的文件夾比較工具

    文件比較平常都是用Beyond?Compare,可以說離不開的神器,不過Beyond?Compare平常拿它主要是用來做代碼比較,用來做一些大批量的二進制文件比較,其實有點不是很方便,所以本文來用WPF做一個簡單的文件夾比較的小工具
    2023-05-05
  • C#實現(xiàn)拆分合并Word表格中的單元格

    C#實現(xiàn)拆分合并Word表格中的單元格

    我們在使用Word制作表格時,由于表格較為復雜,只是簡單的插入行、列并不能滿足我們的需要。要做一個完整的表格,很多時候需要將單元格進行拆分或者合并。本文將詳細為您介紹在Word表格中拆分或合并單元格的思路及方法,希望對大家有所幫助
    2022-12-12
  • C#?操作Windows注冊表的實現(xiàn)方法

    C#?操作Windows注冊表的實現(xiàn)方法

    本文主要介紹了C#?操作Windows注冊表的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-03-03

最新評論

满洲里市| 黑水县| 嘉兴市| 故城县| 恩施市| 塔河县| 渭源县| 禄丰县| 万载县| 泰兴市| 基隆市| 沅陵县| 循化| 龙里县| 韩城市| 兴海县| 桂林市| 略阳县| 长治市| 安陆市| 沈丘县| 沽源县| 娱乐| 诸暨市| 西林县| 阿勒泰市| 淮阳县| 五河县| 东至县| 稷山县| 霍城县| 册亨县| 荣昌县| 荆州市| 盘锦市| 宜兰市| 正镶白旗| 丹东市| 台江县| 沈阳市| 枞阳县|