-
Notifications
You must be signed in to change notification settings - Fork 7
/
ExternalProgram.cs
262 lines (225 loc) · 9.74 KB
/
ExternalProgram.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
using Serilog;
using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
using SharpCompress.Common;
namespace YoutubeSegmentDownloader;
public static class ExternalProgram
{
public enum DependencyStatus
{
Unknown,
NotExist,
Downloading,
Exist,
Failed
}
// https://github.com/yt-dlp/FFmpeg-Builds/releases/latest
private const string FFmpegFileName = "ffmpeg-master-latest-win64-gpl-shared.zip";
private static readonly DirectoryInfo WorkingDir = new(Path.GetDirectoryName(Application.ExecutablePath) ?? Path.GetTempPath());
public static DependencyStatus YtdlpStatus { get; private set; } = DependencyStatus.Unknown;
public static DependencyStatus FFmpegStatus { get; private set; } = DependencyStatus.Unknown;
public static string? YtdlpPath { get; private set; }
public static string? FFmpegPath { get; private set; }
private static async Task DownloadYtdlpAsync()
{
Log.Information("Start downloading yt-dlp...");
YtdlpStatus = DependencyStatus.Downloading;
YtdlpPath = WorkingDir.FullName;
HttpClient client = new();
const string ytdlpUrl = @"https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.exe";
HttpResponseMessage response = await client.GetAsync(ytdlpUrl, HttpCompletionOption.ResponseHeadersRead);
Log.Debug("Get response from {YtdlpUrl}", ytdlpUrl);
if (!response.IsSuccessStatusCode)
{
Log.Fatal("Failed to get yt-dlp from {YtdlpUrl}!", ytdlpUrl);
YtdlpStatus = DependencyStatus.Failed;
return;
}
string filePath = Path.Combine(YtdlpPath, "yt-dlp.exe");
File.Delete(filePath);
await using FileStream fs = new(filePath, FileMode.Create);
//response.EnsureSuccessStatusCode();
await response.Content.CopyToAsync(fs);
if (File.Exists(filePath))
{
Log.Information("Finish downloading yt-dlp at {filePath}", filePath);
YtdlpStatus = DependencyStatus.Exist;
}
else
{
Log.Fatal("Finished downloading yt-dlp but file not found in {filePath}!", filePath);
YtdlpStatus = DependencyStatus.Failed;
}
}
internal static async Task DownloadFFmpegAsync()
{
Log.Information("Start downloading FFmpeg...");
FFmpegStatus = DependencyStatus.Downloading;
FFmpegPath = WorkingDir.FullName;
HttpClient client = new();
const string ffmpegUrl = @$"https://github.com/yt-dlp/FFmpeg-Builds/releases/download/latest/{FFmpegFileName}";
HttpResponseMessage response = await client.GetAsync(ffmpegUrl, HttpCompletionOption.ResponseHeadersRead);
Log.Debug("Get response from {FFmpegUrl}", ffmpegUrl);
if (!response.IsSuccessStatusCode)
{
Log.Fatal("Failed to get ffmpeg from {FFmpegUrl}!", ffmpegUrl);
YtdlpStatus = DependencyStatus.Failed;
return;
}
string archivePath = Path.Combine(FFmpegPath, FFmpegFileName);
File.Delete(archivePath);
await using (FileStream fs = new(archivePath, FileMode.Create))
{
//response.EnsureSuccessStatusCode();
await response.Content.CopyToAsync(fs);
}
Log.Information("Finish downloading FFmpeg at {filePath}", archivePath);
try
{
using var archive = ZipArchive.Open(archivePath);
Log.Information("Start unpacking {FFmpegFileName}", FFmpegFileName);
foreach (ZipArchiveEntry entry in archive.Entries.Where(entry => !entry.IsDirectory
&& (entry.Key.EndsWith("exe")
|| entry.Key.EndsWith("dll")
|| entry.Key.Contains("LICENSE"))))
{
Log.Information("{FilePath}", Path.Combine(FFmpegPath, Path.GetFileName(entry.Key)));
entry.WriteToDirectory(FFmpegPath,
new ExtractionOptions
{
ExtractFullPath = false,
Overwrite = true
});
}
if (File.Exists(Path.Combine(FFmpegPath, "ffmpeg.exe")))
{
Log.Information("Complete FFmpeg unpacking to {filePath}", FFmpegPath);
FFmpegStatus = DependencyStatus.Exist;
}
else
{
Log.Fatal("Finished unpacking FFmpeg under {filePath} but couldn't find ffmpeg.exe!", FFmpegPath);
FFmpegStatus = DependencyStatus.Failed;
}
}
catch (Exception e)
{
Log.Fatal(e, "Failed to unpack FFmpeg!");
FFmpegStatus = DependencyStatus.Failed;
}
finally
{
File.Delete(archivePath);
}
}
/// <summary>
/// 尋找程式路徑
/// </summary>
/// <returns>Full path of yt-dlp and FFmpeg</returns>
public static (string?, string?) WhereIs()
{
// https://stackoverflow.com/a/63021455
string[] paths = Environment.GetEnvironmentVariable("PATH")?.Split(';') ?? [];
string[] extensions = Environment.GetEnvironmentVariable("PATHEXT")?.Split(';') ?? [];
// ffmpeg not selected as downloader when not in PATH
// In short: --ffmpeg-location doesn't work. Force download everything to my tmp folder.
// https://github.com/yt-dlp/yt-dlp/issues/2191
//string? ytdlpPath = (from p in new[] { Environment.CurrentDirectory, TempDirectory.FullName }.Concat(paths)
string? ytdlpPath = (from p in new[] { WorkingDir.FullName }
from e in extensions
let path = Path.Combine(p.Trim(), "yt-dlp" + e.ToLower())
where File.Exists(path)
select Path.GetDirectoryName(path)).FirstOrDefault();
//string? _FFmpegPath = (from p in new[] { Environment.CurrentDirectory, TempDirectory.FullName }.Concat(paths)
string? ffmpegPath = (from p in new[] { WorkingDir.FullName }
from e in extensions
let path = Path.Combine(p.Trim(), "ffmpeg" + e.ToLower())
where File.Exists(path)
select Path.GetDirectoryName(path)).FirstOrDefault();
//Ytdlp_Status = string.IsNullOrEmpty(ytdlpPath)
// ? DependencyStatus.NotExist
// : DependencyStatus.Exist;
//FFmpeg_Status = string.IsNullOrEmpty(_FFmpegPath)
// ? DependencyStatus.NotExist
// : DependencyStatus.Exist;
YtdlpPath = ytdlpPath;
FFmpegPath = ffmpegPath;
Log.Debug("Found yt-dlp.exe at {YtdlpPath}", YtdlpPath);
Log.Debug("Found ffmpeg.exe at {FFmpegPath}", FFmpegPath);
#if false
#warning Disable this "Force download again" debug section!
Log.Debug("Force download again!");
YtdlpPath = FFmpegPath = null;
foreach (var file in TempDirectory.GetFiles())
{
file.Delete();
}
#endif
return (ytdlpPath, ffmpegPath);
}
/// <summary>
/// 更新依賴
/// </summary>
/// <param name="ytdlpPath"></param>
/// <param name="ffmpegPath"></param>
/// <param name="force">強制更新所有依賴</param>
/// <returns></returns>
public static Task UpdateDependenciesAsync(string? ytdlpPath = null, string? ffmpegPath = null, bool force = false)
{
List<Task> tasks = [];
if (string.IsNullOrEmpty(ytdlpPath)
|| !File.Exists(Path.Combine(ytdlpPath, "yt-dlp.exe"))
|| force)
{
YtdlpStatus = DependencyStatus.NotExist;
tasks.Add(DownloadYtdlpAsync());
}
else
{
YtdlpStatus = DependencyStatus.Exist;
}
//string version = await GetFFmpegVersionAsync(_FFmpegPath);
//if (!version.Contains("5.0") || force)
if (string.IsNullOrEmpty(ffmpegPath)
|| !File.Exists(Path.Combine(ffmpegPath, "ffmpeg.exe"))
|| !File.Exists(Path.Combine(ffmpegPath, "ffprobe.exe"))
|| force)
{
FFmpegStatus = DependencyStatus.NotExist;
//Log.Information("No matching version of FFmpeg was detected.");
tasks.Add(DownloadFFmpegAsync());
}
else
{
FFmpegStatus = DependencyStatus.Exist;
//Log.Information("Detected that your FFmpeg version matches.");
}
return Task.WhenAll(tasks);
}
//public static async Task<string> GetFFmpegVersionAsync(string? _FFmpegPath)
//{
// string FFmpegVersion = "";
// if (string.IsNullOrEmpty(_FFmpegPath)) return "";
// FFmpeg.SetExecutablesPath(_FFmpegPath);
// Log.Information("Start to check FFmpeg version.");
// IConversion conversion = FFmpeg.Conversions.New();
// conversion.OnDataReceived += (_, e) =>
// {
// if (string.IsNullOrEmpty(FFmpegVersion))
// {
// // Take the first line
// FFmpegVersion = e.Data ?? "";
// }
// Log.Verbose(e.Data);
// };
// //Log.Debug("FFmpeg arguments: {arguments}", conversion.Build());
// try
// {
// await conversion.Start();
// }
// // Must Exception
// catch (ConversionException) { }
// Log.Information(FFmpegVersion);
// return FFmpegVersion;
//}
}