增加`--ad-keyword`选项过滤广告URL

#221 #204
This commit is contained in:
nilaoda 2023-07-12 21:51:25 +08:00
parent e3c4cbb01b
commit bde15e58c7
6 changed files with 63 additions and 2 deletions

View File

@ -12,6 +12,7 @@ namespace N_m3u8DL_RE.Common.Resource
public readonly static string ReLiveTs = "<RE_LIVE_TS>";
public static string customRangeWarn { get => GetText("customRangeWarn"); }
public static string customRangeFound { get => GetText("customRangeFound"); }
public static string customAdKeywordsFound { get => GetText("customAdKeywordsFound"); }
public static string customRangeInvalid { get => GetText("customRangeInvalid"); }
public static string autoBinaryMerge { get => GetText("autoBinaryMerge"); }
public static string autoBinaryMerge2 { get => GetText("autoBinaryMerge2"); }
@ -31,6 +32,7 @@ namespace N_m3u8DL_RE.Common.Resource
public static string cmd_ffmpegBinaryPath { get => GetText("cmd_ffmpegBinaryPath"); }
public static string cmd_mkvmergeBinaryPath { get => GetText("cmd_mkvmergeBinaryPath"); }
public static string cmd_baseUrl { get => GetText("cmd_baseUrl"); }
public static string cmd_adKeyword { get => GetText("cmd_adKeyword"); }
public static string cmd_moreHelp { get => GetText("cmd_moreHelp"); }
public static string cmd_header { get => GetText("cmd_header"); }
public static string cmd_muxImport { get => GetText("cmd_muxImport"); }

View File

@ -22,6 +22,12 @@ namespace N_m3u8DL_RE.Common.Resource
zhTW: "自定義下載範圍無效",
enUS: "User customed range invalid"
),
["customAdKeywordsFound"] = new TextContainer
(
zhCN: "用户自定义广告分片URL关键字",
zhTW: "用戶自定義廣告分片URL關鍵字",
enUS: "User customed Ad keyword: "
),
["customRangeFound"] = new TextContainer
(
zhCN: "用户自定义下载范围:",
@ -346,6 +352,12 @@ namespace N_m3u8DL_RE.Common.Resource
zhTW: "手動設置直播列表刷新間隔",
enUS: "Manually set the live playlist refresh interval"
),
["cmd_adKeyword"] = new TextContainer
(
zhCN: "设置广告分片的URL关键字(正则表达式)",
zhTW: "設置廣告分片的URL關鍵字(正則表達式)",
enUS: "Set URL keywords (regular expressions) for AD segments"
),
["cmd_liveTakeCount"] = new TextContainer
(
zhCN: "手动设置录制直播时首次获取分片的数量",

View File

@ -18,7 +18,7 @@ namespace N_m3u8DL_RE.CommandLine
{
internal partial class CommandInvoker
{
public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20230707";
public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20230712";
[GeneratedRegex("((best|worst)\\d*|all)")]
private static partial Regex ForStrRegex();
@ -57,6 +57,8 @@ namespace N_m3u8DL_RE.CommandLine
private readonly static Option<string?> BaseUrl = new(new string[] { "--base-url" }, description: ResString.cmd_baseUrl);
private readonly static Option<bool> ConcurrentDownload = new(new string[] { "-mt", "--concurrent-download" }, description: ResString.cmd_concurrentDownload, getDefaultValue: () => false);
private readonly static Option<bool> NoLog = new(new string[] { "--no-log" }, description: ResString.cmd_noLog, getDefaultValue: () => false);
private readonly static Option<string[]?> AdKeywords = new(new string[] { "--ad-keyword" }, description: ResString.cmd_adKeyword) { ArgumentHelpName = "REG" };
//代理选项
private readonly static Option<bool> UseSystemProxy = new(new string[] { "--use-system-proxy" }, description: ResString.cmd_useSystemProxy, getDefaultValue: () => true);
@ -499,6 +501,7 @@ namespace N_m3u8DL_RE.CommandLine
LiveTakeCount = bindingContext.ParseResult.GetValueForOption(LiveTakeCount),
NoDateInfo = bindingContext.ParseResult.GetValueForOption(NoDateInfo),
NoLog = bindingContext.ParseResult.GetValueForOption(NoLog),
AdKeywords = bindingContext.ParseResult.GetValueForOption(AdKeywords),
};
if (bindingContext.ParseResult.HasOption(CustomHLSMethod)) option.CustomHLSMethod = bindingContext.ParseResult.GetValueForOption(CustomHLSMethod);
@ -564,7 +567,7 @@ namespace N_m3u8DL_RE.CommandLine
MuxAfterDone,
CustomHLSMethod, CustomHLSKey, CustomHLSIv, UseSystemProxy, CustomProxy, CustomRange, TaskStartAt,
LivePerformAsVod, LiveRealTimeMerge, LiveKeepSegments, LivePipeMux, LiveFixVttByAudio, LiveRecordLimit, LiveWaitTime, LiveTakeCount,
MuxImports, VideoFilter, AudioFilter, SubtitleFilter, DropVideoFilter, DropAudioFilter, DropSubtitleFilter, MoreHelp
MuxImports, VideoFilter, AudioFilter, SubtitleFilter, DropVideoFilter, DropAudioFilter, DropSubtitleFilter, AdKeywords, MoreHelp
};
rootCommand.TreatUnmatchedTokensAsErrors = true;

View File

@ -17,6 +17,10 @@ namespace N_m3u8DL_RE.CommandLine
/// </summary>
public Dictionary<string, string> Headers { get; set; } = new Dictionary<string, string>();
/// <summary>
/// See: <see cref="CommandInvoker.AdKeywords"/>.
/// </summary>
public string[]? AdKeywords { get; set; }
/// <summary>
/// See: <see cref="CommandInvoker.Keys"/>.
/// </summary>
public string[]? Keys { get; set; }

View File

@ -304,6 +304,9 @@ namespace N_m3u8DL_RE
if (!livingFlag)
FilterUtil.ApplyCustomRange(selectedStreams, option.CustomRange);
//应用用户自定义的广告分片关键字
FilterUtil.CleanAd(selectedStreams, option.AdKeywords);
//记录文件
extractor.RawFiles["meta_selected.json"] = GlobalUtil.ConvertToJson(selectedStreams);

View File

@ -8,6 +8,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace N_m3u8DL_RE.Util
@ -227,5 +228,41 @@ namespace N_m3u8DL_RE.Util
stream.SkippedDuration = skippedDur;
}
}
/// <summary>
/// 根据用户输入,清除广告分片
/// </summary>
/// <param name="selectedSteams"></param>
/// <param name="customRange"></param>
public static void CleanAd(List<StreamSpec> selectedSteams, string[]? keywords)
{
if (keywords == null) return;
var regList = keywords.Select(s => new Regex(s));
foreach ( var reg in regList)
{
Logger.InfoMarkUp($"{ResString.customAdKeywordsFound}[Cyan underline]{reg}[/]");
}
foreach (var stream in selectedSteams)
{
if (stream.Playlist == null) continue;
foreach (var part in stream.Playlist.MediaParts)
{
//没有找到广告分片
if (part.MediaSegments.All(x => regList.All(reg => !reg.IsMatch(x.Url))))
{
continue;
}
//找到广告分片 清理
else
{
part.MediaSegments = part.MediaSegments.Where(x => regList.All(reg => !reg.IsMatch(x.Url))).ToList();
}
}
//清理已经为空的 part
stream.Playlist.MediaParts = stream.Playlist.MediaParts.Where(x => x.MediaSegments.Count > 0).ToList();
}
}
}
}