From c8be76020ebd395b8c9906884951da0eae6e732e Mon Sep 17 00:00:00 2001 From: Scrub <72096833+ScrubN@users.noreply.github.com> Date: Mon, 29 Jul 2024 15:19:50 -0400 Subject: [PATCH] Allow trimming clip chats (#1167) --- TwitchDownloaderCore/ChatDownloader.cs | 8 +-- TwitchDownloaderWPF/PageChatDownload.xaml.cs | 63 +++++++++++--------- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/TwitchDownloaderCore/ChatDownloader.cs b/TwitchDownloaderCore/ChatDownloader.cs index d89b18aa..70c5b047 100644 --- a/TwitchDownloaderCore/ChatDownloader.cs +++ b/TwitchDownloaderCore/ChatDownloader.cs @@ -370,16 +370,12 @@ private async Task DownloadAsyncImpl(FileInfo outputFileInfo, FileStream outputF } videoId = clipInfoResponse.data.clip.video.id; - downloadOptions.TrimBeginning = true; - downloadOptions.TrimBeginningTime = (int)clipInfoResponse.data.clip.videoOffsetSeconds; - downloadOptions.TrimEnding = true; - downloadOptions.TrimEndingTime = downloadOptions.TrimBeginningTime + clipInfoResponse.data.clip.durationSeconds; chatRoot.streamer.name = clipInfoResponse.data.clip.broadcaster.displayName; chatRoot.streamer.id = int.Parse(clipInfoResponse.data.clip.broadcaster.id); chatRoot.video.title = clipInfoResponse.data.clip.title; chatRoot.video.created_at = clipInfoResponse.data.clip.createdAt; - chatRoot.video.start = (int)clipInfoResponse.data.clip.videoOffsetSeconds; - chatRoot.video.end = (int)clipInfoResponse.data.clip.videoOffsetSeconds + clipInfoResponse.data.clip.durationSeconds; + chatRoot.video.start = (double)clipInfoResponse.data.clip.videoOffsetSeconds + (downloadOptions.TrimBeginning ? downloadOptions.TrimBeginningTime : 0); + chatRoot.video.end = (double)clipInfoResponse.data.clip.videoOffsetSeconds + (downloadOptions.TrimEnding ? downloadOptions.TrimEndingTime : clipInfoResponse.data.clip.durationSeconds); chatRoot.video.length = clipInfoResponse.data.clip.durationSeconds; chatRoot.video.viewCount = clipInfoResponse.data.clip.viewCount; chatRoot.video.game = clipInfoResponse.data.clip.game?.displayName ?? "Unknown"; diff --git a/TwitchDownloaderWPF/PageChatDownload.xaml.cs b/TwitchDownloaderWPF/PageChatDownload.xaml.cs index 5820d9bf..0cf6e540 100644 --- a/TwitchDownloaderWPF/PageChatDownload.xaml.cs +++ b/TwitchDownloaderWPF/PageChatDownload.xaml.cs @@ -43,7 +43,7 @@ public PageChatDownload() private void Page_Initialized(object sender, EventArgs e) { - SetEnabled(false, false); + SetEnabled(false); SetEnabledTrimStart(false); SetEnabledTrimEnd(false); checkEmbed.IsChecked = Settings.Default.ChatEmbedEmotes; @@ -73,10 +73,10 @@ private void Page_Initialized(object sender, EventArgs e) }; } - private void SetEnabled(bool isEnabled, bool isClip) + private void SetEnabled(bool isEnabled) { - CheckTrimStart.IsEnabled = isEnabled & !isClip; - CheckTrimEnd.IsEnabled = isEnabled & !isClip; + CheckTrimStart.IsEnabled = isEnabled; + CheckTrimEnd.IsEnabled = isEnabled; radioTimestampRelative.IsEnabled = isEnabled; radioTimestampUTC.IsEnabled = isEnabled; radioTimestampNone.IsEnabled = isEnabled; @@ -147,6 +147,9 @@ private async Task GetVideoInfo() streamerId = int.Parse(videoInfo.data.video.owner.id); viewCount = videoInfo.data.video.viewCount; game = videoInfo.data.video.game?.displayName ?? Translations.Strings.UnknownGame; + + numStartHour.Maximum = (int)vodLength.TotalHours; + numStartMinute.Maximum = 60; var urlTimeCodeMatch = TwitchRegex.UrlTimeCode.Match(textUrl.Text); if (urlTimeCodeMatch.Success) { @@ -162,14 +165,14 @@ private async Task GetVideoInfo() numStartMinute.Value = 0; numStartSecond.Value = 0; } - numStartHour.Maximum = (int)vodLength.TotalHours; - numEndHour.Value = (int)vodLength.TotalHours; numEndHour.Maximum = (int)vodLength.TotalHours; + numEndHour.Value = (int)vodLength.TotalHours; + numEndMinute.Maximum = 60; numEndMinute.Value = vodLength.Minutes; numEndSecond.Value = vodLength.Seconds; labelLength.Text = vodLength.ToString("c"); - SetEnabled(true, false); + SetEnabled(true); } else if (downloadType == DownloadType.Clip) { @@ -184,17 +187,27 @@ private async Task GetVideoInfo() } imgThumbnail.Source = image; - TimeSpan clipLength = TimeSpan.FromSeconds(clipInfo.data.clip.durationSeconds); + vodLength = TimeSpan.FromSeconds(clipInfo.data.clip.durationSeconds); textStreamer.Text = clipInfo.data.clip.broadcaster?.displayName ?? Translations.Strings.UnknownUser; var clipCreatedAt = clipInfo.data.clip.createdAt; textCreatedAt.Text = Settings.Default.UTCVideoTime ? clipCreatedAt.ToString(CultureInfo.CurrentCulture) : clipCreatedAt.ToLocalTime().ToString(CultureInfo.CurrentCulture); currentVideoTime = Settings.Default.UTCVideoTime ? clipCreatedAt : clipCreatedAt.ToLocalTime(); textTitle.Text = clipInfo.data.clip.title; streamerId = int.Parse(clipInfo.data.clip.broadcaster?.id ?? "-1"); - labelLength.Text = clipLength.ToString("c"); - SetEnabled(true, true); - SetEnabledTrimStart(false); - SetEnabledTrimEnd(false); + labelLength.Text = vodLength.ToString("c"); + SetEnabled(true); + + numStartHour.Maximum = 0; + numStartHour.Value = 0; + numStartMinute.Maximum = vodLength.Minutes; + numStartMinute.Value = 0; + numStartSecond.Value = 0; + + numEndHour.Maximum = 0; + numEndHour.Value = 0; + numEndMinute.Maximum = vodLength.Minutes; + numEndMinute.Value = vodLength.Minutes; + numEndSecond.Value = vodLength.Seconds; } btnGetInfo.IsEnabled = true; @@ -268,22 +281,18 @@ public ChatDownloadOptions GetOptions(string filename) else if (radioCompressionGzip.IsChecked == true) options.Compression = ChatCompression.Gzip; - // TODO: Enable trimming clip chats - if (downloadType is DownloadType.Video) + if (CheckTrimStart.IsChecked == true) { - if (CheckTrimStart.IsChecked == true) - { - options.TrimBeginning = true; - TimeSpan start = new TimeSpan((int)numStartHour.Value, (int)numStartMinute.Value, (int)numStartSecond.Value); - options.TrimBeginningTime = (int)start.TotalSeconds; - } + options.TrimBeginning = true; + TimeSpan start = new TimeSpan((int)numStartHour.Value, (int)numStartMinute.Value, (int)numStartSecond.Value); + options.TrimBeginningTime = (int)start.TotalSeconds; + } - if (CheckTrimEnd.IsChecked == true) - { - options.TrimEnding = true; - TimeSpan end = new TimeSpan((int)numEndHour.Value, (int)numEndMinute.Value, (int)numEndSecond.Value); - options.TrimEndingTime = (int)end.TotalSeconds; - } + if (CheckTrimEnd.IsChecked == true) + { + options.TrimEnding = true; + TimeSpan end = new TimeSpan((int)numEndHour.Value, (int)numEndMinute.Value, (int)numEndSecond.Value); + options.TrimEndingTime = (int)end.TotalSeconds; } if (radioTimestampUTC.IsChecked == true) @@ -532,7 +541,7 @@ private async void SplitBtnDownload_Click(object sender, RoutedEventArgs e) var currentDownload = new ChatDownloader(downloadOptions, downloadProgress); btnGetInfo.IsEnabled = false; - SetEnabled(false, false); + SetEnabled(false); SetImage("Images/ppOverheat.gif", true); statusMessage.Text = Translations.Strings.StatusDownloading;