-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
122 lines (107 loc) · 4.07 KB
/
Program.cs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FFMpegCore;
using FFMpegCore.Enums;
using Newtonsoft.Json;
namespace VideoMinify
{
class Program
{
public static Dictionary<Task, ConversionStatusText> Tasks = new Dictionary<Task, ConversionStatusText>();
private static Config config;
private static CancellationTokenSource _cancellationTokenSource;
static async Task Main(string[] args)
{
config = ReadConfig();
_cancellationTokenSource = new CancellationTokenSource();
AppDomain.CurrentDomain.ProcessExit +=
(_, _) => _cancellationTokenSource.Cancel();
Console.CancelKeyPress += (_, _) => _cancellationTokenSource.Cancel();
int counter = Console.CursorTop + 1;
foreach (var item in args)
{
if (item.IsValidVideoPath())
{
Tasks.Add(ConvertVideoAsync(item), new ConversionStatusText(counter, item));
counter++;
}
else
{
Console.WriteLine($"File {Path.GetFileName(item)} is not a video file... skipping!");
}
}
if (Tasks.Count == 0)
{
Console.WriteLine("Nothing was converted because no supported file was given!");
return;
}
while (!Tasks.Keys.All(x => x.IsCompleted))
{
foreach (var (key, value) in Tasks)
{
value.Update(key.IsCompleted);
}
await Task.Delay(50);
}
Console.WriteLine("All Tasks completed!");
}
private static Config ReadConfig()
{
var path = AppContext.BaseDirectory + "config.json";
if (File.Exists(path))
{
Console.WriteLine("config.json found!");
Config? conf = null;
try
{
conf = JsonConvert.DeserializeObject<Config>(File.ReadAllText(path));
if (conf != null)
{
Console.WriteLine("Config loaded!");
return conf;
}
}
catch (Exception e)
{
Console.WriteLine("Error while parsing config! \r\n" + e.Message);
Console.WriteLine("Falling back to defaults!");
return new Config();
}
}
Console.WriteLine("No config found. Falling back to defaults!");
return new Config();
}
private static async Task ConvertVideoAsync(string path)
{
var stopwatch = new Stopwatch();
var newFileName = Path.GetFileNameWithoutExtension(path);
newFileName += config.Suffix + ".mp4";
var directory = Path.GetDirectoryName(path);
var outputPath = directory + Path.DirectorySeparatorChar + newFileName;
try
{
stopwatch.Start();
await FFMpegArguments.FromFileInput(path)
.OutputToFile(outputPath, config.OverwriteExisting, options => options
.UsingMultithreading(config.UseMultithreading)
.WithVideoCodec(VideoCodec.LibX264)
.WithConstantRateFactor(config.ConstantRateFactor))
.CancellableThrough(_cancellationTokenSource.Token)
.ProcessAsynchronously();
stopwatch.Stop();
Console.WriteLine(
$"Done with file {Path.GetFileName(path)} in {stopwatch.Elapsed}, output file is {newFileName}");
}
catch (Exception e)
{
stopwatch.Stop();
Console.WriteLine($"ERROR: {path} not processed: {e}");
}
}
}
}