2014-05-08 21:06:53 +12:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2014-12-31 22:41:32 +13:00
|
|
|
|
Copyright © 2007-2015 ShareX Developers
|
2014-05-08 21:06:53 +12: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;
|
|
|
|
|
using ShareX.ScreenCaptureLib.Properties;
|
2014-05-08 21:06:53 +12:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2014-05-12 10:58:10 +12:00
|
|
|
|
using System.Text.RegularExpressions;
|
2014-05-08 21:06:53 +12:00
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.ScreenCaptureLib
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
2014-05-10 20:06:43 +12:00
|
|
|
|
public class FFmpegHelper : ExternalCLIManager
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
2014-05-15 14:08:47 +12:00
|
|
|
|
public static readonly int libmp3lame_qscale_end = 9;
|
2014-08-29 20:22:51 +12:00
|
|
|
|
public static readonly string SourceNone = "None";
|
|
|
|
|
public static readonly string SourceGDIGrab = "GDI grab";
|
2014-05-15 14:08:47 +12:00
|
|
|
|
|
2014-05-22 13:22:32 +12:00
|
|
|
|
public StringBuilder Output { get; private set; }
|
2014-05-09 01:28:46 +12:00
|
|
|
|
public ScreencastOptions Options { get; private set; }
|
2014-05-08 21:06:53 +12:00
|
|
|
|
|
2014-05-10 20:06:43 +12:00
|
|
|
|
public FFmpegHelper(ScreencastOptions options)
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
2014-05-22 13:22:32 +12:00
|
|
|
|
Output = new StringBuilder();
|
|
|
|
|
OutputDataReceived += FFmpegHelper_DataReceived;
|
|
|
|
|
ErrorDataReceived += FFmpegHelper_DataReceived;
|
2014-05-08 21:06:53 +12:00
|
|
|
|
Options = options;
|
|
|
|
|
Helpers.CreateDirectoryIfNotExist(Options.OutputPath);
|
2014-05-09 11:08:30 +12:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-22 13:22:32 +12:00
|
|
|
|
private void FFmpegHelper_DataReceived(object sender, DataReceivedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(e.Data))
|
|
|
|
|
{
|
|
|
|
|
Output.AppendLine(e.Data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 21:31:10 +12:00
|
|
|
|
public bool Record()
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
2015-06-03 23:32:34 +12:00
|
|
|
|
return Run(Options.FFmpeg.CLIPath, Options.GetFFmpegCommands());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool EncodeGIF(string input, string output)
|
|
|
|
|
{
|
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
|
|
string palettePath = Path.Combine(Path.GetDirectoryName(Options.FFmpeg.CLIPath), "palette.png");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-06-09 06:19:30 +12:00
|
|
|
|
// https://ffmpeg.org/ffmpeg-filters.html#palettegen-1
|
|
|
|
|
result = Run(Options.FFmpeg.CLIPath, string.Format("-i \"{0}\" -vf \"palettegen=stats_mode={2}\" -y \"{1}\"", input, palettePath, Options.FFmpeg.GIFStatsMode));
|
2015-06-03 23:32:34 +12:00
|
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
2015-06-09 06:19:30 +12:00
|
|
|
|
// https://ffmpeg.org/ffmpeg-filters.html#paletteuse
|
|
|
|
|
result = Run(Options.FFmpeg.CLIPath, string.Format("-i \"{0}\" -i \"{1}\" -lavfi \"paletteuse=dither={3}\" -y \"{2}\"", input, palettePath, output, Options.FFmpeg.GIFDither));
|
2015-06-03 23:32:34 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(palettePath))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(palettePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool Run(string path, string args = null)
|
|
|
|
|
{
|
|
|
|
|
int errorCode = Open(path, args);
|
2014-05-13 21:49:34 +12:00
|
|
|
|
bool result = errorCode == 0;
|
|
|
|
|
if (Options.FFmpeg.ShowError && !result)
|
|
|
|
|
{
|
2014-10-18 00:08:33 +13:00
|
|
|
|
new OutputBox(Output.ToString(), Resources.FFmpegHelper_Record_FFmpeg_error).ShowDialog();
|
2014-05-13 21:49:34 +12:00
|
|
|
|
}
|
|
|
|
|
return result;
|
2014-05-08 21:06:53 +12:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-13 18:57:09 +12:00
|
|
|
|
public DirectShowDevices GetDirectShowDevices()
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
2014-05-13 18:57:09 +12:00
|
|
|
|
DirectShowDevices devices = new DirectShowDevices();
|
|
|
|
|
|
|
|
|
|
if (File.Exists(Options.FFmpeg.CLIPath))
|
|
|
|
|
{
|
|
|
|
|
string arg = "-list_devices true -f dshow -i dummy";
|
|
|
|
|
Open(Options.FFmpeg.CLIPath, arg);
|
2014-05-22 13:22:32 +12:00
|
|
|
|
string output = Output.ToString();
|
2014-05-13 18:57:09 +12:00
|
|
|
|
string[] lines = output.Lines();
|
|
|
|
|
bool isVideo = true;
|
|
|
|
|
Regex regex = new Regex("\\[dshow @ \\w+\\] \"(.+)\"", RegexOptions.Compiled | RegexOptions.CultureInvariant);
|
|
|
|
|
foreach (string line in lines)
|
|
|
|
|
{
|
|
|
|
|
if (line.EndsWith("] DirectShow video devices", StringComparison.InvariantCulture))
|
|
|
|
|
{
|
|
|
|
|
isVideo = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-10-19 10:48:47 +13:00
|
|
|
|
|
|
|
|
|
if (line.EndsWith("] DirectShow audio devices", StringComparison.InvariantCulture))
|
2014-05-13 18:57:09 +12:00
|
|
|
|
{
|
|
|
|
|
isVideo = false;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Match match = regex.Match(line);
|
|
|
|
|
|
|
|
|
|
if (match.Success)
|
|
|
|
|
{
|
|
|
|
|
string value = match.Groups[1].Value;
|
|
|
|
|
|
|
|
|
|
if (isVideo)
|
|
|
|
|
{
|
|
|
|
|
devices.VideoDevices.Add(value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
devices.AudioDevices.Add(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return devices;
|
2014-05-08 21:06:53 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Close()
|
|
|
|
|
{
|
2014-05-09 11:08:30 +12:00
|
|
|
|
WriteInput("q");
|
2014-05-08 21:06:53 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-13 18:57:09 +12:00
|
|
|
|
|
|
|
|
|
public class DirectShowDevices
|
|
|
|
|
{
|
|
|
|
|
public List<string> VideoDevices = new List<string>();
|
|
|
|
|
public List<string> AudioDevices = new List<string>();
|
|
|
|
|
}
|
2014-05-08 21:06:53 +12:00
|
|
|
|
}
|