ShareX/ShareX.ScreenCaptureLib/Screencast/ScreenRecorder.cs

269 lines
7.6 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
2019-01-02 20:43:52 +13:00
Copyright (c) 2007-2019 ShareX Team
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)
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib;
2013-11-03 23:53:49 +13:00
using System;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
2014-12-11 09:25:20 +13:00
namespace ShareX.ScreenCaptureLib
2013-11-03 23:53:49 +13:00
{
public class ScreenRecorder : IDisposable
{
public bool IsRecording { get; private 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; }
public event Action RecordingStarted;
2013-11-03 23:53:49 +13:00
public delegate void ProgressEventHandler(int progress);
2013-11-03 23:53:49 +13:00
public event ProgressEventHandler EncodingProgressChanged;
private int fps, delay, frameCount, previousProgress;
2013-11-03 23:53:49 +13:00
private float durationSeconds;
2016-07-22 02:23:45 +12:00
private Screenshot screenshot;
2013-11-03 23:53:49 +13:00
private Rectangle captureRectangle;
private ImageCache imgCache;
private FFmpegHelper ffmpegCli;
2013-11-03 23:53:49 +13:00
private bool stopRequest;
2016-07-22 02:23:45 +12:00
public ScreenRecorder(ScreenRecordOutput outputType, ScreencastOptions options, Screenshot screenshot, Rectangle captureRectangle)
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
{
2014-10-25 06:36:06 +13:00
throw new Exception("Screen recorder cache path is empty.");
2013-11-03 23:53:49 +13:00
}
2018-10-02 07:14:32 +13:00
FPS = options.FPS;
2014-05-09 15:52:53 +12:00
DurationSeconds = options.Duration;
2013-11-03 23:53:49 +13:00
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
{
default:
case ScreenRecordOutput.FFmpeg:
ffmpegCli = new FFmpegHelper(Options);
ffmpegCli.RecordingStarted += OnRecordingStarted;
2014-05-09 15:14:53 +12:00
break;
case ScreenRecordOutput.GIF:
imgCache = new HardDiskCache(Options);
break;
2013-11-03 23:53:49 +13:00
}
2016-07-22 02:23:45 +12:00
this.screenshot = screenshot;
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.FFmpeg)
2013-11-03 23:53:49 +13:00
{
ffmpegCli.Record();
}
else
{
OnRecordingStarted();
RecordUsingCache();
}
}
IsRecording = false;
}
private void RecordUsingCache()
{
try
{
for (int i = 0; !stopRequest && (frameCount == 0 || i < frameCount); i++)
{
Stopwatch timer = Stopwatch.StartNew();
2016-07-22 02:23:45 +12:00
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
{
Helpers.CreateDirectoryFromFilePath(path);
2015-06-03 23:32:34 +12:00
HardDiskCache hdCache = imgCache as HardDiskCache;
using (AnimatedGifCreator gifEncoder = new AnimatedGifCreator(path, delay))
2013-11-03 23:53:49 +13:00
{
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);
}
}
}
}
}
2018-10-02 07:14:32 +13:00
public bool FFmpegEncodeVideo(string input, string output)
{
Helpers.CreateDirectoryFromFilePath(output);
return ffmpegCli.EncodeVideo(input, output);
2018-10-02 07:14:32 +13:00
}
public bool FFmpegEncodeAsGIF(string input, string output)
2015-06-03 23:32:34 +12:00
{
Helpers.CreateDirectoryFromFilePath(output);
return ffmpegCli.EncodeGIF(input, output);
2015-06-03 23:32:34 +12:00
}
protected void OnRecordingStarted()
{
if (RecordingStarted != null)
{
RecordingStarted();
}
}
2013-11-03 23:53:49 +13:00
protected void OnEncodingProgressChanged(int progress)
{
if (EncodingProgressChanged != null && progress != previousProgress)
2013-11-03 23:53:49 +13:00
{
EncodingProgressChanged(progress);
previousProgress = progress;
2013-11-03 23:53:49 +13:00
}
}
public void Dispose()
{
if (imgCache != null)
2013-11-03 23:53:49 +13:00
{
imgCache.Dispose();
2013-11-03 23:53:49 +13:00
}
}
}
}