基本信息
源码名称:C#实时录屏软件 MP4格式
源码大小:7.69M
文件格式:.rar
开发语言:C#
更新时间:2017-11-22
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
实时录制视频为mp4格式的视频,不借助外部程序
实时录制视频为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)
{
}
}
}