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

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?模擬控制臺(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)文章

  • C#應(yīng)用ToolStrip控件使用方法

    C#應(yīng)用ToolStrip控件使用方法

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

    快速解決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)

    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ù)ref詳解

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

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

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

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

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

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

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

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

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

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

    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)方法

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

最新評論

垣曲县| 福泉市| 青川县| 济阳县| 镇原县| 岱山县| 平邑县| 桃园市| 平远县| 黑河市| 方城县| 健康| 鲜城| 邛崃市| 新兴县| 府谷县| 和林格尔县| 涡阳县| 洛川县| 桃园县| 洛浦县| 马公市| 凤山市| 濮阳县| 双桥区| 新邵县| 安岳县| 璧山县| 来安县| 星座| 时尚| 玉溪市| 瓦房店市| 大安市| 疏勒县| 合水县| 佳木斯市| 准格尔旗| 武平县| 安溪县| 德阳市|