Skip to content

Commit

Permalink
解决mp4decrypt解密中文文件名失败问题 (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilaoda committed Nov 29, 2024
1 parent 3081701 commit 8702d36
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/N_m3u8DL-RE.Common/Resource/ResString.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace N_m3u8DL_RE.Common.Resource;

public class ResString
public static class ResString
{
public static string CurrentLoc = "en-US";

Expand Down Expand Up @@ -125,6 +125,7 @@ public class ResString
public static string promptTitle => GetText("promptTitle");
public static string readingInfo => GetText("readingInfo");
public static string searchKey => GetText("searchKey");
public static string decryptionFailed => GetText("decryptionFailed");
public static string segmentCountCheckNotPass => GetText("segmentCountCheckNotPass");
public static string selectedStream => GetText("selectedStream");
public static string startDownloading => GetText("startDownloading");
Expand Down
6 changes: 6 additions & 0 deletions src/N_m3u8DL-RE.Common/Resource/StaticText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,12 @@ internal class StaticText
zhTW: "正在嘗試從文本文件搜尋KEY...",
enUS: "Trying to search for KEY from text file..."
),
["decryptionFailed"] = new TextContainer
(
zhCN: "解密失败",
zhTW: "解密失敗",
enUS: "Decryption failed"
),
["segmentCountCheckNotPass"] = new TextContainer
(
zhCN: "分片数量校验不通过, 共{}个,已下载{}.",
Expand Down
2 changes: 1 addition & 1 deletion src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace N_m3u8DL_RE.CommandLine;

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

[GeneratedRegex("((best|worst)\\d*|all)")]
private static partial Regex ForStrRegex();
Expand Down
38 changes: 29 additions & 9 deletions src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public static async Task<bool> DecryptAsync(DecryptEngine decryptEngine, string
var keyPairs = keys.ToList();
string? keyPair = null;
string? trackId = null;
string? tmpEncFile = null;
string? tmpDecFile = null;
string? workDir = null;

if (isMultiDRM)
{
Expand Down Expand Up @@ -75,11 +78,17 @@ public static async Task<bool> DecryptAsync(DecryptEngine decryptEngine, string
{
cmd = string.Join(" ", keyPairs.Select(k => $"--key {trackId}:{k.Split(':')[1]}"));
}
// 解决mp4decrypt中文问题 切换到源文件所在目录并改名再解密
workDir = Path.GetDirectoryName(source)!;
tmpEncFile = Path.Combine(workDir, $"{Guid.NewGuid()}{Path.GetExtension(source)}");
tmpDecFile = Path.Combine(workDir, $"{Path.GetFileNameWithoutExtension(tmpEncFile)}_dec{Path.GetExtension(tmpEncFile)}");
File.Move(source, tmpEncFile);
if (init != "")
{
cmd += $" --fragments-info \"{init}\" ";
var infoFile = Path.GetDirectoryName(init) == workDir ? Path.GetFileName(init) : init;
cmd += $" --fragments-info \"{infoFile}\" ";
}
cmd += $" \"{source}\" \"{dest}\"";
cmd += $" \"{Path.GetFileName(tmpEncFile)}\" \"{Path.GetFileName(tmpDecFile)}\"";
}
else
{
Expand All @@ -95,30 +104,41 @@ public static async Task<bool> DecryptAsync(DecryptEngine decryptEngine, string
cmd = $"-loglevel error -nostdin -decryption_key {keyPair.Split(':')[1]} -i \"{enc}\" -c copy \"{dest}\"";
}

await RunCommandAsync(bin, cmd);
var isSuccess = await RunCommandAsync(bin, cmd, workDir);

// mp4decrypt 还原文件改名操作
if (workDir is not null)
{
if (File.Exists(tmpEncFile)) File.Move(tmpEncFile, source);
if (File.Exists(tmpDecFile)) File.Move(tmpDecFile, dest);
}

if (File.Exists(dest) && new FileInfo(dest).Length > 0)
if (isSuccess)
{
if (tmpFile != "" && File.Exists(tmpFile)) File.Delete(tmpFile);
return true;
}


Logger.Error(ResString.decryptionFailed);
return false;
}

private static async Task RunCommandAsync(string name, string arg)
private static async Task<bool> RunCommandAsync(string name, string arg, string? workDir = null)
{
Logger.DebugMarkUp($"FileName: {name}");
Logger.DebugMarkUp($"Arguments: {arg}");
await Process.Start(new ProcessStartInfo()
var process = Process.Start(new ProcessStartInfo()
{
FileName = name,
Arguments = arg,
// RedirectStandardOutput = true,
// RedirectStandardError = true,
CreateNoWindow = true,
UseShellExecute = false
})!.WaitForExitAsync();
UseShellExecute = false,
WorkingDirectory = workDir
});
await process!.WaitForExitAsync();
return process.ExitCode == 0;
}

/// <summary>
Expand Down

0 comments on commit 8702d36

Please sign in to comment.