-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
83 lines (68 loc) · 3.37 KB
/
Program.cs
File metadata and controls
83 lines (68 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using CryMediaAPI;
using System.Text.Json;
using System.Diagnostics;
using static CryCompressor.ColorConsole;
namespace CryCompressor;
class Program
{
const string CONFIG_PATH = "compressor-config.jsonc";
static async Task Main()
{
if (!File.Exists(CONFIG_PATH))
{
Configuration.Create(CONFIG_PATH);
Console.WriteLine("No configuration file found. Created new one.");
return;
}
try
{
var config = Configuration.Load(CONFIG_PATH);
// VALIDATE CONFIGURATION
if (string.IsNullOrEmpty(config.InputDirectory) || !Directory.Exists(config.InputDirectory)) throw new Exception("Invalid input directory!");
if (string.IsNullOrEmpty(config.OutputDirectory) || !Directory.Exists(config.OutputDirectory)) throw new Exception("Invalid output directory!");
if (config.ImageExtensions == null || config.VideoExtensions == null || config.AudioExtensions == null) throw new Exception("Extensions can not be null!");
if (config.VideoCompression == null || config.ImageCompression == null || config.AudioCompression == null) throw new Exception("Compression configurations can not be null!");
if (config.VideoCompression.ParametersPriorityList == null || config.VideoCompression.ParametersPriorityList.Length == 0) throw new Exception("Video parameters priority list can not be empty!");
if (config.ImageCompression.ParametersPriorityList == null || config.ImageCompression.ParametersPriorityList.Length == 0) throw new Exception("Image parameters priority list can not be empty!");
if (config.AudioCompression.ParametersPriorityList == null || config.AudioCompression.ParametersPriorityList.Length == 0) throw new Exception("Audio parameters priority list can not be empty!");
if (config.VideoCompression.MaxConcurrentWorkers <= 0) throw new Exception("Video max. concurrent workers can not be 0 or less!");
if (config.ImageCompression.MaxConcurrentWorkers <= 0) throw new Exception("Image max. concurrent workers can not be 0 or less!");
if (config.AudioCompression.MaxConcurrentWorkers <= 0) throw new Exception("Image max. concurrent workers can not be 0 or less!");
try
{
// CHECK IF FFMPEG PRESENT
var encoders = FFmpegWrapper.GetEncoders();
}
catch (Exception e)
{
throw new Exception("Failed to use FFmpeg! Make sure it's accessible! " + e.Message);
}
// HANDLE CANCEL EVENT
var csc = new CancellationTokenSource();
Console.CancelKeyPress += (a, b) =>
{
csc.Cancel();
b.Cancel = true;
};
// START WORK
var sw = Stopwatch.StartNew();
var c = new Compressor(config, csc.Token);
await c.Start();
sw.Stop();
Console.WriteLine();
WriteInfo($"Done ({sw.Elapsed.TotalMilliseconds.ToTimeString()})");
}
catch (JsonException)
{
WriteError("ERROR: Failed to parse configuration file!");
}
catch (Exception ex)
{
WriteError("ERROR: " + ex.Message);
}
}
}