最近由於項目需求,需要屏幕錄像功能。
經過查找資料發現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
