From 03264b0b310cde984420c42e8d3bb64b936b6b1a Mon Sep 17 00:00:00 2001 From: Scrub <72096833+ScrubN@users.noreply.github.com> Date: Sun, 21 Jul 2024 02:17:42 -0400 Subject: [PATCH] Add `Audio Only` option to mass downloader preferred quality (#1159) * Add `Audio Only` option to mass downloader preferred quality * Adjust file extension * Make code not awful --- TwitchDownloaderCLI/Modes/DownloadVideo.cs | 7 +----- TwitchDownloaderCore/Tools/FilenameService.cs | 23 +++++++++++++++++++ TwitchDownloaderWPF/PageVodDownload.xaml.cs | 4 ++-- .../WindowQueueOptions.xaml.cs | 7 +++++- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/TwitchDownloaderCLI/Modes/DownloadVideo.cs b/TwitchDownloaderCLI/Modes/DownloadVideo.cs index fe0043df..df291fad 100644 --- a/TwitchDownloaderCLI/Modes/DownloadVideo.cs +++ b/TwitchDownloaderCLI/Modes/DownloadVideo.cs @@ -43,12 +43,7 @@ private static VideoDownloadOptions GetDownloadOptions(VideoDownloadArgs inputOp if (!Path.HasExtension(inputOptions.OutputFile) && inputOptions.Quality is { Length: > 0 }) { - if (inputOptions.Quality.Contains("audio", StringComparison.OrdinalIgnoreCase)) - inputOptions.OutputFile += ".m4a"; - else if (char.IsDigit(inputOptions.Quality[0]) - || inputOptions.Quality.Contains("source", StringComparison.OrdinalIgnoreCase) - || inputOptions.Quality.Contains("chunked", StringComparison.OrdinalIgnoreCase)) - inputOptions.OutputFile += ".mp4"; + inputOptions.OutputFile += FilenameService.GuessVodFileExtension(inputOptions.Quality); } VideoDownloadOptions downloadOptions = new() diff --git a/TwitchDownloaderCore/Tools/FilenameService.cs b/TwitchDownloaderCore/Tools/FilenameService.cs index 67796551..e59cb99e 100644 --- a/TwitchDownloaderCore/Tools/FilenameService.cs +++ b/TwitchDownloaderCore/Tools/FilenameService.cs @@ -124,6 +124,29 @@ public static string ReplaceInvalidFilenameChars([AllowNull] string filename) return newName.ReplaceAny(FilenameInvalidChars, '_'); } + [return: MaybeNull] + public static string GuessVodFileExtension([AllowNull] string qualityString) + { + if (string.IsNullOrWhiteSpace(qualityString)) + { + return ".mp4"; + } + + if (qualityString.Contains("audio", StringComparison.OrdinalIgnoreCase)) + { + return ".m4a"; + } + + if (char.IsDigit(qualityString[0]) + || qualityString.Contains("source", StringComparison.OrdinalIgnoreCase) + || qualityString.Contains("chunked", StringComparison.OrdinalIgnoreCase)) + { + return ".mp4"; + } + + return null; + } + public static FileInfo GetNonCollidingName(FileInfo fileInfo) { fileInfo.Refresh(); diff --git a/TwitchDownloaderWPF/PageVodDownload.xaml.cs b/TwitchDownloaderWPF/PageVodDownload.xaml.cs index 1202a5e2..a42d03b6 100644 --- a/TwitchDownloaderWPF/PageVodDownload.xaml.cs +++ b/TwitchDownloaderWPF/PageVodDownload.xaml.cs @@ -206,7 +206,7 @@ public VideoDownloadOptions GetOptions(string filename, string folder) Filename = filename ?? Path.Combine(folder, FilenameService.GetFilename(Settings.Default.TemplateVod, textTitle.Text, currentVideoId.ToString(), currentVideoTime, textStreamer.Text, checkStart.IsChecked == true ? new TimeSpan((int)numStartHour.Value, (int)numStartMinute.Value, (int)numStartSecond.Value) : TimeSpan.Zero, checkEnd.IsChecked == true ? new TimeSpan((int)numEndHour.Value, (int)numEndMinute.Value, (int)numEndSecond.Value) : vodLength, - viewCount, game) + (comboQuality.Text.Contains("Audio", StringComparison.OrdinalIgnoreCase) ? ".m4a" : ".mp4")), + viewCount, game) + FilenameService.GuessVodFileExtension(comboQuality.Text)), Oauth = TextOauth.Text, Quality = GetQualityWithoutSize(comboQuality.Text), Id = currentVideoId, @@ -425,7 +425,7 @@ private async void SplitBtnDownloader_Click(object sender, RoutedEventArgs e) FileName = FilenameService.GetFilename(Settings.Default.TemplateVod, textTitle.Text, currentVideoId.ToString(), currentVideoTime, textStreamer.Text, checkStart.IsChecked == true ? new TimeSpan((int)numStartHour.Value, (int)numStartMinute.Value, (int)numStartSecond.Value) : TimeSpan.Zero, checkEnd.IsChecked == true ? new TimeSpan((int)numEndHour.Value, (int)numEndMinute.Value, (int)numEndSecond.Value) : vodLength, - viewCount, game) + (comboQuality.Text.Contains("Audio", StringComparison.OrdinalIgnoreCase) ? ".m4a" : ".mp4") + viewCount, game) + FilenameService.GuessVodFileExtension(comboQuality.Text) }; if (saveFileDialog.ShowDialog() == false) { diff --git a/TwitchDownloaderWPF/WindowQueueOptions.xaml.cs b/TwitchDownloaderWPF/WindowQueueOptions.xaml.cs index e76849bb..a65fdb5a 100644 --- a/TwitchDownloaderWPF/WindowQueueOptions.xaml.cs +++ b/TwitchDownloaderWPF/WindowQueueOptions.xaml.cs @@ -99,6 +99,11 @@ public WindowQueueOptions(List dataList) if (Directory.Exists(queueFolder)) textFolder.Text = queueFolder; + if (_dataList.Any(x => x.Id.All(char.IsDigit))) + { + ComboPreferredQuality.Items.Add(new ComboBoxItem { Content = "Audio Only" }); + } + var preferredQuality = Settings.Default.PreferredQuality; for (var i = 0; i < ComboPreferredQuality.Items.Count; i++) { @@ -505,7 +510,7 @@ private void EnqueueDataList() downloadOptions.Filename = Path.Combine(folderPath, FilenameService.GetFilename(Settings.Default.TemplateVod, taskData.Title, taskData.Id, taskData.Time, taskData.Streamer, downloadOptions.TrimBeginning ? downloadOptions.TrimBeginningTime : TimeSpan.Zero, downloadOptions.TrimEnding ? downloadOptions.TrimEndingTime : TimeSpan.FromSeconds(taskData.Length), - taskData.Views, taskData.Game) + ".mp4"); + taskData.Views, taskData.Game) + FilenameService.GuessVodFileExtension(downloadOptions.Quality)); VodDownloadTask downloadTask = new VodDownloadTask {