Skip to content

Commit

Permalink
add support for vtt sub and refactor subtitle format list usage
Browse files Browse the repository at this point in the history
  • Loading branch information
huynhsontung committed Dec 16, 2023
1 parent 676fd4a commit fb411b9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Screenbox.Core/Helpers/FilesHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ public static class FilesHelpers
public static ImmutableArray<string> SupportedFormats { get; } =
SupportedVideoFormats.AddRange(SupportedAudioFormats).AddRange(SupportedPlaylistFormats);

public static ImmutableArray<string> SupportedSubtitleFormats { get; } = ImmutableArray.Create(".srt", ".vtt", ".ass");

public static bool IsSupportedAudio(this IStorageFile file) => SupportedAudioFormats.Contains(file.FileType.ToLowerInvariant());
public static bool IsSupportedVideo(this IStorageFile file) => SupportedVideoFormats.Contains(file.FileType.ToLowerInvariant());
public static bool IsSupportedPlaylist(this IStorageFile file) => SupportedPlaylistFormats.Contains(file.FileType.ToLowerInvariant());
public static bool IsSupported(this IStorageFile file) => SupportedFormats.Contains(file.FileType.ToLowerInvariant());
public static bool IsSupportedSubtitle(this IStorageFile file) => SupportedSubtitleFormats.Contains(file.FileType.ToLowerInvariant());
}
6 changes: 4 additions & 2 deletions Screenbox.Core/ViewModels/AudioTrackSubtitleViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Screenbox.Core.Enums;
using Screenbox.Core.Helpers;
using Screenbox.Core.Messages;
using Screenbox.Core.Playback;
using Screenbox.Core.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Search;
Expand Down Expand Up @@ -60,7 +62,7 @@ public async void Receive(PlaylistCurrentItemChangedMessage message)
{
if (_mediaPlayer == null) return;
if (message.Value?.Source is not StorageFile file) return;
QueryOptions options = new(CommonFileQuery.DefaultQuery, new[] { ".srt", ".ass" })
QueryOptions options = new(CommonFileQuery.DefaultQuery, FilesHelpers.SupportedSubtitleFormats)
{
ApplicationSearchFilter = $"System.FileName:$<\"{Path.GetFileNameWithoutExtension(file.Name)}\""
};
Expand Down Expand Up @@ -92,7 +94,7 @@ private async Task AddSubtitle()
if (_mediaPlayer?.PlaybackItem == null) return;
try
{
StorageFile? file = await _filesService.PickFileAsync(".srt", ".ass");
StorageFile? file = await _filesService.PickFileAsync(FilesHelpers.SupportedSubtitleFormats.Add("*").ToArray());
if (file == null) return;

_mediaPlayer.AddSubtitle(file);
Expand Down
2 changes: 1 addition & 1 deletion Screenbox.Core/ViewModels/PlayerPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public async void OnDrop(object sender, DragEventArgs e)
IReadOnlyList<IStorageItem>? items = await e.DataView.GetStorageItemsAsync();
if (items.Count > 0)
{
if (items.Count == 1 && items[0] is StorageFile { FileType: ".srt" or ".ass" } file)
if (items.Count == 1 && items[0] is StorageFile file && file.IsSupportedSubtitle())
{
_mediaPlayer.AddSubtitle(file);
Messenger.Send(new SubtitleAddedNotificationMessage(file));
Expand Down

0 comments on commit fb411b9

Please sign in to comment.