当前位置:   article > 正文

DXGI高帧率屏幕录像软件源码解析(声音捕获,抓屏,ffmpeg录像,MP4录像,flv录像,麦克风采集)(第1篇API展示及使用部分)_pydxgi

pydxgi

本文DEMO源码下载:

(91条消息) dxgi桌面屏幕录像(windows屏幕录像,硬件编码,声音捕获,音视频同步)-编解码文档类资源-CSDN文库icon-default.png?t=M276https://download.csdn.net/download/xjb2006/85109025

由于篇幅有限,分为4篇发表:

1、SDK接口一览:

2、声音采集部分:

3、屏幕捕获部分:

4、编码,录像部分:

距离上篇文章已经过了快1年了,才有时间把正式DEMO传上来,直接上个截图看看吧:

该DEMO演示了win10屏幕录像的核心功能,包含音源选择(支持麦克风,计算机声音和2者混合),屏幕选择(主副屏选择),鼠标,帧率,码率,硬件编码,实时预览,双录制(同时录制为flv,mp4)等基本功能。为了扩展需要,程序核心模块做成DLL动态库,可以多语言扩展,适用于C++,C#,JAVA,VB,Python等等其他语言。

SDK接口展示:

为方便其他语言和开发复用,将核心部件封装成了DLL:

xDxgiScreen.dll:负责屏幕采集

xScreenRecord.dll:负责音频采集,声音合成,音视频编码,音视频合成录像等

API一览:

  1. typedef void (*pUserKey)(char* szUser,char* szKey);
  2. typedef int (*pStartSendScreen)();
  3. typedef void (*pStopSendScreen)();
  4. typedef void (*pSetEncoderName)(char* name);//指定编码器
  5. typedef void (*pSetEncoder)(int bps,double fps,int gop);
  6. typedef void (*pSetIP)(char* szIP);
  7. typedef int (*pGetCapMod)();
  8. typedef int (*pGetEncoderID)();
  9. typedef void(*pSetHaveCursor) (int nHave);
  10. typedef void(*pSetMonitor) (int nMonitor);
  11. typedef void(*pSetCapAudioType) (int nCapAudioType);
  12. typedef void(*pSetVideoProperty)(int bps,int fps,int gop);
  13. typedef void(*pStartRecord)(char *szMp4,char *szFlv);
  14. typedef void(*pStopRecord)();
  15. typedef int (*pOpenDxgi)();
  16. typedef void(*pStartPreview)(int hWnd);
  17. typedef void(*pStopPreview)();
  18. typedef void(*pCloseAll)();
  19. typedef bool(*pIsRecording)();
  20. typedef int(*pGetMonitorCount)();
  21. typedef int(*pGetMonitorWidth)(int nMonitorIndex);
  22. typedef int(*pGetMonitorHeight)(int nMonitorIndex);
  23. typedef struct tagScreenShareDll
  24. {
  25. pUserKey UserKey;
  26. pStartSendScreen StartSendScreen;
  27. pStopSendScreen StopSendScreen;
  28. pSetEncoderName SetEncoderName;
  29. pSetEncoder SetEncoder;
  30. pSetIP SetIP;
  31. pGetCapMod GetCapMod;
  32. pGetEncoderID GetEncoderID;
  33. pSetHaveCursor SetHaveCursor;
  34. pSetMonitor SetMonitor;
  35. pSetCapAudioType SetCapAudioType;
  36. pSetVideoProperty SetVideoProperty;
  37. pStartRecord StartRecord;
  38. pStopRecord StopRecord;
  39. pOpenDxgi OpenDxgi;
  40. pStartPreview StartPreview;
  41. pStopPreview StopPreview;
  42. pCloseAll CloseAll;
  43. pIsRecording IsRecording;
  44. pGetMonitorCount GetMonitorCount;
  45. pGetMonitorWidth GetMonitorWidth;
  46. pGetMonitorHeight GetMonitorHeight;
  47. }SCREENSHAREDLL;

