Skip to content

Commit 87b0696

Browse files
committed
解决mp4decrypt解密中文文件名失败问题 (#524)
1 parent 3081701 commit 87b0696

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

src/N_m3u8DL-RE.Common/Resource/ResString.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace N_m3u8DL_RE.Common.Resource;
22

3-
public class ResString
3+
public static class ResString
44
{
55
public static string CurrentLoc = "en-US";
66

@@ -125,6 +125,7 @@ public class ResString
125125
public static string promptTitle => GetText("promptTitle");
126126
public static string readingInfo => GetText("readingInfo");
127127
public static string searchKey => GetText("searchKey");
128+
public static string decryptionFailed => GetText("decryptionFailed");
128129
public static string segmentCountCheckNotPass => GetText("segmentCountCheckNotPass");
129130
public static string selectedStream => GetText("selectedStream");
130131
public static string startDownloading => GetText("startDownloading");

src/N_m3u8DL-RE.Common/Resource/StaticText.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,12 @@ internal class StaticText
910910
zhTW: "正在嘗試從文本文件搜尋KEY...",
911911
enUS: "Trying to search for KEY from text file..."
912912
),
913+
["decryptionFailed"] = new TextContainer
914+
(
915+
zhCN: "解密失败",
916+
zhTW: "解密失敗",
917+
enUS: "Decryption failed"
918+
),
913919
["segmentCountCheckNotPass"] = new TextContainer
914920
(
915921
zhCN: "分片数量校验不通过, 共{}个,已下载{}.",

src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace N_m3u8DL_RE.CommandLine;
1717

1818
internal partial class CommandInvoker
1919
{
20-
public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20241129";
20+
public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20241130";
2121

2222
[GeneratedRegex("((best|worst)\\d*|all)")]
2323
private static partial Regex ForStrRegex();

src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public static async Task<bool> DecryptAsync(DecryptEngine decryptEngine, string
1717
var keyPairs = keys.ToList();
1818
string? keyPair = null;
1919
string? trackId = null;
20+
string? tmpEncFile = null;
21+
string? tmpDecFile = null;
22+
string? workDir = null;
2023

2124
if (isMultiDRM)
2225
{
@@ -79,7 +82,12 @@ public static async Task<bool> DecryptAsync(DecryptEngine decryptEngine, string
7982
{
8083
cmd += $" --fragments-info \"{init}\" ";
8184
}
82-
cmd += $" \"{source}\" \"{dest}\"";
85+
// 解决mp4decrypt中文问题 切换到源文件所在目录并改名再解密
86+
workDir = Path.GetDirectoryName(source)!;
87+
tmpEncFile = Path.Combine(workDir, $"{Guid.NewGuid()}{Path.GetExtension(source)}");
88+
tmpDecFile = Path.Combine(workDir, $"{Path.GetFileNameWithoutExtension(tmpEncFile)}_dec{Path.GetExtension(tmpEncFile)}");
89+
File.Move(source, tmpEncFile);
90+
cmd += $" \"{Path.GetFileName(tmpEncFile)}\" \"{Path.GetFileName(tmpDecFile)}\"";
8391
}
8492
else
8593
{
@@ -95,30 +103,41 @@ public static async Task<bool> DecryptAsync(DecryptEngine decryptEngine, string
95103
cmd = $"-loglevel error -nostdin -decryption_key {keyPair.Split(':')[1]} -i \"{enc}\" -c copy \"{dest}\"";
96104
}
97105

98-
await RunCommandAsync(bin, cmd);
106+
var isSuccess = await RunCommandAsync(bin, cmd, workDir);
107+
108+
// mp4decrypt 还原文件改名操作
109+
if (workDir is not null)
110+
{
111+
if (File.Exists(tmpEncFile)) File.Move(tmpEncFile, source);
112+
if (File.Exists(tmpDecFile)) File.Move(tmpDecFile, dest);
113+
}
99114

100-
if (File.Exists(dest) && new FileInfo(dest).Length > 0)
115+
if (isSuccess)
101116
{
102117
if (tmpFile != "" && File.Exists(tmpFile)) File.Delete(tmpFile);
103118
return true;
104119
}
105-
120+
121+
Logger.Error(ResString.decryptionFailed);
106122
return false;
107123
}
108124

109-
private static async Task RunCommandAsync(string name, string arg)
125+
private static async Task<bool> RunCommandAsync(string name, string arg, string? workDir = null)
110126
{
111127
Logger.DebugMarkUp($"FileName: {name}");
112128
Logger.DebugMarkUp($"Arguments: {arg}");
113-
await Process.Start(new ProcessStartInfo()
129+
var process = Process.Start(new ProcessStartInfo()
114130
{
115131
FileName = name,
116132
Arguments = arg,
117133
// RedirectStandardOutput = true,
118134
// RedirectStandardError = true,
119135
CreateNoWindow = true,
120-
UseShellExecute = false
121-
})!.WaitForExitAsync();
136+
UseShellExecute = false,
137+
WorkingDirectory = workDir
138+
});
139+
await process!.WaitForExitAsync();
140+
return process.ExitCode == 0;
122141
}
123142

124143
/// <summary>

0 commit comments

Comments
 (0)