直播录制到达限制后允许自动混流

This commit is contained in:
nilaoda 2022-10-31 23:54:56 +08:00
parent b7c1515971
commit 27191240f4
3 changed files with 61 additions and 22 deletions

View File

@ -49,24 +49,6 @@ namespace N_m3u8DL_RE.DownloadManager
} }
} }
//若该文件夹为空,删除,同时判断其父文件夹,直到遇到根目录或不为空的目录
private void SafeDeleteDir(string dirPath)
{
if (string.IsNullOrEmpty(dirPath) || !Directory.Exists(dirPath))
return;
var parent = Path.GetDirectoryName(dirPath)!;
if (!Directory.EnumerateFileSystemEntries(dirPath).Any())
{
Directory.Delete(dirPath);
}
else
{
return;
}
SafeDeleteDir(parent);
}
//从文件读取KEY //从文件读取KEY
private async Task SearchKeyAsync(string? currentKID) private async Task SearchKeyAsync(string? currentKID)
{ {
@ -532,7 +514,7 @@ namespace N_m3u8DL_RE.DownloadManager
{ {
File.Delete(file); File.Delete(file);
} }
SafeDeleteDir(tmpDir); OtherUtil.SafeDeleteDir(tmpDir);
} }
//重新读取init信息 //重新读取init信息
@ -650,7 +632,7 @@ namespace N_m3u8DL_RE.DownloadManager
Logger.WarnMarkUp("[grey]Cleaning files...[/]"); Logger.WarnMarkUp("[grey]Cleaning files...[/]");
OutputFiles.ForEach(f => File.Delete(f.FilePath)); OutputFiles.ForEach(f => File.Delete(f.FilePath));
var tmpDir = DownloaderConfig.MyOptions.TmpDir ?? Environment.CurrentDirectory; var tmpDir = DownloaderConfig.MyOptions.TmpDir ?? Environment.CurrentDirectory;
SafeDeleteDir(tmpDir); OtherUtil.SafeDeleteDir(tmpDir);
} }
else Logger.ErrorMarkUp($"Mux failed"); else Logger.ErrorMarkUp($"Mux failed");
//判断是否要改名 //判断是否要改名

View File

@ -24,6 +24,7 @@ namespace N_m3u8DL_RE.DownloadManager
DownloaderConfig DownloaderConfig; DownloaderConfig DownloaderConfig;
StreamExtractor StreamExtractor; StreamExtractor StreamExtractor;
List<StreamSpec> SelectedSteams; List<StreamSpec> SelectedSteams;
List<OutputFile> OutputFiles = new();
DateTime NowDateTime; DateTime NowDateTime;
DateTime? PublishDateTime; DateTime? PublishDateTime;
bool STOP_FLAG = false; bool STOP_FLAG = false;
@ -533,6 +534,15 @@ namespace N_m3u8DL_RE.DownloadManager
if (fileOutputStream != null) if (fileOutputStream != null)
{ {
//记录所有文件信息
OutputFiles.Add(new OutputFile()
{
Index = task.Id,
FilePath = fileOutputStream.Name,
LangCode = streamSpec.Language,
Description = streamSpec.Name,
Mediainfos = mediaInfos
});
fileOutputStream.Close(); fileOutputStream.Close();
fileOutputStream.Dispose(); fileOutputStream.Dispose();
} }
@ -685,9 +695,38 @@ namespace N_m3u8DL_RE.DownloadManager
var success = Results.Values.All(v => v == true); var success = Results.Values.All(v => v == true);
//混流 //混流
if (success && DownloaderConfig.MyOptions.MuxAfterDone) if (success && DownloaderConfig.MyOptions.MuxAfterDone && OutputFiles.Count > 0)
{ {
Logger.Error("Not supported yet!"); OutputFiles = OutputFiles.OrderBy(o => o.Index).ToList();
if (DownloaderConfig.MyOptions.MuxImports != null)
{
OutputFiles.AddRange(DownloaderConfig.MyOptions.MuxImports);
}
OutputFiles.ForEach(f => Logger.WarnMarkUp($"[grey]{Path.GetFileName(f.FilePath).EscapeMarkup()}[/]"));
var saveDir = DownloaderConfig.MyOptions.SaveDir ?? Environment.CurrentDirectory;
var ext = DownloaderConfig.MyOptions.MuxToMp4 ? ".mp4" : ".mkv";
var outName = $"{DownloaderConfig.MyOptions.SaveName ?? NowDateTime.ToString("yyyy-MM-dd_HH-mm-ss")}.MUX";
var outPath = Path.Combine(saveDir, outName);
Logger.WarnMarkUp($"Muxing to [grey]{outName.EscapeMarkup()}{ext}[/]");
var result = false;
if (DownloaderConfig.MyOptions.UseMkvmerge) result = MergeUtil.MuxInputsByMkvmerge(DownloaderConfig.MyOptions.MkvmergeBinaryPath!, OutputFiles.ToArray(), outPath);
else result = MergeUtil.MuxInputsByFFmpeg(DownloaderConfig.MyOptions.FFmpegBinaryPath!, OutputFiles.ToArray(), outPath, DownloaderConfig.MyOptions.MuxToMp4);
//完成后删除各轨道文件
if (result && !DownloaderConfig.MyOptions.MuxKeepFiles)
{
Logger.WarnMarkUp("[grey]Cleaning files...[/]");
OutputFiles.ForEach(f => File.Delete(f.FilePath));
var tmpDir = DownloaderConfig.MyOptions.TmpDir ?? Environment.CurrentDirectory;
OtherUtil.SafeDeleteDir(tmpDir);
}
else Logger.ErrorMarkUp($"Mux failed");
//判断是否要改名
var newPath = Path.ChangeExtension(outPath, ext);
if (result && !File.Exists(newPath))
{
Logger.WarnMarkUp($"Rename to [grey]{Path.GetFileName(newPath).EscapeMarkup()}[/]");
File.Move(outPath + ext, newPath);
}
} }
return success; return success;

View File

@ -118,5 +118,23 @@ namespace N_m3u8DL_RE.Util
return new TimeSpan(days, hours, mins, secs); return new TimeSpan(days, hours, mins, secs);
} }
//若该文件夹为空,删除,同时判断其父文件夹,直到遇到根目录或不为空的目录
public static void SafeDeleteDir(string dirPath)
{
if (string.IsNullOrEmpty(dirPath) || !Directory.Exists(dirPath))
return;
var parent = Path.GetDirectoryName(dirPath)!;
if (!Directory.EnumerateFileSystemEntries(dirPath).Any())
{
Directory.Delete(dirPath);
}
else
{
return;
}
SafeDeleteDir(parent);
}
} }
} }