C++调用示例:

  1. BOOL CScreenShareDlg::LoadScreenShareDll()
  2. {
  3. CString szDll=GetExeFilePath()+L"xScreenRecord.dll";
  4. //HINSTANCE hDLL = LoadLibrary(szDll);
  5. HINSTANCE hDLL = LoadLibraryEx(szDll, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  6. if(hDLL==0)
  7. {
  8. MessageBox(L"xScreenRecord.dll加载失败!");
  9. return false;
  10. }
  11. else
  12. {
  13. m_screenrecord.UserKey= (pUserKey)GetProcAddress(hDLL, "UserKey");
  14. m_screenrecord.StartSendScreen = (pStartSendScreen)GetProcAddress(hDLL, "StartSendScreen");
  15. m_screenrecord.StopSendScreen = (pStopSendScreen)GetProcAddress(hDLL, "StopSendScreen");
  16. m_screenrecord.SetIP = (pSetIP)GetProcAddress(hDLL, "SetIP");
  17. m_screenrecord.SetEncoder = (pSetEncoder)GetProcAddress(hDLL, "SetEncoder");
  18. m_screenrecord.GetCapMod = (pGetCapMod)GetProcAddress(hDLL, "GetCapMod");
  19. m_screenrecord.GetEncoderID = (pGetEncoderID)GetProcAddress(hDLL, "GetEncoderID");
  20. m_screenrecord.SetHaveCursor = (pSetHaveCursor)GetProcAddress(hDLL, "SetHaveCursor");
  21. m_screenrecord.SetMonitor = (pSetMonitor)GetProcAddress(hDLL, "SetMonitor");
  22. m_screenrecord.SetCapAudioType = (pSetCapAudioType)GetProcAddress(hDLL, "SetCapAudioType");
  23. m_screenrecord.SetVideoProperty = (pSetVideoProperty)GetProcAddress(hDLL, "SetVideoProperty");
  24. m_screenrecord.StartRecord = (pStartRecord)GetProcAddress(hDLL, "StartRecord");
  25. m_screenrecord.StopRecord = (pStopRecord)GetProcAddress(hDLL, "StopRecord");
  26. m_screenrecord.OpenDxgi = (pOpenDxgi)GetProcAddress(hDLL, "OpenDxgi");
  27. m_screenrecord.StartPreview = (pStartPreview)GetProcAddress(hDLL, "StartPreview");
  28. m_screenrecord.StopPreview = (pStopPreview)GetProcAddress(hDLL, "StopPreview");
  29. m_screenrecord.CloseAll = (pCloseAll)GetProcAddress(hDLL, "CloseAll");
  30. m_screenrecord.GetMonitorCount = (pGetMonitorCount)GetProcAddress(hDLL, "GetMonitorCount");
  31. m_screenrecord.GetMonitorWidth = (pGetMonitorWidth)GetProcAddress(hDLL, "GetMonitorWidth");
  32. m_screenrecord.GetMonitorHeight = (pGetMonitorHeight)GetProcAddress(hDLL, "GetMonitorHeight");
  33. m_screenrecord.SetEncoderName = (pSetEncoderName)GetProcAddress(hDLL, "SetEncoderName");
  34. m_screenrecord.IsRecording = (pIsRecording)GetProcAddress(hDLL, "IsRecording");
  35. m_screenrecord.UserKey("test","test");
  36. //m_screenrecord.SetHaveCursor(0);
  37. int nMonitor=this->GetCheckedRadioButton(IDC_RADIO4,IDC_RADIO5)-IDC_RADIO4;
  38. m_screenrecord.SetMonitor(nMonitor);
  39. m_screenrecord.OpenDxgi();
  40. m_screenrecord.StartPreview((int)m_vdlg.m_hWnd);
  41. }
  42. return true;
  43. }
  44. void CScreenShareDlg::OnBnClickedOk()
  45. {
  46. if(m_recmin.GetChecked())
  47. {
  48. this->ShowWindow(SW_HIDE);
  49. ShowInTaskbar(this->GetSafeHwnd(),0);
  50. }
  51. int nCapAudioType=this->GetCheckedRadioButton(IDC_RADIO1,IDC_RADIO3)-IDC_RADIO1;
  52. int nMonitor=this->GetCheckedRadioButton(IDC_RADIO4,IDC_RADIO5)-IDC_RADIO4;
  53. int nOutFPS=this->GetCheckedRadioButton(IDC_RADIO10,IDC_RADIO12)-IDC_RADIO10;
  54. int nOutKBPS=this->GetCheckedRadioButton(IDC_RADIO6,IDC_RADIO9)-IDC_RADIO6;
  55. int nEncodeType=m_encodetype.GetChecked();
  56. if(m_screenrecord.SetEncoderName)
  57. m_screenrecord.SetEncoderName(nEncodeType?"":"libx264");
  58. m_screenrecord.SetCapAudioType(nCapAudioType);
  59. m_screenrecord.SetMonitor(nMonitor);
  60. m_screenrecord.SetVideoProperty(OutKBPS[nOutKBPS]*1024*1024,OutFPS[nOutFPS],OutFPS[nOutFPS]*3);
  61. m_screenrecord.StartRecord("c:\\mp4.mp4","c:\\flv.flv");
  62. int n0=m_screenrecord.GetCapMod();
  63. int n1=m_screenrecord.GetEncoderID();
  64. n1=n1;
  65. m_nStartTime=::GetTickCount();
  66. }

C#调用示例:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Runtime.InteropServices; // DLL support
  11. namespace DXGIRecordTest
  12. {
  13. public partial class Form1 : Form
  14. {
  15. [DllImport("user32.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  16. public static extern void SetProcessDPIAware();
  17. //导入xScreenRecord.dll
  18. [DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  19. public static extern void UserKey([In, Out] string user, [In, Out] string key);
  20. [DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  21. public static extern void StartRecord([In, Out] string szMp4, [In, Out] string szFLv);
  22. [DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  23. public static extern void StopRecord();
  24. [DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  25. public static extern void SetHaveCursor([In, Out] int nHave);
  26. [DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  27. public static extern void SetCapAudioType([In, Out] int nCapAudioType);
  28. [DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  29. public static extern void SetMonitor([In, Out] int nMonitorIndex);
  30. [DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  31. public static extern void SetVideoProperty([In, Out] int bps, [In, Out] int fps, [In, Out] int gop);
  32. [DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  33. public static extern int OpenDxgi();
  34. [DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  35. public static extern void StartPreview([In, Out] int hWnd);
  36. [DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  37. public static extern void StopPreview();
  38. [DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  39. public static extern int GetMonitorCount();
  40. [DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  41. public static extern int GetMonitorWidth(int nMonitor);
  42. [DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  43. public static extern int GetMonitorHeight(int nMonitor);
  44. [DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  45. public static extern void CloseAll();
  46. public Form1()
  47. {
  48. InitializeComponent();
  49. }
  50. private void Form1_Load(object sender, EventArgs e)
  51. {
  52. SetProcessDPIAware();
  53. UserKey("test", "test");
  54. comboBox1.SelectedIndex = 0;
  55. comboBox2.SelectedIndex = 0;
  56. checkBox1.Checked = true;
  57. OpenDxgi();
  58. StartPreview((int)pictureBox1.Handle);
  59. }
  60. private void button1_Click(object sender, EventArgs e)//录像
  61. {
  62. comboBox2.Enabled = false;
  63. SetCapAudioType(comboBox1.SelectedIndex);//0麦克风 1计算机 2麦克风+计算机
  64. SetMonitor(comboBox2.SelectedIndex);//显示器序号
  65. SetHaveCursor(checkBox1.Checked==true?1:0);//是否录制鼠标
  66. SetVideoProperty(Convert.ToInt32(bps.Text)*1024*8, Convert.ToInt32(fps.Text), Convert.ToInt32(gop.Text));
  67. StartRecord("c:\\mp4.mp4","c:\\flv.flv");
  68. }
  69. private void button2_Click(object sender, EventArgs e)//停止
  70. {
  71. StopRecord();
  72. comboBox2.Enabled = true;
  73. }
  74. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  75. {
  76. CloseAll();
  77. }
  78. private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
  79. {
  80. int cx=GetMonitorWidth(comboBox2.SelectedIndex);
  81. int cy = GetMonitorHeight(comboBox2.SelectedIndex);
  82. label9.Text = comboBox2.Text + cx + "X" + cy;
  83. SetHaveCursor(checkBox1.Checked == true ? 1 : 0);
  84. SetMonitor(comboBox2.SelectedIndex);//显示器序号
  85. OpenDxgi();
  86. StartPreview((int)pictureBox1.Handle);
  87. }
  88. private void checkBox1_CheckedChanged(object sender, EventArgs e)
  89. {
  90. comboBox2_SelectedIndexChanged(sender,e);
  91. }
  92. }
  93. }

本文DEMO源码下载:

(91条消息) dxgi桌面屏幕录像(windows屏幕录像,硬件编码,声音捕获,音视频同步)-编解码文档类资源-CSDN文库icon-default.png?t=M276https://download.csdn.net/download/xjb2006/85109025

QQ35744025 萧萧工作室

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/430052
推荐阅读
相关标签
  

闽ICP备14008679号