基于Microsoft Expression Encoder 4 SDK的屏幕录像

本文介绍如何使用Microsoft Expression Encoder SDK实现屏幕录像,并详细解释了如何将录制的xesc文件转换为常见的视频格式,包括定义视频和音频格式、设置编码参数及最终的文件保存路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近由于项目需求,需要屏幕录像功能。

经过查找资料发现Microsoft Expression Encoder 4 SDK提供了屏幕录像的功能,并简单实现了该功能。

 

主要实现代码

  1 using System;
  2 using System.Collections.ObjectModel;
  3 using System.Drawing;
  4 using System.IO;
  5 using System.Windows.Forms;
  6 using Microsoft.Expression.Encoder;
  7 using Microsoft.Expression.Encoder.Devices;
  8 using Microsoft.Expression.Encoder.Profiles;
  9 using Microsoft.Expression.Encoder.ScreenCapture;
 10 
 11 namespace ScreenCapture
 12 {
 13     public partial class Form1 : Form
 14     {
 15         public Form1()
 16         {
 17             InitializeComponent();
 18         }
 19 
 20         //定义屏幕截图job
 21         ScreenCaptureJob screencapturejob = new ScreenCaptureJob();
 22         //设置临时目录
 23          string path = "c:\\"+Guid.NewGuid().ToString();
 24 
 25         /// <summary>
 26         /// 开始录制
 27         /// </summary>
 28         /// <param name="sender"></param>
 29         /// <param name="e"></param>
 30         private void button1_Click(object sender, EventArgs e)
 31         {
 32             screencapturejob.OutputPath = path;
 33             Collection<EncoderDevice> devices = EncoderDevices.FindDevices(EncoderDeviceType.Audio);//查询当前所有音频设备
 34             foreach (EncoderDevice device in devices)
 35             {
 36                 try
 37                 {
 38                     screencapturejob.AddAudioDeviceSource(device);//将音频设备的声音记录
 39                 }
 40                 catch { 
 41                 //抛出其他异常的音频设备
 42                 }
 43             }
 44             //录制开始
 45             screencapturejob.Start();
 46         }
 47 
 48         /// <summary>
 49         /// 停止录制
 50         /// </summary>
 51         /// <param name="sender"></param>
 52         /// <param name="e"></param>
 53         private void button2_Click(object sender, EventArgs e)
 54         {
 55             label1.Text = "正在处理...";
 56             screencapturejob.Stop();//停止录制job
 57             string xescfile = GetxescPath();//获取录制文件路径
 58             MediaItem src = new MediaItem(xescfile);//加入转换媒体
 59 
 60             //获取屏幕信息
 61             Rectangle rect = new Rectangle();
 62             rect =Screen.PrimaryScreen.Bounds;
 63            // Screen.AllScreens
 64 
 65             //重新定义视频格式
 66             src.OutputFormat = new WindowsMediaOutputFormat();
 67             src.OutputFormat.VideoProfile = new AdvancedVC1VideoProfile();
 68             src.OutputFormat.VideoProfile.Bitrate = new VariableConstrainedBitrate(1000, 1500);
 69             src.OutputFormat.VideoProfile.Size = new Size(rect.Width, rect.Height);
 70             src.OutputFormat.VideoProfile.FrameRate = 30;
 71             src.OutputFormat.VideoProfile.KeyFrameDistance = new TimeSpan(0, 0, 4);
 72 
 73             //重新定义音频格式
 74             src.OutputFormat.AudioProfile = new WmaAudioProfile();
 75             src.OutputFormat.AudioProfile.Bitrate = new VariableConstrainedBitrate(128, 192);
 76             src.OutputFormat.AudioProfile.Codec = AudioCodec.WmaProfessional;
 77             src.OutputFormat.AudioProfile.BitsPerSample = 24;
 78 
 79             Job encoderjob = new Job();//实例化转换作业
 80             encoderjob.MediaItems.Add(src);//添加xesc文件
 81             //encoderjob.ApplyPreset(Presets.VC1HD720pVBR);//设置视频编码
 82             encoderjob.CreateSubfolder = false;//不创建文件夹
 83             encoderjob.OutputDirectory = "C:\\output";//设置转换完后的文件保存目录
 84             encoderjob.EncodeCompleted += job2_EncodeCompleted;
 85 
 86             encoderjob.Encode();
 87 
 88             if (File.Exists(xescfile)) File.Delete(xescfile);
 89             if (Directory.Exists(path)) Directory.Delete(path);
 90         }
 91 
 92         void job2_EncodeCompleted(object sender, EncodeCompletedEventArgs e)
 93         {
 94             label1.Text = "处理完成...";
 95         }
 96 
 97         private string GetxescPath()
 98         {
 99             string result = "";
100             FileInfo[] filelist = new DirectoryInfo(path).GetFiles("*.xesc");
101             foreach (FileInfo NextFile in filelist)
102             {
103                 result = NextFile.FullName;
104                 break;
105             }
106             return result;
107         }
108     }
109 }

 

 

源代码:ScreenCapture.7z

本源码为VS2012项目,需要.net framework 4.0支持。

如果编译失败,请重新引用Microsoft.Expression.Encoder.dll,Microsoft.Expression.Encoder.Types.dll,Microsoft.Expression.Encoder.Utilities.dll

转载于:https://www.cnblogs.com/yuanlin/p/3270204.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值