本文DEMO源码下载:
(91条消息) dxgi桌面屏幕录像(windows屏幕录像,硬件编码,声音捕获,音视频同步)-编解码文档类资源-CSDN文库https://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一览:
typedef void (*pUserKey)(char* szUser,char* szKey);
typedef int (*pStartSendScreen)();
typedef void (*pStopSendScreen)();
typedef void (*pSetEncoderName)(char* name);//指定编码器
typedef void (*pSetEncoder)(int bps,double fps,int gop);
typedef void (*pSetIP)(char* szIP);
typedef int (*pGetCapMod)();
typedef int (*pGetEncoderID)();
typedef void(*pSetHaveCursor) (int nHave);
typedef void(*pSetMonitor) (int nMonitor);
typedef void(*pSetCapAudioType) (int nCapAudioType);
typedef void(*pSetVideoProperty)(int bps,int fps,int gop);
typedef void(*pStartRecord)(char *szMp4,char *szFlv);
typedef void(*pStopRecord)();
typedef int (*pOpenDxgi)();
typedef void(*pStartPreview)(int hWnd);
typedef void(*pStopPreview)();
typedef void(*pCloseAll)();
typedef bool(*pIsRecording)();
typedef int(*pGetMonitorCount)();
typedef int(*pGetMonitorWidth)(int nMonitorIndex);
typedef int(*pGetMonitorHeight)(int nMonitorIndex);
typedef struct tagScreenShareDll
{
pUserKey UserKey;
pStartSendScreen StartSendScreen;
pStopSendScreen StopSendScreen;
pSetEncoderName SetEncoderName;
pSetEncoder SetEncoder;
pSetIP SetIP;
pGetCapMod GetCapMod;
pGetEncoderID GetEncoderID;
pSetHaveCursor SetHaveCursor;
pSetMonitor SetMonitor;
pSetCapAudioType SetCapAudioType;
pSetVideoProperty SetVideoProperty;
pStartRecord StartRecord;
pStopRecord StopRecord;
pOpenDxgi OpenDxgi;
pStartPreview StartPreview;
pStopPreview StopPreview;
pCloseAll CloseAll;
pIsRecording IsRecording;
pGetMonitorCount GetMonitorCount;
pGetMonitorWidth GetMonitorWidth;
pGetMonitorHeight GetMonitorHeight;
}SCREENSHAREDLL;
C++调用示例:
BOOL CScreenShareDlg::LoadScreenShareDll()
{
CString szDll=GetExeFilePath()+L"xScreenRecord.dll";
//HINSTANCE hDLL = LoadLibrary(szDll);
HINSTANCE hDLL = LoadLibraryEx(szDll, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
if(hDLL==0)
{
MessageBox(L"xScreenRecord.dll加载失败!");
return false;
}
else
{
m_screenrecord.UserKey= (pUserKey)GetProcAddress(hDLL, "UserKey");
m_screenrecord.StartSendScreen = (pStartSendScreen)GetProcAddress(hDLL, "StartSendScreen");
m_screenrecord.StopSendScreen = (pStopSendScreen)GetProcAddress(hDLL, "StopSendScreen");
m_screenrecord.SetIP = (pSetIP)GetProcAddress(hDLL, "SetIP");
m_screenrecord.SetEncoder = (pSetEncoder)GetProcAddress(hDLL, "SetEncoder");
m_screenrecord.GetCapMod = (pGetCapMod)GetProcAddress(hDLL, "GetCapMod");
m_screenrecord.GetEncoderID = (pGetEncoderID)GetProcAddress(hDLL, "GetEncoderID");
m_screenrecord.SetHaveCursor = (pSetHaveCursor)GetProcAddress(hDLL, "SetHaveCursor");
m_screenrecord.SetMonitor = (pSetMonitor)GetProcAddress(hDLL, "SetMonitor");
m_screenrecord.SetCapAudioType = (pSetCapAudioType)GetProcAddress(hDLL, "SetCapAudioType");
m_screenrecord.SetVideoProperty = (pSetVideoProperty)GetProcAddress(hDLL, "SetVideoProperty");
m_screenrecord.StartRecord = (pStartRecord)GetProcAddress(hDLL, "StartRecord");
m_screenrecord.StopRecord = (pStopRecord)GetProcAddress(hDLL, "StopRecord");
m_screenrecord.OpenDxgi = (pOpenDxgi)GetProcAddress(hDLL, "OpenDxgi");
m_screenrecord.StartPreview = (pStartPreview)GetProcAddress(hDLL, "StartPreview");
m_screenrecord.StopPreview = (pStopPreview)GetProcAddress(hDLL, "StopPreview");
m_screenrecord.CloseAll = (pCloseAll)GetProcAddress(hDLL, "CloseAll");
m_screenrecord.GetMonitorCount = (pGetMonitorCount)GetProcAddress(hDLL, "GetMonitorCount");
m_screenrecord.GetMonitorWidth = (pGetMonitorWidth)GetProcAddress(hDLL, "GetMonitorWidth");
m_screenrecord.GetMonitorHeight = (pGetMonitorHeight)GetProcAddress(hDLL, "GetMonitorHeight");
m_screenrecord.SetEncoderName = (pSetEncoderName)GetProcAddress(hDLL, "SetEncoderName");
m_screenrecord.IsRecording = (pIsRecording)GetProcAddress(hDLL, "IsRecording");
m_screenrecord.UserKey("test","test");
//m_screenrecord.SetHaveCursor(0);
int nMonitor=this->GetCheckedRadioButton(IDC_RADIO4,IDC_RADIO5)-IDC_RADIO4;
m_screenrecord.SetMonitor(nMonitor);
m_screenrecord.OpenDxgi();
m_screenrecord.StartPreview((int)m_vdlg.m_hWnd);
}
return true;
}
void CScreenShareDlg::OnBnClickedOk()
{
if(m_recmin.GetChecked())
{
this->ShowWindow(SW_HIDE);
ShowInTaskbar(this->GetSafeHwnd(),0);
}
int nCapAudioType=this->GetCheckedRadioButton(IDC_RADIO1,IDC_RADIO3)-IDC_RADIO1;
int nMonitor=this->GetCheckedRadioButton(IDC_RADIO4,IDC_RADIO5)-IDC_RADIO4;
int nOutFPS=this->GetCheckedRadioButton(IDC_RADIO10,IDC_RADIO12)-IDC_RADIO10;
int nOutKBPS=this->GetCheckedRadioButton(IDC_RADIO6,IDC_RADIO9)-IDC_RADIO6;
int nEncodeType=m_encodetype.GetChecked();
if(m_screenrecord.SetEncoderName)
m_screenrecord.SetEncoderName(nEncodeType?"":"libx264");
m_screenrecord.SetCapAudioType(nCapAudioType);
m_screenrecord.SetMonitor(nMonitor);
m_screenrecord.SetVideoProperty(OutKBPS[nOutKBPS]*1024*1024,OutFPS[nOutFPS],OutFPS[nOutFPS]*3);
m_screenrecord.StartRecord("c:\\mp4.mp4","c:\\flv.flv");
int n0=m_screenrecord.GetCapMod();
int n1=m_screenrecord.GetEncoderID();
n1=n1;
m_nStartTime=::GetTickCount();
}
C#调用示例:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices; // DLL support
namespace DXGIRecordTest
{
public partial class Form1 : Form
{
[DllImport("user32.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetProcessDPIAware();
//导入xScreenRecord.dll
[DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void UserKey([In, Out] string user, [In, Out] string key);
[DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void StartRecord([In, Out] string szMp4, [In, Out] string szFLv);
[DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void StopRecord();
[DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetHaveCursor([In, Out] int nHave);
[DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetCapAudioType([In, Out] int nCapAudioType);
[DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetMonitor([In, Out] int nMonitorIndex);
[DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetVideoProperty([In, Out] int bps, [In, Out] int fps, [In, Out] int gop);
[DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int OpenDxgi();
[DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void StartPreview([In, Out] int hWnd);
[DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void StopPreview();
[DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int GetMonitorCount();
[DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int GetMonitorWidth(int nMonitor);
[DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int GetMonitorHeight(int nMonitor);
[DllImport("xScreenRecord.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void CloseAll();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SetProcessDPIAware();
UserKey("test", "test");
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
checkBox1.Checked = true;
OpenDxgi();
StartPreview((int)pictureBox1.Handle);
}
private void button1_Click(object sender, EventArgs e)//录像
{
comboBox2.Enabled = false;
SetCapAudioType(comboBox1.SelectedIndex);//0麦克风 1计算机 2麦克风+计算机
SetMonitor(comboBox2.SelectedIndex);//显示器序号
SetHaveCursor(checkBox1.Checked==true?1:0);//是否录制鼠标
SetVideoProperty(Convert.ToInt32(bps.Text)*1024*8, Convert.ToInt32(fps.Text), Convert.ToInt32(gop.Text));
StartRecord("c:\\mp4.mp4","c:\\flv.flv");
}
private void button2_Click(object sender, EventArgs e)//停止
{
StopRecord();
comboBox2.Enabled = true;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
CloseAll();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
int cx=GetMonitorWidth(comboBox2.SelectedIndex);
int cy = GetMonitorHeight(comboBox2.SelectedIndex);
label9.Text = comboBox2.Text + cx + "X" + cy;
SetHaveCursor(checkBox1.Checked == true ? 1 : 0);
SetMonitor(comboBox2.SelectedIndex);//显示器序号
OpenDxgi();
StartPreview((int)pictureBox1.Handle);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
comboBox2_SelectedIndexChanged(sender,e);
}
}
}
本文DEMO源码下载:
(91条消息) dxgi桌面屏幕录像(windows屏幕录像,硬件编码,声音捕获,音视频同步)-编解码文档类资源-CSDN文库https://download.csdn.net/download/xjb2006/85109025
QQ35744025 萧萧工作室