欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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)

    這篇文章主要介紹了c#單例模式(Singleton)的6種實(shí)現(xiàn) ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-12-12
  • C#加密解密文件小工具實(shí)現(xiàn)代碼

    C#加密解密文件小工具實(shí)現(xiàn)代碼

    一個(gè)文件夾加密小工具,該工具是操作文件夾名稱的方法實(shí)現(xiàn)文件夾的一般加密,文件夾中的文件(視頻、圖片等)都原封不動的保存在那里
    2012-05-05
  • c# 用ffmpeg從視頻中截圖

    c# 用ffmpeg從視頻中截圖

    這篇文章主要介紹了c# 用ffmpeg從視頻中截圖的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • C# winform操作CSV格式文件

    C# winform操作CSV格式文件

    這篇文章主要為大家詳細(xì)介紹了C#在winform中的表格操作CSV格式文件的相關(guān)實(shí)例,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下
    2025-03-03
  • Unity工具類之生成文本驗(yàn)證碼

    Unity工具類之生成文本驗(yàn)證碼

    這篇文章主要為大家詳細(xì)介紹了Unity工具類之生成文本驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • C#中datagridview的EditingControlShowing事件用法實(shí)例

    C#中datagridview的EditingControlShowing事件用法實(shí)例

    這篇文章主要介紹了C#中datagridview的EditingControlShowing事件用法,實(shí)例分析了datagridview的EditingControlShowing事件的定義與使用技巧,需要的朋友可以參考下
    2015-06-06
  • Unity實(shí)現(xiàn)識別圖像中主體及其位置

    Unity實(shí)現(xiàn)識別圖像中主體及其位置

    EasyDL基于飛槳開源深度學(xué)習(xí)平臺,面向企業(yè)AI應(yīng)用開發(fā)者提供零門檻AI開發(fā)平臺,實(shí)現(xiàn)零算法基礎(chǔ)定制高精度AI模型。本文將利用Unity和EasyDL實(shí)現(xiàn)識別圖像中主體及其位置,感興趣的可以了解一下
    2022-02-02
  • C#中的引用類型以及特殊引用類型詳解

    C#中的引用類型以及特殊引用類型詳解

    本文詳細(xì)講解了C#中的引用類型以及特殊引用類型,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • C#使用Fody實(shí)現(xiàn)監(jiān)控方法執(zhí)行時(shí)間

    C#使用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
  • 詳解Unity入門之GameObject

    詳解Unity入門之GameObject

    Unity是一個(gè)Component-Based的引擎,所有物體都是GameObject。本文將詳細(xì)介紹GameObject和MonoBehaviour,感興趣的同學(xué),可以參考下。
    2021-05-05

最新評論