From 0dde0bceb5f170887285c77105d57b95fe807ddb Mon Sep 17 00:00:00 2001 From: croire <1432593898@qq.com> Date: Sun, 16 Oct 2022 14:03:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81AV1=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E8=A7=86=E9=A2=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DownKyi.Core/BiliApi/BiliUtils/Constant.cs | 19 +++ .../VideoStream/Models/PlayUrlDashVideo.cs | 3 +- DownKyi.Core/Settings/Models/VideoSettings.cs | 2 +- .../Settings/SettingsManager.Video.cs | 8 +- DownKyi/Services/Download/DownloadService.cs | 7 +- DownKyi/Services/Utils.cs | 55 +++++---- .../ViewModels/Settings/ViewVideoViewModel.cs | 110 +++++++++--------- DownKyi/Views/Settings/ViewVideo.xaml | 1 + 8 files changed, 120 insertions(+), 85 deletions(-) diff --git a/DownKyi.Core/BiliApi/BiliUtils/Constant.cs b/DownKyi.Core/BiliApi/BiliUtils/Constant.cs index ccae7ad..c8a3fa2 100644 --- a/DownKyi.Core/BiliApi/BiliUtils/Constant.cs +++ b/DownKyi.Core/BiliApi/BiliUtils/Constant.cs @@ -19,6 +19,13 @@ namespace DownKyi.Core.BiliApi.BiliUtils new Quality { Name = "360P 流畅", Id = 16 }, }; + private static readonly List codecIds = new List + { + new Quality { Name = "H.264/AVC", Id = 7 }, + new Quality { Name = "H.265/HEVC", Id = 12 }, + new Quality { Name = "AV1", Id = 13 }, + }; + private static readonly List qualities = new List { new Quality { Name = "64K", Id = 30216 }, @@ -40,6 +47,18 @@ namespace DownKyi.Core.BiliApi.BiliUtils return new List(resolutions); } + /// + /// 获取视频编码代码 + /// + /// + public static List GetCodecIds() + { + // 使用深复制, + // 保证外部修改list后, + // 不会影响其他调用处 + return new List(codecIds); + } + /// /// 获取支持的视频音质 /// diff --git a/DownKyi.Core/BiliApi/VideoStream/Models/PlayUrlDashVideo.cs b/DownKyi.Core/BiliApi/VideoStream/Models/PlayUrlDashVideo.cs index 098f9bc..fab8044 100644 --- a/DownKyi.Core/BiliApi/VideoStream/Models/PlayUrlDashVideo.cs +++ b/DownKyi.Core/BiliApi/VideoStream/Models/PlayUrlDashVideo.cs @@ -30,6 +30,7 @@ namespace DownKyi.Core.BiliApi.VideoStream.Models // start_with_sap // SegmentBase // segment_base - // codecid + [JsonProperty("codecid")] + public int CodecId { get; set; } } } diff --git a/DownKyi.Core/Settings/Models/VideoSettings.cs b/DownKyi.Core/Settings/Models/VideoSettings.cs index 0cf431e..0eeec39 100644 --- a/DownKyi.Core/Settings/Models/VideoSettings.cs +++ b/DownKyi.Core/Settings/Models/VideoSettings.cs @@ -8,7 +8,7 @@ namespace DownKyi.Core.Settings.Models /// public class VideoSettings { - public VideoCodecs VideoCodecs { get; set; } = VideoCodecs.NONE; // AVC or HEVC + public int VideoCodecs { get; set; } = -1; // AVC or HEVC public int Quality { get; set; } = -1; // 画质 public int AudioQuality { get; set; } = -1; // 音质 public AllowStatus IsTranscodingFlvToMp4 { get; set; } = AllowStatus.NONE; // 是否将flv转为mp4 diff --git a/DownKyi.Core/Settings/SettingsManager.Video.cs b/DownKyi.Core/Settings/SettingsManager.Video.cs index 7a88cc4..c8133f8 100644 --- a/DownKyi.Core/Settings/SettingsManager.Video.cs +++ b/DownKyi.Core/Settings/SettingsManager.Video.cs @@ -9,7 +9,7 @@ namespace DownKyi.Core.Settings public partial class SettingsManager { // 设置优先下载的视频编码 - private readonly VideoCodecs videoCodecs = VideoCodecs.AVC; + private readonly int videoCodecs = 7; // 设置优先下载画质 private readonly int quality = 120; @@ -58,10 +58,10 @@ namespace DownKyi.Core.Settings /// 获取优先下载的视频编码 /// /// - public VideoCodecs GetVideoCodecs() + public int GetVideoCodecs() { appSettings = GetSettings(); - if (appSettings.Video.VideoCodecs == VideoCodecs.NONE) + if (appSettings.Video.VideoCodecs == -1) { // 第一次获取,先设置默认值 SetVideoCodecs(videoCodecs); @@ -75,7 +75,7 @@ namespace DownKyi.Core.Settings /// /// /// - public bool SetVideoCodecs(VideoCodecs videoCodecs) + public bool SetVideoCodecs(int videoCodecs) { appSettings.Video.VideoCodecs = videoCodecs; return SetSettings(); diff --git a/DownKyi/Services/Download/DownloadService.cs b/DownKyi/Services/Download/DownloadService.cs index 50d30cc..49a7c60 100644 --- a/DownKyi/Services/Download/DownloadService.cs +++ b/DownKyi/Services/Download/DownloadService.cs @@ -1,4 +1,5 @@ -using DownKyi.Core.BiliApi.VideoStream; +using DownKyi.Core.BiliApi.BiliUtils; +using DownKyi.Core.BiliApi.VideoStream; using DownKyi.Core.BiliApi.VideoStream.Models; using DownKyi.Core.Danmaku2Ass; using DownKyi.Core.FFmpeg; @@ -14,6 +15,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; +using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -115,7 +117,8 @@ namespace DownKyi.Services.Download PlayUrlDashVideo downloadVideo = null; foreach (PlayUrlDashVideo video in downloading.PlayUrl.Dash.Video) { - if (video.Id == downloading.Resolution.Id && Utils.GetVideoCodecName(video.Codecs) == downloading.VideoCodecName) + Quality codecs = Constant.GetCodecIds().FirstOrDefault(t => t.Id == video.CodecId); + if (video.Id == downloading.Resolution.Id && codecs.Name == downloading.VideoCodecName) { downloadVideo = video; break; diff --git a/DownKyi/Services/Utils.cs b/DownKyi/Services/Utils.cs index ec497bd..b3e055a 100644 --- a/DownKyi/Services/Utils.cs +++ b/DownKyi/Services/Utils.cs @@ -30,7 +30,7 @@ namespace DownKyi.Services // 获取设置 UserInfoSettings userInfo = SettingsManager.GetInstance().GetUserInfo(); int defaultQuality = SettingsManager.GetInstance().GetQuality(); - VideoCodecs videoCodecs = SettingsManager.GetInstance().GetVideoCodecs(); + int videoCodecs = SettingsManager.GetInstance().GetVideoCodecs(); int defaultAudioQuality = SettingsManager.GetInstance().GetAudioQuality(); // 未登录时,最高仅720P @@ -179,9 +179,10 @@ namespace DownKyi.Services /// /// /// - private static List GetVideoQualityList(PlayUrl playUrl, UserInfoSettings userInfo, int defaultQuality, VideoCodecs videoCodecs) + private static List GetVideoQualityList(PlayUrl playUrl, UserInfoSettings userInfo, int defaultQuality, int videoCodecs) { List videoQualityList = new List(); + List codeIds = Constant.GetCodecIds(); if (playUrl.Dash.Video == null) { @@ -209,7 +210,8 @@ namespace DownKyi.Services // 寻找是否已存在这个画质 // 不存在则添加,存在则修改 - string codecName = GetVideoCodecName(video.Codecs); + //string codecName = GetVideoCodecName(video.Codecs); + string codecName = codeIds.FirstOrDefault(t => t.Id == video.CodecId).Name; VideoQuality videoQualityExist = videoQualityList.FirstOrDefault(t => t.Quality == video.Id); if (videoQualityExist == null) { @@ -249,24 +251,29 @@ namespace DownKyi.Services } // 设置选中的视频编码 - switch (videoCodecs) + //switch (videoCodecs) + //{ + // case VideoCodecs.AVC: + // if (videoQualityList[videoQualityList.IndexOf(selectedVideoQuality)].VideoCodecList.Contains("H.264/AVC")) + // { + // videoQualityList[videoQualityList.IndexOf(selectedVideoQuality)].SelectedVideoCodec = "H.264/AVC"; + // } + // break; + // case VideoCodecs.HEVC: + // if (videoQualityList[videoQualityList.IndexOf(selectedVideoQuality)].VideoCodecList.Contains("H.265/HEVC")) + // { + // videoQualityList[videoQualityList.IndexOf(selectedVideoQuality)].SelectedVideoCodec = "H.265/HEVC"; + // } + // break; + // case VideoCodecs.NONE: + // break; + // default: + // break; + //} + string videoCodecsName = codeIds.FirstOrDefault(t => t.Id == videoCodecs).Name; + if (videoQualityList[videoQualityList.IndexOf(selectedVideoQuality)].VideoCodecList.Contains(videoCodecsName)) { - case VideoCodecs.AVC: - if (videoQualityList[videoQualityList.IndexOf(selectedVideoQuality)].VideoCodecList.Contains("H.264/AVC")) - { - videoQualityList[videoQualityList.IndexOf(selectedVideoQuality)].SelectedVideoCodec = "H.264/AVC"; - } - break; - case VideoCodecs.HEVC: - if (videoQualityList[videoQualityList.IndexOf(selectedVideoQuality)].VideoCodecList.Contains("H.265/HEVC")) - { - videoQualityList[videoQualityList.IndexOf(selectedVideoQuality)].SelectedVideoCodec = "H.265/HEVC"; - } - break; - case VideoCodecs.NONE: - break; - default: - break; + videoQualityList[videoQualityList.IndexOf(selectedVideoQuality)].SelectedVideoCodec = videoCodecsName; } } @@ -279,10 +286,10 @@ namespace DownKyi.Services /// /// /// - internal static string GetVideoCodecName(string origin) - { - return origin.Contains("avc") ? "H.264/AVC" : origin.Contains("hev") ? "H.265/HEVC" : origin.Contains("dvh") || origin.Contains("hvc") ? "Dolby Vision" : ""; - } + //internal static string GetVideoCodecName(string origin) + //{ + // return origin.Contains("avc") ? "H.264/AVC" : origin.Contains("hev") ? "H.265/HEVC" : origin.Contains("dvh") || origin.Contains("hvc") ? "Dolby Vision" : ""; + //} } } diff --git a/DownKyi/ViewModels/Settings/ViewVideoViewModel.cs b/DownKyi/ViewModels/Settings/ViewVideoViewModel.cs index 4ff0b51..3007a94 100644 --- a/DownKyi/ViewModels/Settings/ViewVideoViewModel.cs +++ b/DownKyi/ViewModels/Settings/ViewVideoViewModel.cs @@ -24,15 +24,15 @@ namespace DownKyi.ViewModels.Settings #region 页面属性申明 - private List videoCodecs; - public List VideoCodecs + private List videoCodecs; + public List VideoCodecs { get => videoCodecs; set => SetProperty(ref videoCodecs, value); } - private string selectedVideoCodec; - public string SelectedVideoCodec + private Quality selectedVideoCodec; + public Quality SelectedVideoCodec { get => selectedVideoCodec; set => SetProperty(ref selectedVideoCodec, value); @@ -186,11 +186,12 @@ namespace DownKyi.ViewModels.Settings #region 属性初始化 // 优先下载的视频编码 - VideoCodecs = new List - { - "H.264/AVC", - "H.265/HEVC", - }; + VideoCodecs = Constant.GetCodecIds(); + //VideoCodecs = new List + //{ + // "H.264/AVC", + // "H.265/HEVC", + //}; // 优先下载画质 VideoQualityList = Constant.GetResolutions(); @@ -255,8 +256,9 @@ namespace DownKyi.ViewModels.Settings isOnNavigatedTo = true; // 优先下载的视频编码 - VideoCodecs videoCodecs = SettingsManager.GetInstance().GetVideoCodecs(); - SelectedVideoCodec = GetVideoCodecsString(videoCodecs); + int videoCodecs = SettingsManager.GetInstance().GetVideoCodecs(); + //SelectedVideoCodec = GetVideoCodecsString(videoCodecs); + SelectedVideoCodec = VideoCodecs.FirstOrDefault(t => { return t.Id == videoCodecs; }); // 优先下载画质 int quality = SettingsManager.GetInstance().GetQuality(); @@ -317,18 +319,20 @@ namespace DownKyi.ViewModels.Settings #region 命令申明 // 优先下载的视频编码事件 - private DelegateCommand videoCodecsCommand; - public DelegateCommand VideoCodecsCommand => videoCodecsCommand ?? (videoCodecsCommand = new DelegateCommand(ExecuteVideoCodecsCommand)); + private DelegateCommand videoCodecsCommand; + public DelegateCommand VideoCodecsCommand => videoCodecsCommand ?? (videoCodecsCommand = new DelegateCommand(ExecuteVideoCodecsCommand)); /// /// 优先下载的视频编码事件 /// /// - private void ExecuteVideoCodecsCommand(string parameter) + private void ExecuteVideoCodecsCommand(object parameter) { - VideoCodecs videoCodecs = GetVideoCodecs(parameter); + //VideoCodecs videoCodecs = GetVideoCodecs(parameter); - bool isSucceed = SettingsManager.GetInstance().SetVideoCodecs(videoCodecs); + if (!(parameter is Quality videoCodecs)) { return; } + + bool isSucceed = SettingsManager.GetInstance().SetVideoCodecs(videoCodecs.Id); PublishTip(isSucceed); } @@ -676,49 +680,49 @@ namespace DownKyi.ViewModels.Settings /// /// /// - private string GetVideoCodecsString(VideoCodecs videoCodecs) - { - string codec; - switch (videoCodecs) - { - case Core.Settings.VideoCodecs.NONE: - codec = ""; - break; - case Core.Settings.VideoCodecs.AVC: - codec = "H.264/AVC"; - break; - case Core.Settings.VideoCodecs.HEVC: - codec = "H.265/HEVC"; - break; - default: - codec = ""; - break; - } - return codec; - } + //private string GetVideoCodecsString(VideoCodecs videoCodecs) + //{ + // string codec; + // switch (videoCodecs) + // { + // case Core.Settings.VideoCodecs.NONE: + // codec = ""; + // break; + // case Core.Settings.VideoCodecs.AVC: + // codec = "H.264/AVC"; + // break; + // case Core.Settings.VideoCodecs.HEVC: + // codec = "H.265/HEVC"; + // break; + // default: + // codec = ""; + // break; + // } + // return codec; + //} /// /// 返回VideoCodecs /// /// /// - private VideoCodecs GetVideoCodecs(string str) - { - VideoCodecs videoCodecs; - switch (str) - { - case "H.264/AVC": - videoCodecs = Core.Settings.VideoCodecs.AVC; - break; - case "H.265/HEVC": - videoCodecs = Core.Settings.VideoCodecs.HEVC; - break; - default: - videoCodecs = Core.Settings.VideoCodecs.NONE; - break; - } - return videoCodecs; - } + //private VideoCodecs GetVideoCodecs(string str) + //{ + // VideoCodecs videoCodecs; + // switch (str) + // { + // case "H.264/AVC": + // videoCodecs = Core.Settings.VideoCodecs.AVC; + // break; + // case "H.265/HEVC": + // videoCodecs = Core.Settings.VideoCodecs.HEVC; + // break; + // default: + // videoCodecs = Core.Settings.VideoCodecs.NONE; + // break; + // } + // return videoCodecs; + //} /// /// 保存下载视频内容到设置 diff --git a/DownKyi/Views/Settings/ViewVideo.xaml b/DownKyi/Views/Settings/ViewVideo.xaml index 4511b48..10eca51 100644 --- a/DownKyi/Views/Settings/ViewVideo.xaml +++ b/DownKyi/Views/Settings/ViewVideo.xaml @@ -23,6 +23,7 @@ Name="nameVideoCodecs" Width="120" VerticalContentAlignment="Center" + DisplayMemberPath="Name" ItemsSource="{Binding VideoCodecs}" SelectedValue="{Binding SelectedVideoCodec}">