Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持TS格式混流 #466

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions 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) 20240630";
public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20241020";

[GeneratedRegex("((best|worst)\\d*|all)")]
private static partial Regex ForStrRegex();
Expand Down Expand Up @@ -438,7 +438,8 @@ private static List<OutputFile> ParseImports(ArgumentResult result)
var p = new ComplexParamParser(v);
//混流格式
var format = p.GetValue("format") ?? v.Split(':')[0]; //若未获取到,直接:前的字符串作为format解析
if (format != "mp4" && format != "mkv")
var parseResult = System.Enum.TryParse(format.ToUpperInvariant(), out MuxFormat muxFormat);
if (!parseResult)
{
result.ErrorMessage = $"format={format} not valid";
return null;
Expand Down Expand Up @@ -480,7 +481,7 @@ private static List<OutputFile> ParseImports(ArgumentResult result)
return new MuxOptions()
{
UseMkvmerge = muxer == "mkvmerge",
MuxToMp4 = format == "mp4",
MuxFormat = muxFormat,
KeepFiles = keep == "true",
SkipSubtitle = skipSub == "true",
BinPath = bin_path == "auto" ? null : bin_path
Expand Down
4 changes: 2 additions & 2 deletions src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -725,14 +725,14 @@ await Parallel.ForEachAsync(dic, async (kp, _) =>
}
OutputFiles.ForEach(f => Logger.WarnMarkUp($"[grey]{Path.GetFileName(f.FilePath).EscapeMarkup()}[/]"));
var saveDir = DownloaderConfig.MyOptions.SaveDir ?? Environment.CurrentDirectory;
var ext = DownloaderConfig.MyOptions.MuxOptions.MuxToMp4 ? ".mp4" : ".mkv";
var ext = OtherUtil.GetMuxExtension(DownloaderConfig.MyOptions.MuxOptions.MuxFormat);
var dirName = Path.GetFileName(DownloaderConfig.DirPrefix);
var outName = $"{dirName}.MUX";
var outPath = Path.Combine(saveDir, outName);
Logger.WarnMarkUp($"Muxing to [grey]{outName.EscapeMarkup()}{ext}[/]");
var result = false;
if (DownloaderConfig.MyOptions.MuxOptions.UseMkvmerge) result = MergeUtil.MuxInputsByMkvmerge(DownloaderConfig.MyOptions.MkvmergeBinaryPath!, OutputFiles.ToArray(), outPath);
else result = MergeUtil.MuxInputsByFFmpeg(DownloaderConfig.MyOptions.FFmpegBinaryPath!, OutputFiles.ToArray(), outPath, DownloaderConfig.MyOptions.MuxOptions.MuxToMp4, !DownloaderConfig.MyOptions.NoDateInfo);
else result = MergeUtil.MuxInputsByFFmpeg(DownloaderConfig.MyOptions.FFmpegBinaryPath!, OutputFiles.ToArray(), outPath, DownloaderConfig.MyOptions.MuxOptions.MuxFormat, !DownloaderConfig.MyOptions.NoDateInfo);
//完成后删除各轨道文件
if (result)
{
Expand Down
4 changes: 2 additions & 2 deletions src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -896,14 +896,14 @@ await Parallel.ForEachAsync(dic, options, async (kp, _) =>
}
OutputFiles.ForEach(f => Logger.WarnMarkUp($"[grey]{Path.GetFileName(f.FilePath).EscapeMarkup()}[/]"));
var saveDir = DownloaderConfig.MyOptions.SaveDir ?? Environment.CurrentDirectory;
var ext = DownloaderConfig.MyOptions.MuxOptions.MuxToMp4 ? ".mp4" : ".mkv";
var ext = OtherUtil.GetMuxExtension(DownloaderConfig.MyOptions.MuxOptions.MuxFormat);
var dirName = Path.GetFileName(DownloaderConfig.DirPrefix);
var outName = $"{dirName}.MUX";
var outPath = Path.Combine(saveDir, outName);
Logger.WarnMarkUp($"Muxing to [grey]{outName.EscapeMarkup()}{ext}[/]");
var result = false;
if (DownloaderConfig.MyOptions.MuxOptions.UseMkvmerge) result = MergeUtil.MuxInputsByMkvmerge(DownloaderConfig.MyOptions.MkvmergeBinaryPath!, OutputFiles.ToArray(), outPath);
else result = MergeUtil.MuxInputsByFFmpeg(DownloaderConfig.MyOptions.FFmpegBinaryPath!, OutputFiles.ToArray(), outPath, DownloaderConfig.MyOptions.MuxOptions.MuxToMp4, !DownloaderConfig.MyOptions.NoDateInfo);
else result = MergeUtil.MuxInputsByFFmpeg(DownloaderConfig.MyOptions.FFmpegBinaryPath!, OutputFiles.ToArray(), outPath, DownloaderConfig.MyOptions.MuxOptions.MuxFormat, !DownloaderConfig.MyOptions.NoDateInfo);
//完成后删除各轨道文件
if (result)
{
Expand Down
8 changes: 2 additions & 6 deletions src/N_m3u8DL-RE/Entity/MuxOptions.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using N_m3u8DL_RE.Enum;

namespace N_m3u8DL_RE.Entity
{
internal class MuxOptions
{
public bool UseMkvmerge { get; set; } = false;
public bool MuxToMp4 { get; set; } = false;
public MuxFormat MuxFormat { get; set; } = MuxFormat.MP4;
public bool KeepFiles { get; set; } = false;
public bool SkipSubtitle { get; set; } = false;
public string? BinPath { get; set; }
Expand Down
8 changes: 8 additions & 0 deletions src/N_m3u8DL-RE/Enum/MuxFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace N_m3u8DL_RE.Enum;

internal enum MuxFormat
{
MP4,
MKV,
TS,
}
17 changes: 5 additions & 12 deletions src/N_m3u8DL-RE/Enum/SubtitleFormat.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace N_m3u8DL_RE.Enum;

namespace N_m3u8DL_RE.Enum
internal enum SubtitleFormat
{
internal enum SubtitleFormat
{
VTT,
SRT
}
}
VTT,
SRT
}
22 changes: 9 additions & 13 deletions src/N_m3u8DL-RE/Util/MergeUtil.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
using N_m3u8DL_RE.Common.Log;
using N_m3u8DL_RE.Entity;
using Spectre.Console;
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using N_m3u8DL_RE.Enum;

namespace N_m3u8DL_RE.Util
{
Expand Down Expand Up @@ -186,9 +179,9 @@ public static bool MergeByFFmpeg(string binary, string[] files, string outputPat
return code == 0;
}

public static bool MuxInputsByFFmpeg(string binary, OutputFile[] files, string outputPath, bool mp4, bool dateinfo)
public static bool MuxInputsByFFmpeg(string binary, OutputFile[] files, string outputPath, MuxFormat muxFormat, bool dateinfo)
{
var ext = mp4 ? "mp4" : "mkv";
var ext = OtherUtil.GetMuxExtension(muxFormat);
string dateString = DateTime.Now.ToString("o");
StringBuilder command = new StringBuilder("-loglevel warning -nostdin -y -dn ");

Expand All @@ -206,10 +199,13 @@ public static bool MuxInputsByFFmpeg(string binary, OutputFile[] files, string o

var srt = files.Any(x => x.FilePath.EndsWith(".srt"));

if (mp4)
if (muxFormat == MuxFormat.MP4)
command.Append($" -strict unofficial -c:a copy -c:v copy -c:s mov_text "); //mp4不支持vtt/srt字幕,必须转换格式
else
else if (muxFormat == MuxFormat.TS)
command.Append($" -strict unofficial -c:a copy -c:v copy ");
else if (muxFormat == MuxFormat.MKV)
command.Append($" -strict unofficial -c:a copy -c:v copy -c:s {(srt ? "srt" : "webvtt")} ");
else throw new ArgumentException($"unknown format: {muxFormat}");

//CLEAN
command.Append(" -map_metadata -1 ");
Expand Down Expand Up @@ -254,7 +250,7 @@ public static bool MuxInputsByFFmpeg(string binary, OutputFile[] files, string o

if (dateinfo) command.Append($" -metadata date=\"{dateString}\" ");
command.Append($" -ignore_unknown -copy_unknown ");
command.Append($" \"{outputPath}.{ext}\"");
command.Append($" \"{outputPath}{ext}\"");

var code = InvokeFFmpeg(binary, command.ToString(), Environment.CurrentDirectory);

Expand Down
11 changes: 11 additions & 0 deletions src/N_m3u8DL-RE/Util/OtherUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,16 @@ public static string GetEnvironmentVariable(string key, string defaultValue = ""
{
return Environment.GetEnvironmentVariable(key) ?? defaultValue;
}

public static string GetMuxExtension(MuxFormat muxFormat)
{
return muxFormat switch
{
MuxFormat.MP4 => ".mp4",
MuxFormat.MKV => ".mkv",
MuxFormat.TS => ".ts",
_ => throw new ArgumentException($"unknown format: {muxFormat}")
};
}
}
}