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

支持字幕下载(不包括 AI 字幕) #540

Merged
merged 1 commit into from
Sep 14, 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<DropDownButton
x:Name="Btn"
HorizontalAlignment="Stretch"
Click="OnBtnClick"
Style="{StaticResource AccentDropDownButtonStyle}">
<StackPanel>
<TextBlock Text="{ext:Locale Name=DownloadVideo}" />
Expand Down
26 changes: 22 additions & 4 deletions src/Desktop/BiliCopilot.UI/Controls/Player/DownloadButton.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ protected override void OnControlLoaded()

_viewModel = ViewModel;
ViewModel.MetaInitialized += OnMetaInitialized;
InitializeDownloadFlyout();
}

/// <inheritdoc/>
Expand Down Expand Up @@ -59,11 +58,11 @@ private void OnViewModelPropertyChanged(DependencyObject sender, DependencyPrope

_viewModel = ViewModel;
_viewModel.MetaInitialized += OnMetaInitialized;
InitializeDownloadFlyout();
DownloadFlyout.Items.Clear();
}

private void OnMetaInitialized(object? sender, EventArgs e)
=> InitializeDownloadFlyout();
=> DownloadFlyout.Items.Clear();

private void InitializeDownloadFlyout()
{
Expand All @@ -72,7 +71,6 @@ private void InitializeDownloadFlyout()
return;
}

DownloadFlyout.Items.Clear();
var formatHeader = MenuFlyoutItemHeader.LoadContent() as MenuFlyoutSeparator;
formatHeader.Tag = ResourceToolkit.GetLocalizedString(Models.Constants.StringNames.Quality);
DownloadFlyout.Items.Add(formatHeader);
Expand Down Expand Up @@ -129,6 +127,18 @@ private void InitializeDownloadFlyout()

DownloadFlyout.Items.Add(downloadCoverItem);
DownloadFlyout.Items.Add(downloadDanmakuItem);

if (ViewModel.HasAvailableSubtitle)
{
var downloadSubtitleItem = new MenuFlyoutItem
{
MinWidth = this.ActualWidth,
Text = ResourceToolkit.GetLocalizedString(Models.Constants.StringNames.DownloadSubtitle),
Command = ViewModel.DownloadSubtitleCommand,
};

DownloadFlyout.Items.Add(downloadSubtitleItem);
}
}

private void ShowPartsSelectionTip()
Expand All @@ -149,6 +159,14 @@ private void OnSelectionSubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubm
SelectionBox.Text = string.Empty;
ViewModel.BatchDownloadSelectedPartsCommand.Execute(text);
}

private void OnBtnClick(object sender, RoutedEventArgs e)
{
if (DownloadFlyout.Items.Count == 0)
{
InitializeDownloadFlyout();
}
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
MinWidth="120"
LargeChange="50"
Maximum="300"
Minimum="0"
Minimum="3"
SmallChange="10"
SpinButtonPlacementMode="Compact"
Value="{x:Bind ViewModel.SingleFastForwardAndRewindSpan, Mode=TwoWay}" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2663,4 +2663,7 @@
<data name="OpenUpdate" xml:space="preserve">
<value>查看更新</value>
</data>
<data name="DownloadSubtitle" xml:space="preserve">
<value>下载字幕</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public sealed partial class DownloadViewModel
[ObservableProperty]
private IReadOnlyCollection<EpisodeInformation>? _episodes;

[ObservableProperty]
private bool _hasAvailableSubtitle;

/// <summary>
/// 元数据初始化完成事件.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ private void DownloadDanmaku()
LaunchDownloadProcess(command);
}

[RelayCommand]
private void DownloadSubtitle()
{
var command = $"{GetBasicCommand(false)} --sub-only \"{_currentUrl}\"";
LaunchDownloadProcess(command);
}

[RelayCommand]
private void BatchDownloadAllParts()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public sealed partial class SubtitleViewModel : ViewModelBase
private string _cid;
private int _position;
private IReadOnlyCollection<SubtitleInformation> _subtitles;
private Action? _initializedAction;

[ObservableProperty]
private bool _isAvailable;
Expand Down Expand Up @@ -60,6 +61,12 @@ public void ResetData(string aid, string cid)
IsEnabled = SettingsToolkit.ReadLocalSetting(Models.Constants.SettingNames.IsSubtitleEnabled, true);
}

/// <summary>
/// 设置初始化回调.
/// </summary>
public void SetInitializedCallback(Action action)
=> _initializedAction = action;

/// <summary>
/// 更新当前播放位置.
/// </summary>
Expand Down Expand Up @@ -113,6 +120,8 @@ private async Task InitializeAsync()
{
ChangeSubtitleCommand.Execute(firstMeta);
}

_initializedAction?.Invoke();
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,7 @@ private void ReloadPart()

InitializeDashMediaCommand.Execute(_part);
}

private void SyncDownloadAndSubtitle()
=> Downloader.HasAvailableSubtitle = Subtitle.IsAvailable && Subtitle.Metas.Any(p => !p.IsAI);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public VideoPlayerPageViewModel(
Subtitle = subtitle;
Downloader = download;
AI = ai;
Subtitle.SetInitializedCallback(SyncDownloadAndSubtitle);
Player.SetProgressAction(PlayerProgressChanged);
Player.SetStateAction(PlayerStateChanged);
Player.SetEndAction(PlayerMediaEnded);
Expand Down
Loading