ShareX/ScreenCaptureLib/ScreenRecorder.cs

254 lines
7.4 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2014-01-12 20:25:51 +13:00
Copyright (C) 2008-2014 ShareX Developers
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using HelpersLib;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Threading;
namespace ScreenCaptureLib
2013-11-03 23:53:49 +13:00
{
public class ScreenRecorder : IDisposable
{
public bool IsRecording { get; private set; }
public bool WriteCompressed { get; set; }
public int FPS
{
get
{
return fps;
}
set
{
if (!IsRecording)
{
fps = value;
UpdateInfo();
}
}
}
public float DurationSeconds
{
get
{
return durationSeconds;
}
set
{
if (!IsRecording)
{
durationSeconds = value;
UpdateInfo();
}
}
}
public Rectangle CaptureRectangle
{
get
{
return captureRectangle;
}
private set
{
if (!IsRecording)
{
captureRectangle = value;
}
}
}
public string CachePath { get; private set; }
public ScreenRecordOutput OutputType { get; private set; }
public AVIOptions Options { get; set; }
2013-11-03 23:53:49 +13:00
public delegate void ProgressEventHandler(int progress);
public event ProgressEventHandler EncodingProgressChanged;
private int fps, delay, frameCount;
private float durationSeconds;
private Rectangle captureRectangle;
private HardDiskCache hdCache;
private AVICache aviCache;
private bool stopRequest;
public ScreenRecorder(int fps, float durationSeconds, Rectangle captureRectangle, string cachePath, ScreenRecordOutput outputType, AVICOMPRESSOPTIONS compressOptions)
2013-11-03 23:53:49 +13:00
{
if (string.IsNullOrEmpty(cachePath))
{
throw new Exception("Screen recorder cache path is empty.");
}
FPS = fps;
DurationSeconds = durationSeconds;
CaptureRectangle = captureRectangle;
CachePath = cachePath;
OutputType = outputType;
if (OutputType == ScreenRecordOutput.AVI || OutputType == ScreenRecordOutput.AVICommandLine)
{
Options = new AVIOptions
{
CompressOptions = compressOptions,
FPS = FPS,
OutputPath = CachePath,
Size = CaptureRectangle.Size
};
aviCache = new AVICache(Options);
2013-11-03 23:53:49 +13:00
}
else if (OutputType == ScreenRecordOutput.GIF)
{
hdCache = new HardDiskCache(CachePath);
}
}
private void UpdateInfo()
{
delay = 1000 / fps;
frameCount = (int)(fps * durationSeconds);
}
public void StartRecording()
{
if (!IsRecording)
{
IsRecording = true;
stopRequest = false;
2014-04-27 19:07:43 +12:00
for (int i = 0; !stopRequest && (frameCount == 0 || i < frameCount); i++)
2013-11-03 23:53:49 +13:00
{
Stopwatch timer = Stopwatch.StartNew();
2014-04-27 19:07:43 +12:00
2013-11-03 23:53:49 +13:00
Image img = Screenshot.CaptureRectangle(CaptureRectangle);
2014-04-27 19:07:43 +12:00
//DebugHelper.WriteLine("Screen capture: " + (int)timer.ElapsedMilliseconds);
2013-11-03 23:53:49 +13:00
if (OutputType == ScreenRecordOutput.AVI || OutputType == ScreenRecordOutput.AVICommandLine)
{
aviCache.AddImageAsync(img);
}
else if (OutputType == ScreenRecordOutput.GIF)
{
hdCache.AddImageAsync(img);
}
2014-04-27 19:07:43 +12:00
if (!stopRequest && (frameCount == 0 || i + 1 < frameCount))
2013-11-03 23:53:49 +13:00
{
int sleepTime = delay - (int)timer.ElapsedMilliseconds;
if (sleepTime > 0)
{
Thread.Sleep(sleepTime);
}
2014-04-27 19:07:43 +12:00
else if (sleepTime < 0)
2013-11-03 23:53:49 +13:00
{
2014-04-27 19:07:43 +12:00
//DebugHelper.WriteLine("FPS drop: " + -sleepTime);
2013-11-03 23:53:49 +13:00
}
}
}
if (OutputType == ScreenRecordOutput.AVI || OutputType == ScreenRecordOutput.AVICommandLine)
{
aviCache.Finish();
}
else if (OutputType == ScreenRecordOutput.GIF)
{
hdCache.Finish();
}
}
IsRecording = false;
}
public void StopRecording()
{
stopRequest = true;
}
public void SaveAsGIF(string path, GIFQuality quality)
{
if (!IsRecording)
{
using (GifCreator gifEncoder = new GifCreator(delay))
{
int i = 0;
int count = hdCache.Count;
foreach (Image img in hdCache.GetImageEnumerator())
{
i++;
OnEncodingProgressChanged((int)((float)i / count * 100));
using (img)
{
gifEncoder.AddFrame(img, quality);
}
}
gifEncoder.Finish();
gifEncoder.Save(path);
}
}
}
public void EncodeUsingCommandLine(VideoEncoder encoder, string targetFilePath)
2013-11-03 23:53:49 +13:00
{
if (!string.IsNullOrEmpty(CachePath) && File.Exists(CachePath))
2013-11-03 23:53:49 +13:00
{
OnEncodingProgressChanged(-1);
encoder.Encode(CachePath, targetFilePath);
2013-11-03 23:53:49 +13:00
OnEncodingProgressChanged(100);
}
}
protected void OnEncodingProgressChanged(int progress)
{
if (EncodingProgressChanged != null)
{
EncodingProgressChanged(progress);
}
}
public void Dispose()
{
if (hdCache != null)
{
hdCache.Dispose();
}
if (aviCache != null)
{
aviCache.Dispose();
}
}
}
}