-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
474 lines (400 loc) · 18.6 KB
/
Copy pathProgram.cs
File metadata and controls
474 lines (400 loc) · 18.6 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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// SubDownload
// Baixa a legenda em Portugues (Brasil) de um filme — tenta o OpenSubtitles, depois o
// SubDL e, se nao achar, o subtitlecat.com — pesquisando pelo arquivo de video passado como
// argumento (integracao com o menu de contexto do Windows Explorer). Salva o .srt na
// mesma pasta do video, com o mesmo nome do arquivo.
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
namespace SubDownload;
internal static class Program
{
private const string CleanAltsFlag = "--clean-alts";
private const string DownloadAltsFlag = "--download-alts";
private static readonly string[] SupportedExtensions = { ".mkv", ".mp4" };
private static async Task<int> Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
string? mode = args.Length > 0 && args[0].StartsWith("--", StringComparison.Ordinal)
? args[0]
: null;
var validMode = mode is null
|| string.Equals(mode, CleanAltsFlag, StringComparison.OrdinalIgnoreCase)
|| string.Equals(mode, DownloadAltsFlag, StringComparison.OrdinalIgnoreCase);
var pathArgIndex = mode is null ? 0 : 1;
if (!validMode || args.Length <= pathArgIndex || string.IsNullOrWhiteSpace(args[pathArgIndex]))
{
Console.WriteLine("=== SubDownload - legenda PT-BR (OpenSubtitles / SubDL / subtitlecat.com) ===\n");
Console.WriteLine("Uso: SubDownload.exe \"caminho\\para\\filme.mkv\"");
Console.WriteLine($" SubDownload.exe {DownloadAltsFlag} \"caminho\\para\\filme.mkv\"");
Console.WriteLine($" SubDownload.exe {CleanAltsFlag} \"caminho\\para\\filme.mkv\"");
return Finish(1);
}
// O Explorer, ao chamar o menu de contexto, substitui o caminho pelo nome curto
// 8.3 (ex: STARTR~1.MKV) quando o caminho completo e muito longo — a substituicao
// de %1 no shell32 usa um buffer de tamanho fixo, e isso acontece mesmo com o app
// marcado como longPathAware no manifest. Aqui reconstruimos o nome real do arquivo.
var videoPath = ExpandLongPath(args[pathArgIndex].Trim('"'));
try
{
if (string.Equals(mode, CleanAltsFlag, StringComparison.OrdinalIgnoreCase))
return RunCleanAlts(videoPath);
if (string.Equals(mode, DownloadAltsFlag, StringComparison.OrdinalIgnoreCase))
return await RunDownloadAltsAsync(videoPath);
return await RunAsync(videoPath);
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine($"[ERRO] {ex.Message}");
return Finish(1);
}
}
private const int MaxAlternates = 2; // quantas alternativas o "--download-alts" traz no maximo
private static async Task<int> RunAsync(string videoPath)
{
Console.WriteLine("=== SubDownload - legenda PT-BR (OpenSubtitles / SubDL / subtitlecat.com) ===\n");
if (!File.Exists(videoPath))
{
Console.WriteLine($"[ERRO] Arquivo nao encontrado: {videoPath}");
return Finish(1);
}
var ext = Path.GetExtension(videoPath);
if (!SupportedExtensions.Contains(ext, StringComparer.OrdinalIgnoreCase))
{
Console.WriteLine($"[ERRO] Extensao nao suportada: {ext} (use .mkv ou .mp4)");
return Finish(1);
}
var fileNameNoExt = Path.GetFileNameWithoutExtension(videoPath);
var folder = Path.GetDirectoryName(Path.GetFullPath(videoPath))!;
Console.WriteLine($"Arquivo: {fileNameNoExt}{ext}");
using var http = CreateHttpClient();
// Baixa so a legenda mais indicada (na pratica, quase sempre ja serve): primeiro do
// OpenSubtitles, depois do SubDL e, se nenhum tiver pt-BR/pt, do subtitlecat.com (sem acionar
// nenhuma traducao — so usa o que ja existe pronto). As demais so sao baixadas sob
// demanda, pelo menu "Baixar Legendas Alternativas".
var found = await CollectReadySubtitlesAsync(http, videoPath, fileNameNoExt, skip: 0, take: 1);
if (found.Count == 0)
{
Console.WriteLine();
Console.WriteLine("[ERRO] Nenhuma legenda em Portugues (Brasil) pronta para download em nenhuma das fontes (OpenSubtitles, SubDL, subtitlecat.com).");
return Finish(1);
}
Console.WriteLine();
var destPath = Path.Combine(folder, fileNameNoExt + ".srt");
await DownloadAndSaveAsync(http, found[0], destPath, "Baixando legenda");
Console.WriteLine();
PrintSaved(destPath, found[0]);
Console.WriteLine();
Console.WriteLine("Se ela estiver fora de sincronia, use 'Baixar Legendas Alternativas' no menu do Explorer.");
PlayVideo(videoPath);
return Finish(0);
}
private static async Task<int> RunDownloadAltsAsync(string videoPath)
{
Console.WriteLine("=== SubDownload - legendas alternativas (OpenSubtitles / SubDL / subtitlecat.com) ===\n");
if (!File.Exists(videoPath))
{
Console.WriteLine($"[ERRO] Arquivo nao encontrado: {videoPath}");
return Finish(1);
}
var ext = Path.GetExtension(videoPath);
if (!SupportedExtensions.Contains(ext, StringComparer.OrdinalIgnoreCase))
{
Console.WriteLine($"[ERRO] Extensao nao suportada: {ext} (use .mkv ou .mp4)");
return Finish(1);
}
var fileNameNoExt = Path.GetFileNameWithoutExtension(videoPath);
var folder = Path.GetDirectoryName(Path.GetFullPath(videoPath))!;
Console.WriteLine($"Arquivo: {fileNameNoExt}{ext}");
using var http = CreateHttpClient();
// Pula a mais indicada (essa ja foi baixada como legenda principal pelo outro menu)
// e baixa as proximas, na mesma ordem: OpenSubtitles, SubDL e por fim subtitlecat.com.
var found = await CollectReadySubtitlesAsync(http, videoPath, fileNameNoExt, skip: 1, take: MaxAlternates);
if (found.Count == 0)
{
Console.WriteLine();
Console.WriteLine("[ERRO] Nenhuma legenda alternativa em Portugues (Brasil) pronta para download alem da principal.");
return Finish(1);
}
Console.WriteLine();
var destPaths = new List<string>();
for (var i = 0; i < found.Count; i++)
{
// Alternativas ganham sufixo .1, .2 etc — o usuario troca manualmente pra .srt
// se a principal estiver fora de sincronia.
var destPath = Path.Combine(folder, fileNameNoExt + $".{i + 1}.srt");
await DownloadAndSaveAsync(http, found[i], destPath, $"Baixando legenda alternativa ({i + 1}/{found.Count})");
destPaths.Add(destPath);
}
for (var i = 0; i < found.Count; i++)
{
Console.WriteLine();
PrintSaved(destPaths[i], found[i]);
}
Console.WriteLine();
Console.WriteLine("Troque manualmente o nome pra .srt se a principal estiver fora de sincronia.");
return Finish(0);
}
/// <summary>
/// Percorre as legendas pt-BR/pt JA PRONTAS, na ordem de preferencia (OpenSubtitles,
/// SubDL e subtitlecat.com so se ainda faltar), pulando as primeiras
/// <paramref name="skip"/> (ex: a que ja foi baixada como principal) e parando depois
/// de coletar <paramref name="take"/> novas.
/// </summary>
private static async Task<List<ReadySubtitle>> CollectReadySubtitlesAsync(
HttpClient http, string videoPath, string fileNameNoExt, int skip, int take)
{
var found = new List<ReadySubtitle>();
var seenUrls = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var matched = 0;
Console.WriteLine($"Pesquisa: {MovieNameParser.BuildSearchQuery(fileNameNoExt)}");
await foreach (var subtitle in EnumerateAllSourcesAsync(http, videoPath, fileNameNoExt))
{
// Evita contar/salvar a mesma legenda duas vezes (candidatos diferentes
// podem apontar pro mesmo arquivo .srt).
if (!seenUrls.Add(subtitle.Url))
continue;
matched++;
if (matched <= skip)
{
Console.WriteLine($" (pulando \"{subtitle.Title}\" - ja usada como legenda principal)");
continue;
}
found.Add(subtitle);
Console.WriteLine($" -> pt-BR/pt pronta ({subtitle.Source}) em: {subtitle.Title}");
if (found.Count >= take)
break;
}
return found;
}
private static async IAsyncEnumerable<ReadySubtitle> EnumerateAllSourcesAsync(HttpClient http, string videoPath, string fileNameNoExt)
{
PrintSourceHeader(OpenSubtitlesClient.SourceName);
foreach (var subtitle in await TrySearchAsync(OpenSubtitlesClient.SourceName,
() => OpenSubtitlesClient.FindReadySubtitlesAsync(http, videoPath, fileNameNoExt)))
yield return subtitle;
PrintSourceHeader(SubDlClient.SourceName);
var subDlApiKey = GetOrAskSubDlApiKey();
if (subDlApiKey is not null)
{
foreach (var subtitle in await TrySearchAsync(SubDlClient.SourceName,
() => SubDlClient.FindReadySubtitlesAsync(http, subDlApiKey, fileNameNoExt)))
yield return subtitle;
}
PrintSourceHeader(SubtitleCatClient.SourceName);
await foreach (var subtitle in SubtitleCatClient.FindReadySubtitlesAsync(http, fileNameNoExt))
yield return subtitle;
}
// Separador bem visivel entre as fontes, com o nome centralizado.
private static void PrintSourceHeader(string sourceName)
{
const int Width = 60;
var line = new string('_', Width);
Console.WriteLine();
Console.WriteLine(line);
Console.WriteLine(sourceName.PadLeft((Width + sourceName.Length) / 2));
Console.WriteLine(line);
Console.WriteLine();
}
/// <summary>
/// Chave do SubDL do config. Se nao houver, explica como gerar e pergunta no console;
/// a chave colada e salva no config. Devolve null se o SubDL deve ser pulado.
/// </summary>
private static string? GetOrAskSubDlApiKey()
{
var config = AppConfig.Load();
if (config.SubDlApiKey is not null)
return config.SubDlApiKey;
if (config.SubDlDisabled)
{
Console.WriteLine($" (pulando: SubDL desativado - para ativar, tire o \"subdlDisabled\" do {AppConfig.FilePath})");
return null;
}
if (Console.IsInputRedirected)
{
Console.WriteLine($" (pulando: sem chave de API - gere uma gratis em {SubDlClient.ApiKeyUrl} e coloque em \"subdlApiKey\" no {AppConfig.FilePath})");
return null;
}
Console.WriteLine(" O SubDL precisa de uma chave de API gratuita.");
Console.WriteLine($" Crie uma conta e gere a chave em {SubDlClient.ApiKeyUrl}");
Console.Write(" Cole a chave (ou Enter para pular desta vez, \"n\" para nunca mais perguntar): ");
var answer = Console.ReadLine()?.Trim().Trim('"', '\'').Trim() ?? "";
if (answer.Length == 0)
{
Console.WriteLine(" (pulando o SubDL desta vez)");
return null;
}
var disable = answer.Equals("n", StringComparison.OrdinalIgnoreCase);
try
{
if (disable)
AppConfig.DisableSubDl();
else
AppConfig.SaveSubDlApiKey(answer);
Console.WriteLine(disable
? $" SubDL desativado. Para ativar depois, tire o \"subdlDisabled\" do {AppConfig.FilePath}"
: $" Chave salva em {AppConfig.FilePath}");
}
catch (Exception ex)
{
Console.WriteLine($" [aviso] nao foi possivel salvar {AppConfig.FilePath}: {ex.Message}");
}
return disable ? null : answer;
}
// Falha numa fonte com API (fora do ar, limite de requisicoes, chave invalida etc.)
// so vira aviso e nao impede de tentar as fontes seguintes.
private static async Task<List<ReadySubtitle>> TrySearchAsync(string sourceName, Func<Task<List<ReadySubtitle>>> search)
{
try
{
return await search();
}
catch (Exception ex)
{
Console.WriteLine($" [aviso] falha ao pesquisar no {sourceName}: {ex.Message}");
return new List<ReadySubtitle>();
}
}
private static async Task DownloadAndSaveAsync(HttpClient http, ReadySubtitle subtitle, string destPath, string label)
{
Console.WriteLine($"{label} ({subtitle.Source}): {subtitle.Url}");
var srtText = await subtitle.DownloadTextAsync(http);
var cleanResult = SdhCleaner.RemoveSdh(srtText);
if (cleanResult.BlocksModified > 0 || cleanResult.BlocksRemoved > 0)
{
Console.WriteLine($" Removendo SDH: {cleanResult.BlocksModified} fala(s) limpa(s), " +
$"{cleanResult.BlocksRemoved} bloco(s) 100% SDH removido(s).");
}
await File.WriteAllTextAsync(destPath, cleanResult.Srt, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
}
// Resumo de onde a legenda veio, para o usuario conferir (ou baixar outra na mao).
private static void PrintSaved(string destPath, ReadySubtitle subtitle)
{
Console.WriteLine($"[OK] Legenda salva em: {destPath}");
Console.WriteLine($" Fonte: {subtitle.Source}");
Console.WriteLine($" Legenda: {subtitle.Title}");
if (subtitle.PageUrl.Length > 0)
Console.WriteLine($" Pagina: {subtitle.PageUrl}");
Console.WriteLine($" Arquivo: {subtitle.Url}");
}
private static int RunCleanAlts(string videoPath)
{
Console.WriteLine("=== SubDownload - limpar legendas alternativas (.1, .2 ...) ===\n");
if (!File.Exists(videoPath))
{
Console.WriteLine($"[ERRO] Arquivo nao encontrado: {videoPath}");
return Finish(1);
}
var ext = Path.GetExtension(videoPath);
if (!SupportedExtensions.Contains(ext, StringComparer.OrdinalIgnoreCase))
{
Console.WriteLine($"[ERRO] Extensao nao suportada: {ext} (use .mkv ou .mp4)");
return Finish(1);
}
var fileNameNoExt = Path.GetFileNameWithoutExtension(videoPath);
var folder = Path.GetDirectoryName(Path.GetFullPath(videoPath))!;
Console.WriteLine($"Arquivo: {fileNameNoExt}{ext}");
// As legendas alternativas sao salvas como "NomeDoArquivo.1.srt", "NomeDoArquivo.2.srt"
// etc. A legenda principal (sem sufixo numerico) nunca e removida por este comando.
var altPattern = new Regex(
"^" + Regex.Escape(fileNameNoExt) + @"\.\d+\.srt$",
RegexOptions.IgnoreCase);
var toDelete = Directory.EnumerateFiles(folder)
.Where(f => altPattern.IsMatch(Path.GetFileName(f)))
.OrderBy(f => f, StringComparer.OrdinalIgnoreCase)
.ToList();
if (toDelete.Count == 0)
{
Console.WriteLine("\nNenhuma legenda alternativa (.1.srt, .2.srt ...) encontrada para este arquivo.");
return Finish(0);
}
Console.WriteLine();
var removed = 0;
foreach (var path in toDelete)
{
try
{
File.Delete(path);
Console.WriteLine($" [removido] {Path.GetFileName(path)}");
removed++;
}
catch (Exception ex)
{
Console.WriteLine($" [ERRO] Falha ao remover {Path.GetFileName(path)}: {ex.Message}");
}
}
Console.WriteLine();
Console.WriteLine($"[OK] {removed} legenda(s) alternativa(s) removida(s).");
return Finish(0);
}
private static void PlayVideo(string videoPath)
{
try
{
Console.WriteLine();
Console.WriteLine("Abrindo o video no player padrao...");
Process.Start(new ProcessStartInfo(videoPath) { UseShellExecute = true });
}
catch (Exception ex)
{
Console.WriteLine($"[aviso] Nao foi possivel abrir o video automaticamente: {ex.Message}");
}
}
private static HttpClient CreateHttpClient()
{
var handler = new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.All
};
var http = new HttpClient(handler)
{
Timeout = TimeSpan.FromSeconds(30)
};
http.DefaultRequestHeaders.UserAgent.ParseAdd(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36");
http.DefaultRequestHeaders.Accept.ParseAdd("text/html,application/xhtml+xml");
return http;
}
// Segura a janela aberta (o menu do Explorer abre um console que fecharia sozinho)
// para o usuario ler de onde veio a legenda ou o erro.
private static int Finish(int code)
{
if (Console.IsInputRedirected)
return code;
Console.WriteLine("\nPressione qualquer tecla para fechar...");
try { Console.ReadKey(true); } catch (InvalidOperationException) { /* sem console interativo */ }
return code;
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int GetLongPathName(string shortPath, StringBuilder longPathBuffer, int bufferLength);
/// <summary>
/// Troca um nome curto 8.3 (STARTR~1.MKV) pelo nome longo real do arquivo. Se o
/// caminho ja for o nome longo, ou se o arquivo nao existir (ex: caminho digitado
/// errado), devolve o valor original sem erro.
/// </summary>
private static string ExpandLongPath(string path)
{
try
{
var buffer = new StringBuilder(260);
var length = GetLongPathName(path, buffer, buffer.Capacity);
if (length > buffer.Capacity)
{
// Nome longo nao coube no buffer inicial (caminho > MAX_PATH); tenta de novo
// com o tamanho exato que a API pediu.
buffer = new StringBuilder(length);
length = GetLongPathName(path, buffer, buffer.Capacity);
}
return length > 0 && length < buffer.Capacity ? buffer.ToString() : path;
}
catch (Exception)
{
// DllNotFoundException/EntryPointNotFoundException etc. nunca devem impedir
// o programa de rodar com o caminho original.
return path;
}
}
}