This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FFmpeg.cs
217 lines (180 loc) · 6.31 KB
/
FFmpeg.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace cdcrush.lib.app
{
/// <summary>
/// Simple wrapper for FFmpeg
/// Currently just supports Audio Compression for use with the CDCRUSH project
///
/// "onProgress" => Reports percentage 0 to 100
/// "onComplete" => Exit code 0 for OK, other for ERROR
///
/// </summary>
class FFmpeg:ICliReport
{
const string EXECUTABLE_NAME = "ffmpeg.exe";
private CliApp proc;
// # USER SET ::
public Action<int> onProgress { get; set; }
public Action<bool> onComplete { get; set; }
public string ERROR {get; private set;}
// Percentage Helpers
int secondsConverted, targetSeconds;
public int progress {get; private set;} // Current progress % of the current conversion
// Ogg vorbis Quality Number to kbps.
public static readonly int[] QUALITY = { 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 500 };
// -----------------------------------------
/// <summary>
/// FFMPEG wrapper
/// </summary>
/// <param name="executablePath">Set the path of ffmpeg if not on path already</param>
public FFmpeg(string executablePath = "")
{
proc = new CliApp(Path.Combine(executablePath,EXECUTABLE_NAME));
proc.onComplete = (code) =>
{
if (code == 0)
{
onComplete?.Invoke(true);
}
else
{
ERROR = "Something went wrong with FFMPEG";
onComplete(false);
}
};
// Get and calculate progress, "targetSeconds" needs to be set for this to work
proc.onStdErr = (s) =>
{
if (targetSeconds == 0) return;
secondsConverted = readSecondsFromOutput(s, @"time=(\d{2}):(\d{2}):(\d{2})");
if (secondsConverted == -1) return;
progress = (int)Math.Ceiling(((double)secondsConverted / (double)targetSeconds) * 100f);
// LOG.log("[FFMPEG] : {0} / {1} = {2}", secondsConverted, targetSeconds, progress);
if (progress > 100) progress = 100;
onProgress?.Invoke(progress);
};
}// -----------------------------------------
// --
public void kill() => proc?.kill();
/// <summary>
/// Read a file's duration, used for when converting to PCM
/// </summary>
/// <param name="file"></param>
private int getSecondsFromFile(string input)
{
int i = 0;
var s = CliApp.quickStartSync(proc.executable, string.Format("-i \"{0}\" -f null -", input));
if(s[2]=="0") // ffmpeg success
{
i = readSecondsFromOutput(s[1], @"\s*Duration:\s*(\d{2}):(\d{2}):(\d{2})");
LOG.log("[FFMPEG] : {0} duration in seconds = {1}", input, i);
}
return i;
}// -----------------------------------------
/// <summary>
/// Returns FFMPEG time to seconds. HELPER FUNCTION
/// </summary>
/// <param name="input">The string to check the regex</param>
/// <param name="expression">Needs to be a regexp with 3 capture groups</param>
/// <returns></returns>
private int readSecondsFromOutput(string input,string expression)
{
var m = Regex.Match(input,expression);
var seconds = -1;
if(m.Success){
var hh = int.Parse(m.Groups[1].Value);
var mm = int.Parse(m.Groups[2].Value);
var ss = int.Parse(m.Groups[3].Value);
seconds = (ss + (mm * 60) + (hh * 360));
}
return seconds;
}// -----------------------------------------
/// <summary>
/// Convert an audio file to a PCM file for use in a CD audio
/// ! Does not check INPUT file !
/// ! Overwrites all generated files !
/// </summary>
/// <param name="input"></param>
/// <param name="output">If ommited, will be automatically set</param>
/// <returns></returns>
public bool audioToPCM(string input,string output = null)
{
LOG.log("[FFMPEG] : Converting \"{0}\" to PCM",input);
if(string.IsNullOrEmpty(output)) {
output = Path.ChangeExtension(input,"pcm");
}
// Prepare progress variables
secondsConverted = progress = 0;
targetSeconds = getSecondsFromFile(input);
proc.start(string.Format("-i \"{0}\" -y -f s16le -acodec pcm_s16le \"{1}\"", input, output));
// note: If oncomplete is set, it will be called
// If onprogress is set, it will be called with progress
return true;
}// -----------------------------------------
/// <summary>
/// Convert a PCM audio file to OGG
/// ! Overwrites all generated files !
/// ! Does not check INPUT file !
/// </summary>
/// <param name="input"></param>
/// <param name="OGGquality">0(64kbps) to 10(500kbps)</param>
/// <param name="output">If ommited, will be automatically set</param>
/// <returns></returns>
public bool audioPCMToOgg(string input,int OGGquality,string output = null)
{
// [safequard]
if (OGGquality < 0) OGGquality = 0;
else if (OGGquality > 10) OGGquality = 10;
if(string.IsNullOrEmpty(output)) {
output = Path.ChangeExtension(input,"ogg");
}else{
//[safeguard] Make sure it's an OGG
if(!output.ToLower().EndsWith(".ogg")) {
output += ".ogg"; // try to fix it
}
}
LOG.log("[FFMPEG] : Converting \"{0}\" to OGG, Quality {1}",input,QUALITY[OGGquality]);
_initProgressVars(input);
proc.start(string.Format(
"-y -f s16le -ar 44.1k -ac 2 -i \"{0}\" -c:a libvorbis -q {1} \"{2}\"",
input,OGGquality,output
));
return true;
}// -----------------------------------------
/// <summary>
/// Convert a PCM audio file to FLAC
/// ! Overwrites all generated files !
/// ! Does not check INPUT file !
/// </summary>
/// <param name="input"></param>
/// <param name="output">If ommited, will be automatically set</param>
/// <returns></returns>
public bool audioPCMToFlac(string input,string output = null)
{
if(string.IsNullOrEmpty(output)) {
output = Path.ChangeExtension(input,"flac");
}else{
//[safeguard] Make sure it's a FLAC
if(!output.ToLower().EndsWith(".flac")) {
output += ".flac"; // try to fix it
}
}
LOG.log("[FFMPEG] : Converting \"{0}\" to FLAC",input);
_initProgressVars(input);
// C#6 string interpolation
proc.start($"-y -f s16le -ar 44.1k -ac 2 -i \"{input}\" -c:a flac \"{output}\"");
//proc.start(string.Format("-y -f s16le -ar 44.1k -ac 2 -i \"{0}\" -c:a flac \"{1}\"", input,output));
return true;
}// -----------------------------------------
// Helper
void _initProgressVars(string input)
{
var fsize = (int)new FileInfo(input).Length;
secondsConverted = progress = 0;
targetSeconds = (int)Math.Floor((double)fsize / 176400); // PCM is 176400 bytes per second
// LOG.log("[FFMPEG] : FILE SIZE = {0}, TARGET SECONDS = {1}", fsize, targetSeconds);
}// -----------------------------------------
}// -- end class
}// --