在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#图形和图像处理 → C#实时录屏软件 MP4格式

C#实时录屏软件 MP4格式

C#图形和图像处理

下载此实例
  • 开发语言:C#
  • 实例大小:7.69M
  • 下载次数:250
  • 浏览次数:3358
  • 发布时间:2017-11-22
  • 实例类别:C#图形和图像处理
  • 发 布 人:senwj
  • 文件格式:.rar
  • 所需积分:2
 相关标签: C# 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)
        {

        }
    }
}

标签: C# mp4 录屏 软件 实时

实例下载地址

C#实时录屏软件 MP4格式

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 hums 发表于: 2018-02-03 11:17 35
不可以使用

支持(0) 盖楼(回复)

第 2 楼 zhrwolf 发表于: 2018-11-13 17:01 00
我下载了, 不能录屏 ,点开始录制 就提示: MyAccn.McAccess过期了!请与www. OrayCN.com联系。, 然后就不能录, 请不要再下了

支持(0) 盖楼(回复)

第 3 楼 胡太瑞 发表于: 2019-06-20 09:37 26
不可以使用

支持(0) 盖楼(回复)

第 4 楼 胡太瑞 发表于: 2019-06-20 09:37 27
不可以使用

支持(0) 盖楼(回复)

第 5 楼 bwwdlm 发表于: 2020-05-09 09:37 09
我下载了, 不能录屏 ,点开始录制 就提示: MyAccn.McAccess过期了!请与www. OrayCN.com联系。, 然后就不能录, 请不要再下了

zhrwolf 2018-11-13 17:01 00

是的 这个MCapture也不知道是啥 到期了

支持(0) 盖楼(回复)

第 6 楼 历在今生 发表于: 2020-06-23 09:50 54
不可以用,还扣分了

支持(0) 盖楼(回复)

第 7 楼 乖乖宝贝 发表于: 2021-01-08 20:39 13
连广告都算不上,就是低智商程序员干的活

支持(0) 盖楼(回复)

第 8 楼 tslc 发表于: 2022-02-11 15:05 16
无法运行!

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有8条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警