创业第四天——录屏软件雏形

从18号到今天已经四天了,这几天,我们查尽所有可能的资料,终于有一点点的成果了,就是录屏软件的雏形出来了,可以录制一小段视频,但是并没有关闭硬件加速度,并且保存下了截取屏幕时候的图片,也就是帧数对应的图片,由于这些图片是在内存的,所有视频不能录得太长。当然了图片也保存到了磁盘,只是在内存中的缓存没有清除掉。其中我们调用傲瑞科技的组件,在此声明一下。

form1.cs

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
{
    public partial class Form1 : Form
    {
        private ICapturer audioCapturer;
        private ICapturer videoCapturer;
        private VideoFileMaker videoFileMaker;
        ListBitMap list;
        RecordService rs;

        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            combtime.SelectedIndex = 0;
            combselectshengyin.SelectedIndex = 0;
            
        }

        private void selectshengyin_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void buttoncun_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textcun.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void buttonstart_Click(object sender, EventArgs e)
        {
            string stingtime = combtime.Text;

            int time = int.Parse(stingtime);
           
  //          MessageBox.Show(time.ToString());

            list = new ListBitMap(25,time);
            
  //          this.audioCapturer = CapturerFactory.CreateMicrophoneCapturer(0);


            int frameRate = 15;        
    //        this.videoCapturer = CapturerFactory.CreateDesktopCapturer(frameRate, false);

    //        videoFileMaker = new VideoFileMaker();
            rs = new RecordService(audioCapturer, videoCapturer, videoFileMaker, list);

            string fileName = @"C:\test.mp4";
            rs.StartRecord(AudioType.Microphone,25,fileName);
        }

        private void buttonover_Click(object sender, EventArgs e)
        {
            rs.Stop();
            MessageBox.Show(list.listBitMap.Count.ToString());
            
            for (int i = 0; i < list.listBitMap.Count; i++)
            {
                list.listBitMap[i].Save(@"C:\img\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
            }

      //      pbShow.Image = list.listBitMap[20];

            
        }
  
    }
}
RecordService.cs

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
{

    enum AudioType
    {
        Microphone=1,
        Soundcard
    };

    class ListBitMap
    {
        private int maxLength;
        public List<Bitmap> listBitMap;

      
        public ListBitMap( int fps,int time)
        {
            this.listBitMap = new List<Bitmap>();
            this.maxLength = time*fps;
        }
 
        public void Add(Bitmap img)
        {
            if (listBitMap.Count == maxLength)
            {
                listBitMap.RemoveAt(0);
            }
            listBitMap.Add(img);
        }
 
       }
    class RecordService
    {
        private ICapturer audioCapturer;
        private ICapturer videoCapturer;
        private VideoFileMaker videoFileMaker;
        ListBitMap list;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="audioCapturer">音频</param>
        /// <param name="videoCapturer">视频</param>
        /// <param name="videoFileMaker">生成的视频文件</param>
        /// <param name="list">存放图片</param>
        public RecordService(ICapturer audioCapturer,ICapturer videoCapturer,VideoFileMaker videoFileMaker,ListBitMap list)
        {
            this.audioCapturer = audioCapturer;
            this.videoCapturer = videoCapturer;
            this.videoCapturer = videoCapturer;
            this.list = list;
 
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="audioType">声音来源,Microphone=1, Soundcard</param>
        /// <param name="videoFrameRate">视频帧数</param>
        /// <param name="fileName">文件存储路径</param>
        public void StartRecord(AudioType audioType,int videoFrameRate,string fileName)
        {
            int audioSampleRate = 16000;
            int channelCount = 1;
            
            if (audioType == AudioType.Microphone) //麦克风
            {
                this.audioCapturer = CapturerFactory.CreateMicrophoneCapturer(0);
                ((IMicrophoneCapturer)this.audioCapturer).AudioCaptured += new ESBasic.CbGeneric<byte[]>(AudioCaptured);
            }
            else //声卡
            {
                this.audioCapturer = CapturerFactory.CreateSoundcardCapturer();
                ((ISoundcardCapturer)this.audioCapturer).AudioCaptured += new ESBasic.CbGeneric<byte[]>(AudioCaptured);
                audioSampleRate = ((ISoundcardCapturer)this.audioCapturer).SampleRate;
                channelCount = ((ISoundcardCapturer)this.audioCapturer).ChannelCount;
            }
 
            this.videoCapturer = CapturerFactory.CreateDesktopCapturer(videoFrameRate, false);
            ((IDesktopCapturer)this.videoCapturer).ImageCaptured += new ESBasic.CbGeneric<Bitmap>(ImageCaptured);
            Size videoSize = Screen.PrimaryScreen.Bounds.Size;

            this.videoFileMaker = new VideoFileMaker();
            this.videoFileMaker.AutoDisposeVideoFrame = true;
            this.videoFileMaker.Initialize("test.mp4", VideoCodecType.H264, videoSize.Width, videoSize.Height, videoFrameRate, AudioCodecType.AAC, audioSampleRate, channelCount, true);

            //开始采集
            this.audioCapturer.Start();
            this.videoCapturer.Start();
        }



        void  AudioCaptured(byte[] audioData) //采集到的声音数据
        {
            this.videoFileMaker.AddAudioFrame(audioData);
        }

        void ImageCaptured(Bitmap img)
        {
            Bitmap tmp = (Bitmap)img.Clone();
            list.Add(tmp);
            tmp = (Bitmap)img.Clone();
            this.videoFileMaker.AddVideoFrame(tmp);
            if (img != null)
                img.Dispose();
            
        }



        public void Stop()
        {
            //停止采集
            this.audioCapturer.Stop();
            this.videoCapturer.Stop();
            this.videoFileMaker.Close(true);
            
        }
 

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值