本文主要是介绍创业第四天——录屏软件雏形,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
从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);
}
}
}
这篇关于创业第四天——录屏软件雏形的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!