-
Notifications
You must be signed in to change notification settings - Fork 16
/
WebVideoConverter.cs
450 lines (431 loc) · 12.9 KB
/
WebVideoConverter.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using static WebOne.Program;
namespace WebOne
{
/// <summary>
/// Web video dowloader & converter
/// </summary>
class WebVideoConverter
{
/// <summary>
/// Download and convert an online video (from hostings like YouTube, VK, etc)
/// </summary>
public WebVideo ConvertVideo(Dictionary<string, string> Arguments, LogWriter Log)
{
WebVideo video = new WebVideo();
try
{
string YoutubeDlArgs = "";
string FFmpegArgs = "";
bool UseFFmpeg = true;
bool GetYoutubeJson = false;
// Check options
if (!Arguments.ContainsKey("url"))
{ throw new InvalidOperationException("Internet video address is missing."); }
// Load default options
foreach (var x in ConfigFile.WebVideoOptions)
{ if (!Arguments.ContainsKey(x.Key)) Arguments[x.Key] = x.Value; }
// Configure output file type
string PreferredMIME = "application/octet-stream", PreferredName = "video.avi";
if(Arguments.ContainsKey("f")) // (ffmpeg output format)
{
switch(Arguments["f"])
{
case "avi":
PreferredMIME = "video/msvideo";
PreferredName = "onlinevideo.avi";
break;
case "mpeg1video":
case "mpeg2video":
PreferredMIME = "video/mpeg";
PreferredName = "onlinevideo.mpg";
break;
case "mpeg4":
PreferredMIME = "video/mp4";
PreferredName = "onlinevideo.mp4";
break;
case "mpegts":
PreferredMIME = "video/mp2t";
PreferredName = "onlinevideo.mts";
break;
case "asf":
case "asf_stream":
case "wmv":
PreferredMIME = "video/x-ms-asf";
PreferredName = "onlinevideo.asf";
break;
case "mov":
PreferredMIME = "video/qucktime";
PreferredName = "onlinevideo.mov";
break;
case "ogg":
PreferredMIME = "video/ogg";
PreferredName = "onlinevideo.ogg";
break;
case "webm":
PreferredMIME = "video/webm";
PreferredName = "onlinevideo.webm";
break;
case "swf":
PreferredMIME = "application/x-shockwave-flash";
PreferredName = "onlinevideo.swf";
break;
case "rm":
PreferredMIME = "application/vnd.rn-realmedia";
PreferredName = "onlinevideo.rm";
break;
case "3gp":
PreferredMIME = "video/3gpp";
PreferredName = "onlinevideo.3gp";
break;
default:
PreferredMIME = "application/octet-stream";
PreferredName = "onlinevideo." + Arguments["f"];
break;
}
}
if(Arguments.ContainsKey("j") ||
Arguments.ContainsKey("J") ||
Arguments.ContainsKey("dump-json") ||
Arguments.ContainsKey("dump-single-json") ||
Arguments.ContainsKey("print-json"))
{
PreferredMIME = "application/json";
PreferredName = "metadata.json";
}
// Set output file type over auto-detected (if need)
if (!Arguments.ContainsKey("content-type"))
{ Arguments.Add("content-type", PreferredMIME); }
if (!Arguments.ContainsKey("filename"))
{ Arguments.Add("filename", PreferredName); }
// Load all parameters
foreach (var Arg in Arguments)
{
if((Arg.Key.StartsWith("vf") && Arguments["vcodec"] != "copy") ||
(Arg.Key.StartsWith("af") && Arguments["acodec"] != "copy"))
{
// Don't apply filters if codec is original
FFmpegArgs += string.Format(" -{0} {1}", Arg.Key, Arg.Value);
continue;
}
if(Arg.Key.StartsWith("filter"))
{
/* Currently may cause FFMPEG errors if combined with `-vcodec copy`:
* Filtergraph 'scale=480:-1' was defined for video output stream 0:0 but codec copy was selected.
* Filtering and streamcopy cannot be used together.
*/
FFmpegArgs += string.Format(" -{0} {1}", Arg.Key, Arg.Value);
continue;
}
switch (Arg.Key.ToLowerInvariant())
{
case "enable":
if (!ToBoolean(Arg.Value)) throw new Exception("This feature is disabled by administrator.");
continue;
case "url":
case "content-type":
case "filename":
case "prefer":
case "gui":
case "youtubedlapp":
case "ffmpegapp":
continue;
case "noffmpeg":
UseFFmpeg = !ToBoolean(Arg.Value);
continue;
case "abort-on-error":
case "ignore-config":
case "mark-watched":
case "no-mark-watched":
case "proxy":
case "socket-timeout":
case "source-address":
case "4":
case "6":
case "geo-verification-proxy":
case "geo-bypass":
case "no-geo-bypass":
case "geo-bypass-country":
case "geo-bypass-ip-block":
case "include-ads":
case "limit-rate":
case "retries":
case "fragment-retries":
case "skip-unavailable-fragments":
case "abort-on-unavailable-fragment":
case "keep-fragments":
case "buffer-size":
case "no-resize-buffer":
case "http-chunk-size":
case "xattr-set-filesize ":
case "hls-prefer-native":
case "hls-prefer-ffmpeg":
case "hls-use-mpegts":
case "external-downloader":
case "external-downloader-args":
case "cookies":
case "no-cache-dir":
case "newline":
case "no-progress":
case "no-check-certificate":
case "prefer-insecure":
case "user-agent":
case "referer":
case "add-header":
case "bidi-workaround":
case "sleep-interval":
case "max-sleep-interval":
case "format":
case "youtube-skip-dash-manifest":
case "merge-output-format":
case "username":
case "password":
case "twofactor":
case "video-password":
case "ap-mso":
case "ap-username":
case "ap-password":
case "extract-audio":
case "audio-format":
case "audio-quality":
case "recode-video":
case "postprocessor-args":
case "embed-subs":
case "embed-thumbnail":
case "add-metadata":
case "metadata-from-title":
case "xattrs":
case "fixup":
case "prefer-avconv":
case "prefer-ffmpeg":
case "convert-subs":
YoutubeDlArgs += string.Format(" --{0} {1}", Arg.Key, Arg.Value);
continue;
case "j":
case "J":
case "dump-json":
case "dump-single-json":
case "print-json":
YoutubeDlArgs += string.Format(" --{0} {1}", Arg.Key, Arg.Value);
UseFFmpeg = false;
GetYoutubeJson = true;
continue;
case "loglevel":
case "max_alloc":
case "filter_threads":
case "filter_complex_threads":
case "stats":
case "max_error_rate":
case "bits_per_raw_sample":
case "vol":
case "codec":
case "pre":
case "t":
case "to":
case "fs":
case "ss":
case "sseof":
case "seek_timestamp":
case "timestamp":
case "metadata":
case "program":
case "target":
case "apad":
case "frames":
case "filter_script":
case "reinit_filter":
case "discard":
case "disposition":
case "vframes":
case "r":
case "s":
case "aspect":
case "vn":
case "vcodec":
case "timecode":
case "pass":
case "ab":
case "b":
case "dn":
case "aframes":
case "aq":
case "ar":
case "ac":
case "an":
case "acodec":
case "sn":
case "scodec":
case "stag":
case "fix_sub_duration":
case "canvas_size":
case "spre":
case "f":
FFmpegArgs += string.Format(" -{0} {1}", Arg.Key, Arg.Value);
continue;
case "vf":
case "af":
case "filter":
//ffmpeg filters parsed above
continue;
default:
Log.WriteLine(" Unsupported argument: {0}", Arg.Key);
continue;
}
}
// Configure YT-DLP and FFmpeg processes and prepare data stream
ProcessStartInfo YoutubeDlStart = new();
YoutubeDlStart.FileName = ConfigFile.WebVideoOptions["YouTubeDlApp"] ?? "yt-dlp";
YoutubeDlStart.Arguments = string.Format("\"{0}\"{1} -o -", Arguments["url"], YoutubeDlArgs);
YoutubeDlStart.RedirectStandardOutput = true;
YoutubeDlStart.RedirectStandardError = true;
ProcessStartInfo FFmpegStart = new();
FFmpegStart.FileName = ConfigFile.WebVideoOptions["FFmpegApp"] ?? "ffmpeg";
FFmpegStart.Arguments = string.Format("-i pipe: {0} pipe:", FFmpegArgs);
FFmpegStart.RedirectStandardInput = true;
FFmpegStart.RedirectStandardOutput = true;
FFmpegStart.RedirectStandardError = false;
video.Available = true;
video.ErrorMessage = "";
video.ContentType = Arguments["content-type"];
video.FileName = Arguments["filename"];
// Start both processes
Process YoutubeDl = null;
Process FFmpeg = null;
if (UseFFmpeg)
{
Log.WriteLine(" Video convert: {0} {1} | {2} {3}", YoutubeDlStart.FileName, YoutubeDlStart.Arguments, FFmpegStart.FileName, FFmpegStart.Arguments);
YoutubeDl = Process.Start(YoutubeDlStart);
FFmpeg = Process.Start(FFmpegStart);
}
else
{
Log.WriteLine(" Video convert: {0} {1}", YoutubeDlStart.FileName, YoutubeDlStart.Arguments);
YoutubeDl = Process.Start(YoutubeDlStart);
}
// Calculate approximately end time
DateTime EndTime = DateTime.Now.AddSeconds(30);
// Enable YT-DLP error handling
if (!GetYoutubeJson)
{
YoutubeDl.ErrorDataReceived += (o, e) =>
{
Console.WriteLine("{0}", e.Data);
if (e.Data != null && e.Data.StartsWith("ERROR:"))
{
video.Available = false;
video.ErrorMessage = "Online video failed to download: " + e.Data[7..];
Log.WriteLine(false, false, " yt-dlp: {0}", e.Data);
}
if (e.Data != null && e.Data.StartsWith("WARNING:"))
{
Log.WriteLine(false, false, " yt-dlp: {0}", e.Data);
}
if (e.Data != null && Regex.IsMatch(e.Data, @"\[download\].*ETA (\d\d:\d\d:\d\d|\d\d:\d\d)"))
{
Match match = Regex.Match(e.Data, @"\[download\].*ETA (\d\d:\d\d:\d\d|\d\d:\d\d)");
//assuming, it's succcessfull & have 2 groups
string ETA = (Regex.IsMatch(match.Groups[1].Value, @"\d\d:\d\d:\d\d")) ? match.Groups[1].Value : "00:" + match.Groups[1].Value;
EndTime = DateTime.Now.Add(TimeSpan.Parse(ETA));
}
};
YoutubeDl.BeginErrorReadLine();
}
// (Here FFmpeg error handling might be useful, but it's output is hard to parse)
// Redirect STDIN/STDOUT streams
if (!GetYoutubeJson)
{
if (UseFFmpeg)
{
// - Redirect yt-dlp STDOUT to FFmpeg STDIN stream, and FFmpeg STDOUT to return stream
new Task(() =>
{
YoutubeDl.StandardOutput.BaseStream.CopyTo(FFmpeg.StandardInput.BaseStream);
}).Start();
video.VideoStream = FFmpeg.StandardOutput.BaseStream;
}
else
{
// - Redirect yt-dlp STDOUT to return stream
video.VideoStream = YoutubeDl.StandardOutput.BaseStream;
}
}
if(GetYoutubeJson)
{
// - Redirect yt-dlp STDERR to return stream (video metadata JSON)
video.VideoStream = YoutubeDl.StandardError.BaseStream;
}
// Initialize idleness hunters
new Task(() =>
{
while (DateTime.Now < EndTime) { Thread.Sleep(1000); }
float YoutubeDlCpuLoad = 0;
while (!YoutubeDl.HasExited)
{
Thread.Sleep(1000);
PreventProcessIdle(ref YoutubeDl, ref YoutubeDlCpuLoad, Log);
}
}).Start();
if(UseFFmpeg) new Task(() =>
{
while (DateTime.Now < EndTime) { Thread.Sleep(1000); }
float FFmpegCpuLoad = 0;
while (!FFmpeg.HasExited)
{
Thread.Sleep(1000);
PreventProcessIdle(ref FFmpeg, ref FFmpegCpuLoad, Log);
}
}).Start();
// Wait for YT-DLP & FFmpeg to start working or end with error
Thread.Sleep(5000);
}
catch (Exception VidCvtError)
{
video.Available = false;
video.ErrorMessage = VidCvtError.Message;
Log.WriteLine("Cannot convert video: {0} - {1}", VidCvtError.GetType(), VidCvtError.Message);
}
return video;
}
}
/// <summary>
/// Converted web video
/// </summary>
class WebVideo
{
/// <summary>
/// The video file stream
/// </summary>
public Stream VideoStream { get; internal set; }
/// <summary>
/// The video container MIME content type
/// </summary>
public string ContentType { get; internal set; }
/// <summary>
/// The video container file name
/// </summary>
public string FileName { get; internal set; }
/// <summary>
/// Is the download & convert successful
/// </summary>
public bool Available { get; internal set; }
/// <summary>
/// Error messages (if any)
/// </summary>
public string ErrorMessage { get; internal set; }
public WebVideo()
{
Available = false;
ErrorMessage = "WebVideo not configured!";
ContentType = "text/plain";
FileName = "webvideo.err";
}
}
}