Skip to content

Commit

Permalink
优化合并排序
Browse files Browse the repository at this point in the history
  • Loading branch information
nilaoda committed Nov 20, 2022
1 parent 3f29e95 commit ec26127
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
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 @@ -18,7 +18,7 @@ namespace N_m3u8DL_RE.CommandLine
{
internal partial class CommandInvoker
{
public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20221117";
public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20221120";

[GeneratedRegex("((best|worst)\\d*|all)")]
private static partial Regex ForStrRegex();
Expand Down
18 changes: 9 additions & 9 deletions src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
}
}
//写出字幕
var files = FileDic.Values.Select(v => v!.ActualFilePath).OrderBy(s => s).ToArray();
var files = FileDic.OrderBy(s => s.Key.Index).Select(s => s.Value).Select(v => v!.ActualFilePath).ToArray();
foreach (var item in files) File.Delete(item);
FileDic.Clear();
var index = 0;
Expand Down Expand Up @@ -375,11 +375,11 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
if (sawVtt)
{
Logger.WarnMarkUp(ResString.fixingVTTmp4);
var mp4s = FileDic.Values.Select(v => v!.ActualFilePath).Where(p => p.EndsWith(".m4s")).OrderBy(s => s).ToArray();
var mp4s = FileDic.OrderBy(s => s.Key.Index).Select(s => s.Value).Select(v => v!.ActualFilePath).Where(p => p.EndsWith(".m4s")).ToArray();
var finalVtt = MP4VttUtil.ExtractSub(mp4s, timescale);
//写出字幕
var firstKey = FileDic.Keys.First();
var files = FileDic.Values.Select(v => v!.ActualFilePath).OrderBy(s => s).ToArray();
var files = FileDic.OrderBy(s => s.Key.Index).Select(s => s.Value).Select(v => v!.ActualFilePath).ToArray();
foreach (var item in files) File.Delete(item);
FileDic.Clear();
var index = 0;
Expand All @@ -405,11 +405,11 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
&& streamSpec.Extension != null && streamSpec.Extension.Contains("ttml"))
{
Logger.WarnMarkUp(ResString.fixingTTML);
var mp4s = FileDic.Values.Select(v => v!.ActualFilePath).Where(p => p.EndsWith(".ttml")).OrderBy(s => s).ToArray();
var mp4s = FileDic.OrderBy(s => s.Key.Index).Select(s => s.Value).Select(v => v!.ActualFilePath).Where(p => p.EndsWith(".ttml")).ToArray();
var finalVtt = MP4TtmlUtil.ExtractFromTTMLs(mp4s, 0);
//写出字幕
var firstKey = FileDic.Keys.First();
var files = FileDic.Values.Select(v => v!.ActualFilePath).OrderBy(s => s).ToArray();
var files = FileDic.OrderBy(s => s.Key.Index).Select(s => s.Value).Select(v => v!.ActualFilePath).ToArray();

//处理图形字幕
if (finalVtt.Cues.All(v => v.Payload.StartsWith("Base64::")))
Expand Down Expand Up @@ -455,11 +455,11 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
//var initFile = FileDic.Values.Where(v => Path.GetFileName(v!.ActualFilePath).StartsWith("_init")).FirstOrDefault();
//var iniFileBytes = File.ReadAllBytes(initFile!.ActualFilePath);
//var sawTtml = MP4TtmlUtil.CheckInit(iniFileBytes);
var mp4s = FileDic.Values.Select(v => v!.ActualFilePath).Where(p => p.EndsWith(".m4s")).OrderBy(s => s).ToArray();
var mp4s = FileDic.OrderBy(s => s.Key.Index).Select(s => s.Value).Select(v => v!.ActualFilePath).Where(p => p.EndsWith(".m4s")).ToArray();
var finalVtt = MP4TtmlUtil.ExtractFromMp4s(mp4s, 0);
//写出字幕
var firstKey = FileDic.Keys.First();
var files = FileDic.Values.Select(v => v!.ActualFilePath).OrderBy(s => s).ToArray();
var files = FileDic.OrderBy(s => s.Key.Index).Select(s => s.Value).Select(v => v!.ActualFilePath).ToArray();

//处理图形字幕
if (finalVtt.Cues.All(v => v.Payload.StartsWith("Base64::")))
Expand Down Expand Up @@ -503,14 +503,14 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
if (DownloaderConfig.MyOptions.BinaryMerge || streamSpec.MediaType == MediaType.SUBTITLES)
{
Logger.InfoMarkUp(ResString.binaryMerge);
var files = FileDic.Values.Select(v => v!.ActualFilePath).OrderBy(s => s).ToArray();
var files = FileDic.OrderBy(s => s.Key.Index).Select(s => s.Value).Select(v => v!.ActualFilePath).ToArray();
MergeUtil.CombineMultipleFilesIntoSingleFile(files, output);
mergeSuccess = true;
}
else
{
//ffmpeg合并
var files = FileDic.Values.Select(v => v!.ActualFilePath).OrderBy(s => s).ToArray();
var files = FileDic.OrderBy(s => s.Key.Index).Select(s => s.Value).Select(v => v!.ActualFilePath).ToArray();
Logger.InfoMarkUp(ResString.ffmpegMerge);
var ext = streamSpec.MediaType == MediaType.AUDIO ? "m4a" : "mp4";
var ffOut = Path.Combine(Path.GetDirectoryName(output)!, Path.GetFileNameWithoutExtension(output) + $".{ext}");
Expand Down
10 changes: 5 additions & 5 deletions src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
var (sawVtt, timescale) = MP4VttUtil.CheckInit(iniFileBytes);
if (sawVtt)
{
var mp4s = FileDic.Values.Select(v => v!.ActualFilePath).Where(p => p.EndsWith(".m4s")).OrderBy(s => s).ToArray();
var mp4s = FileDic.OrderBy(s => s.Key.Index).Select(s => s.Value).Select(v => v!.ActualFilePath).Where(p => p.EndsWith(".m4s")).ToArray();
if (firstSub)
{
currentVtt = MP4VttUtil.ExtractSub(mp4s, timescale);
Expand All @@ -394,7 +394,7 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
if (DownloaderConfig.MyOptions.AutoSubtitleFix && streamSpec.MediaType == Common.Enum.MediaType.SUBTITLES
&& streamSpec.Extension != null && streamSpec.Extension.Contains("ttml"))
{
var mp4s = FileDic.Values.Select(v => v!.ActualFilePath).Where(p => p.EndsWith(".ttml")).OrderBy(s => s).ToArray();
var mp4s = FileDic.OrderBy(s => s.Key.Index).Select(s => s.Value).Select(v => v!.ActualFilePath).Where(p => p.EndsWith(".ttml")).ToArray();
if (firstSub)
{
if (baseTimestamp != 0)
Expand All @@ -421,7 +421,7 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
//var initFile = FileDic.Values.Where(v => Path.GetFileName(v!.ActualFilePath).StartsWith("_init")).FirstOrDefault();
//var iniFileBytes = File.ReadAllBytes(initFile!.ActualFilePath);
//var sawTtml = MP4TtmlUtil.CheckInit(iniFileBytes);
var mp4s = FileDic.Values.Select(v => v!.ActualFilePath).Where(p => p.EndsWith(".m4s")).OrderBy(s => s).ToArray();
var mp4s = FileDic.OrderBy(s => s.Key.Index).Select(s => s.Value).Select(v => v!.ActualFilePath).Where(p => p.EndsWith(".m4s")).ToArray();
if (firstSub)
{
if (baseTimestamp != 0)
Expand Down Expand Up @@ -484,7 +484,7 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
if (streamSpec.MediaType != MediaType.SUBTITLES)
{
var initResult = streamSpec.Playlist!.MediaInit != null ? FileDic[streamSpec.Playlist!.MediaInit!]! : null;
var files = FileDic.Where(f => f.Key != streamSpec.Playlist!.MediaInit).Select(f => f.Value).Select(v => v!.ActualFilePath).OrderBy(s => s).ToArray();
var files = FileDic.Where(f => f.Key != streamSpec.Playlist!.MediaInit).OrderBy(s => s.Key.Index).Select(f => f.Value).Select(v => v!.ActualFilePath).ToArray();
if (initResult != null && mp4InitFile != "")
{
//shaka实时解密不需要init文件用于合并,mp4decrpyt需要
Expand Down Expand Up @@ -513,7 +513,7 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
else
{
var initResult = streamSpec.Playlist!.MediaInit != null ? FileDic[streamSpec.Playlist!.MediaInit!]! : null;
var files = FileDic.Select(f => f.Value).Select(v => v!.ActualFilePath).OrderBy(s => s).ToArray();
var files = FileDic.OrderBy(s => s.Key.Index).Select(f => f.Value).Select(v => v!.ActualFilePath).ToArray();
foreach (var inputFilePath in files)
{
if (!DownloaderConfig.MyOptions.LiveKeepSegments && !Path.GetFileName(inputFilePath).StartsWith("_init"))
Expand Down

0 comments on commit ec26127

Please sign in to comment.