Skip to content

Commit

Permalink
Add setting to disable subtitles
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry08 committed Jun 17, 2024
1 parent e6bba6a commit 19fe106
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
23 changes: 15 additions & 8 deletions Yosu.Youtube.Core/Downloading/VideoDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Gress;
Expand All @@ -12,9 +13,9 @@

namespace Yosu.Youtube.Core.Downloading;

public class VideoDownloader
public class VideoDownloader(IReadOnlyList<Cookie>? initialCookies = null)
{
private readonly YoutubeClient _youtube = new(Http.Client);
private readonly YoutubeClient _youtube = new(Http.Client, initialCookies ?? []);

public async Task<IReadOnlyList<VideoDownloadOption>> GetDownloadOptionsAsync(
VideoId videoId,
Expand All @@ -41,16 +42,22 @@ public async Task DownloadVideoAsync(
string filePath,
IVideo video,
VideoDownloadOption downloadOption,
bool includeSubtitles = true,
IProgress<Percentage>? progress = null,
CancellationToken cancellationToken = default
)
{
// If the target container supports subtitles, embed them in the video too
var trackInfos = !downloadOption.Container.IsAudioOnly
? (
await _youtube.Videos.ClosedCaptions.GetManifestAsync(video.Id, cancellationToken)
).Tracks
: Array.Empty<ClosedCaptionTrackInfo>();
// Include subtitles in the output container
var trackInfos = new List<ClosedCaptionTrackInfo>();
if (includeSubtitles && !downloadOption.Container.IsAudioOnly)
{
var manifest = await _youtube.Videos.ClosedCaptions.GetManifestAsync(
video.Id,
cancellationToken
);

trackInfos.AddRange(manifest.Tracks);
}

var dirPath = Path.GetDirectoryName(filePath);
if (!string.IsNullOrWhiteSpace(dirPath))
Expand Down
2 changes: 2 additions & 0 deletions Yosu/Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public partial class SettingsService : SettingsBase, INotifyPropertyChanged

public bool ShouldSkipExistingFiles { get; set; }

public bool ShouldInjectSubtitles { get; set; } = true;

public string SoundCloudFileNameTemplate { get; set; } = "$title";

public string SpotifyFileNameTemplate { get; set; } = "$title";
Expand Down
1 change: 1 addition & 0 deletions Yosu/ViewModels/YoutubeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ await _videoDownloader.DownloadVideoAsync(
download.TempFilePath!,
download.Video!,
downloadOption,
_settingsService.ShouldInjectSubtitles,
progress,
download.CancellationToken
);
Expand Down
19 changes: 19 additions & 0 deletions Yosu/Views/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,25 @@
Text="{Binding Settings.DownloadDir}" />
</Grid>

<!-- Inject subtitles -->
<Grid
Margin="0,5,0,10"
Padding="20,0"
ColumnDefinitions="*,auto"
RowDefinitions="*,*">
<Label
Margin="0,0,5,0"
FontAttributes="Bold"
FontSize="15"
Text="Inject subtitles:"
VerticalOptions="Center" />
<Label
Grid.Row="1"
Grid.Column="0"
Text="Inject subtitles into downloaded files" />
<Switch Grid.ColumnSpan="2" IsToggled="{Binding Settings.ShouldInjectSubtitles}" />
</Grid>

<!-- Inject tags -->
<Grid
Margin="0,5,0,10"
Expand Down

0 comments on commit 19fe106

Please sign in to comment.