Skip to content

Commit

Permalink
Allow trimming clip chats (#1167)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN authored Jul 29, 2024
1 parent e3dcb20 commit c8be760
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
8 changes: 2 additions & 6 deletions TwitchDownloaderCore/ChatDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
63 changes: 36 additions & 27 deletions TwitchDownloaderWPF/PageChatDownload.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c8be760

Please sign in to comment.