ShareX/ScreenCaptureLib/Screencast/ScreenRecorder.cs

263 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; }
2014-05-09 01:28:46 +12:00
public ScreencastOptions 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 ImageCache imgCache;
private FFmpegCLIHelper ffMpegCli;
2013-11-03 23:53:49 +13:00
private bool stopRequest;
2014-05-09 01:28:46 +12:00
public ScreenRecorder(ScreencastOptions options, float durationSeconds, Rectangle captureRectangle, ScreenRecordOutput outputType)
2013-11-03 23:53:49 +13:00
{
2014-05-09 01:28:46 +12:00
if (string.IsNullOrEmpty(options.OutputPath))
2013-11-03 23:53:49 +13:00
{
throw new Exception("Screen recorder cache path is empty.");
}
2014-05-09 01:28:46 +12:00
FPS = options.FPS;
2013-11-03 23:53:49 +13:00
DurationSeconds = durationSeconds;
CaptureRectangle = captureRectangle;
2014-05-09 01:28:46 +12:00
CachePath = options.OutputPath;
2013-11-03 23:53:49 +13:00
OutputType = outputType;
2014-05-09 01:28:46 +12:00
Options = options;
switch (OutputType)
2013-11-03 23:53:49 +13:00
{
case ScreenRecordOutput.AVI:
imgCache = new AVICache(Options);
break;
case ScreenRecordOutput.FFmpegNet:
imgCache = new FFmpegCache(Options);
break;
case ScreenRecordOutput.GIF:
imgCache = new HardDiskCache(Options);
break;
2013-11-03 23:53:49 +13:00
}
}
private void UpdateInfo()
{
delay = 1000 / fps;
frameCount = (int)(fps * durationSeconds);
}
public void StartRecording()
{
if (!IsRecording)
{
IsRecording = true;
stopRequest = false;
if (OutputType == ScreenRecordOutput.FFmpegCLI)
2013-11-03 23:53:49 +13:00
{
RecordUsingFFmpegCLI();
}
else
{
RecordUsingCache();
}
}
IsRecording = false;
}
private void RecordUsingFFmpegCLI()
{
ffMpegCli = new FFmpegCLIHelper(Options);
2014-05-09 13:16:43 +12:00
ffMpegCli.Record(CaptureRectangle);
}
private void RecordUsingCache()
{
try
{
for (int i = 0; !stopRequest && (frameCount == 0 || i < frameCount); i++)
{
Stopwatch timer = Stopwatch.StartNew();
Image img = Screenshot.CaptureRectangle(CaptureRectangle);
//DebugHelper.WriteLine("Screen capture: " + (int)timer.ElapsedMilliseconds);
2013-11-03 23:53:49 +13:00
imgCache.AddImageAsync(img);
2013-11-03 23:53:49 +13:00
if (!stopRequest && (frameCount == 0 || i + 1 < frameCount))
{
int sleepTime = delay - (int)timer.ElapsedMilliseconds;
2013-11-03 23:53:49 +13:00
if (sleepTime > 0)
2013-11-03 23:53:49 +13:00
{
Thread.Sleep(sleepTime);
}
else if (sleepTime < 0)
{
// Need to handle FPS drops
2013-11-03 23:53:49 +13:00
}
}
}
}
finally
{
imgCache.Finish();
}
2013-11-03 23:53:49 +13:00
}
public void StopRecording()
{
stopRequest = true;
if (ffMpegCli != null)
{
ffMpegCli.Close();
}
2013-11-03 23:53:49 +13:00
}
public void SaveAsGIF(string path, GIFQuality quality)
{
if (imgCache != null && imgCache is HardDiskCache && !IsRecording)
2013-11-03 23:53:49 +13:00
{
HardDiskCache hdCache = imgCache as HardDiskCache;
2013-11-03 23:53:49 +13:00
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 sourceFilePath, string targetFilePath)
2013-11-03 23:53:49 +13:00
{
if (!string.IsNullOrEmpty(sourceFilePath) && File.Exists(sourceFilePath))
2013-11-03 23:53:49 +13:00
{
OnEncodingProgressChanged(-1);
encoder.Encode(sourceFilePath, 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 (imgCache != null)
2013-11-03 23:53:49 +13:00
{
imgCache.Dispose();
2013-11-03 23:53:49 +13:00
}
}
}
}