实例介绍
【实例简介】实时录制视频为mp4格式的视频,不借助外部程序
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using ESBasic; using Oraycn.MCapture; using Oraycn.MFile; namespace Oraycn.RecordDemo { /* * 本demo采用的是 语音视频采集组件MCapture 和 语音视频录制组件MFile 的免费版本。若想获取MCapture、MFile的其它版本,请联系 www.oraycn.com ,客服qq:168757008。 * */ public partial class Form1 : Form { private ISoundcardCapturer soundcardCapturer; private IMicrophoneCapturer microphoneCapturer; private IDesktopCapturer desktopCapturer; private ICameraCapturer cameraCapturer; private IAudioMixter audioMixter; private VideoFileMaker videoFileMaker; private SilenceVideoFileMaker silenceVideoFileMaker; private AudioFileMaker audioFileMaker; private int frameRate = 10; // 采集视频的帧频 private bool sizeRevised = false;// 是否需要将图像帧的长宽裁剪为4的整数倍 private bool isRecording = false; private bool isParsing = false; private Timer timer; private int seconds = 0; private bool justRecordVideo = false; private bool justRecordAudio = false; public Form1() { InitializeComponent(); Oraycn.MCapture.GlobalUtil.SetAuthorizedUser("FreeUser", ""); Oraycn.MFile.GlobalUtil.SetAuthorizedUser("FreeUser", ""); this.timer = new Timer(); this.timer.Interval = 1000; this.timer.Tick = new EventHandler(timer_Tick); } void timer_Tick(object sender, EventArgs e) { if (this.isRecording && !this.isParsing) { var ts = new TimeSpan(0, 0, seconds); this.label_time.Text = ts.Hours.ToString("00") ":" ts.Minutes.ToString("00") ":" ts.Seconds.ToString("00"); } } #region 开始录制 private void button_start_Click(object sender, EventArgs e) { //TODO 开始录制桌面,依据 声音复选框 来选择使用 声卡 麦克风 还是混合录制, 图像复选框来选择 图像的采集器 try { int audioSampleRate = 16000; int channelCount = 1; seconds = 0; System.Drawing.Size videoSize = Screen.PrimaryScreen.Bounds.Size; this.justRecordAudio = this.radioButton_justAudio.Checked; if (this.justRecordAudio && !this.checkBox_micro.Checked && !this.checkBox_soundCard.Checked) { MessageBox.Show("一定要选择一个声音的采集源!"); return; } #region 设置采集器 if (this.radioButton_desktop.Checked) { //桌面采集器 //如果需要录制鼠标的操作,第二个参数请设置为true this.desktopCapturer = CapturerFactory.CreateDesktopCapturer(frameRate, false); this.desktopCapturer.ImageCaptured = this.Form1_ImageCaptured; videoSize = this.desktopCapturer.VideoSize; } else if (this.radioButton_camera.Checked) { //摄像头采集器 videoSize = new System.Drawing.Size(int.Parse(this.textBox_width.Text), int.Parse(this.textBox_height.Text)); this.cameraCapturer = CapturerFactory.CreateCameraCapturer(0, videoSize, frameRate); this.cameraCapturer.ImageCaptured = new CbGeneric<Bitmap>(this.Form1_ImageCaptured); } if (this.checkBox_micro.Checked) { //麦克风采集器 this.microphoneCapturer = CapturerFactory.CreateMicrophoneCapturer(0); this.microphoneCapturer.CaptureError = new CbGeneric<Exception>(this.CaptureError); } if (this.checkBox_soundCard.Checked) { //声卡采集器 【目前声卡采集仅支持vista以及以上系统】 this.soundcardCapturer = CapturerFactory.CreateSoundcardCapturer(); this.soundcardCapturer.CaptureError = this.CaptureError; audioSampleRate = this.soundcardCapturer.SampleRate; channelCount = this.soundcardCapturer.ChannelCount; } if (this.checkBox_micro.Checked && this.checkBox_soundCard.Checked) { //混音器 this.audioMixter = CapturerFactory.CreateAudioMixter(this.microphoneCapturer, this.soundcardCapturer, SoundcardMode4Mix.DoubleChannel, true); this.audioMixter.AudioMixed = audioMixter_AudioMixed; audioSampleRate = this.audioMixter.SampleRate; channelCount = this.audioMixter.ChannelCount; } else if (this.checkBox_micro.Checked) { this.microphoneCapturer.AudioCaptured = audioMixter_AudioMixed; } else if (this.checkBox_soundCard.Checked) { this.soundcardCapturer.AudioCaptured = audioMixter_AudioMixed; } #endregion #region //开始采集 if (this.checkBox_micro.Checked) { this.microphoneCapturer.Start(); } if (this.checkBox_soundCard.Checked) { this.soundcardCapturer.Start(); } if (this.radioButton_camera.Checked) { this.cameraCapturer.Start(); } else if (this.radioButton_desktop.Checked) { this.desktopCapturer.Start(); } #endregion #region //录制组件 if (this.justRecordAudio) { //只录制声音 this.audioFileMaker = new AudioFileMaker(); this.audioFileMaker.Initialize("test.mp3", audioSampleRate, channelCount); } else { this.sizeRevised = (videoSize.Width % 4 != 0) || (videoSize.Height % 4 != 0); if (this.sizeRevised) { videoSize = new System.Drawing.Size(videoSize.Width / 4 * 4, videoSize.Height / 4 * 4); } if (!this.checkBox_micro.Checked && !this.checkBox_soundCard.Checked) { //只录制 图像 this.justRecordVideo = true; this.silenceVideoFileMaker = new SilenceVideoFileMaker(); this.silenceVideoFileMaker.Initialize("test.mp4", VideoCodecType.H264, videoSize.Width, videoSize.Height, frameRate, VideoQuality.High); } else { // 录制声音和图像 this.justRecordVideo = false; this.videoFileMaker = new VideoFileMaker(); this.videoFileMaker.Initialize("test.mp4", VideoCodecType.H264, videoSize.Width, videoSize.Height, frameRate, VideoQuality.High, AudioCodecType.AAC, audioSampleRate, channelCount, true); } } #endregion this.isRecording = true; this.isParsing = false; this.timer.Start(); this.checkBox_micro.Enabled = false; this.checkBox_soundCard.Enabled = false; this.radioButton_desktop.Enabled = false; this.radioButton_camera.Enabled = false; this.radioButton_justAudio.Enabled = false; this.button_start.Enabled = false; } catch (Exception ee) { MessageBox.Show(ee.Message); } } #region CaptureError void CaptureError(Exception obj) { } #endregion #region Form1_ImageCaptured private int imageCount = 0; //采集到的视频或桌面图像 void Form1_ImageCaptured(Bitmap img) { if (this.isRecording && !this.isParsing) { //这里要裁剪 Bitmap imgRecorded = img; if (this.sizeRevised) // 对图像进行裁剪, MFile要求录制的视频帧的长和宽必须是4的整数倍。 { imgRecorded = ESBasic.Helpers.ImageHelper.RoundSizeByNumber(img, 4); img.Dispose(); } if (!this.justRecordVideo) { this.videoFileMaker.AddVideoFrame(imgRecorded); } else { this.silenceVideoFileMaker.AddVideoFrame(imgRecorded); } } } #endregion #region audioMixter_AudioMixed void audioMixter_AudioMixed(byte[] audioData) { if (this.isRecording && !this.isParsing) { if (this.justRecordAudio) { this.audioFileMaker.AddAudioFrame(audioData); } else { if (!this.justRecordVideo) { this.videoFileMaker.AddAudioFrame(audioData); } } } } #endregion #endregion #region 暂停 private void button_parse_Click(object sender, EventArgs e) { //TODO 暂停当前录制或恢复录制 //TODO label 中显示实际录制的时间,需要考虑暂停和恢复这种情况。 格式为 hh:mm:ss if (this.isParsing) { this.isParsing = false; } else { this.isParsing = true; } this.button_parse.Text = (!this.isParsing ? "暂停" : "恢复"); } #endregion #region 结束录制 private void button_stop_Click(object sender, EventArgs e) { this.checkBox_micro.Enabled = true; this.checkBox_soundCard.Enabled = true; this.radioButton_camera.Enabled = true; this.radioButton_desktop.Enabled = true; this.radioButton_justAudio.Enabled = true; this.button_start.Enabled = true; this.button_parse.Text = "暂停"; if (this.checkBox_micro.Checked) // 麦克风 { this.microphoneCapturer.Stop(); } if (this.checkBox_soundCard.Checked) //声卡 { this.soundcardCapturer.Stop(); } if (this.radioButton_camera.Checked) { this.cameraCapturer.Stop(); } if (this.radioButton_desktop.Checked) { this.desktopCapturer.Stop(); } if (this.justRecordAudio) { this.audioFileMaker.Close(true); } else { if (!this.justRecordVideo) { this.videoFileMaker.Close(true); } else { this.silenceVideoFileMaker.Close(true); } } this.isRecording = false; MessageBox.Show("录制完成!"); } #endregion private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (this.isRecording) { MessageBox.Show("正在录制中,退出前请先停止录制!"); e.Cancel = true; return; } e.Cancel = false; } private void radioButton_camera_CheckedChanged(object sender, EventArgs e) { this.panel_camera.Visible = true; } private void radioButton_desktop_CheckedChanged(object sender, EventArgs e) { this.panel_camera.Visible = false; } private void radioButton_justAudio_CheckedChanged(object sender, EventArgs e) { this.panel_camera.Visible = false; } private void label5_Click(object sender, EventArgs e) { } } }
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)