Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lay295 committed Jun 22, 2020
1 parent 68b8cb5 commit 15b341d
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 35 deletions.
7 changes: 7 additions & 0 deletions TwitchDownloaderWPF/ChatRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,15 @@ public class Comment
public bool more_replies { get; set; }
}

public class VideoTime
{
public double start { get; set; }
public double end { get; set; }
}

public class ChatRoot
{
public Streamer streamer { get; set; }
public List<Comment> comments { get; set; }
public VideoTime video { get; set; }
}
4 changes: 2 additions & 2 deletions TwitchDownloaderWPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<!-- <ColumnDefinition Width="*"/> -->
<ColumnDefinition Width="*"/>
<!-- <ColumnDefinition Width="*"/> -->
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<Button x:Name="btnVodDownload" Margin="5" Grid.Row="1" Grid.Column="1" Content="VOD Downloader" Click="btnVodDownload_Click" />
<Button x:Name="btnClipDownload" Margin="5" Grid.Row="1" Grid.Column="2" Content="Clip Downloader" Click="btnClipDownload_Click" />
<Button x:Name="btnChatDownload" Margin="5" Grid.Row="1" Grid.Column="3" Content="Chat Downloader" Click="btnChatDownload_Click" />
<Button x:Name="btnChatRender" Margin="5" Grid.Row="1" Grid.Column="4" Content="Chat Render" Click="btnChatRender_Click" />
<!-- <Button x:Name="btnQueue" Margin="5" Grid.Row="1" Grid.Column="5" Content="Task Queue" Click="btnQueue_Click" /> -->
<!-- <Button x:Name="btnQueue" Margin="5" Grid.Row="1" Grid.Column="5" Content="Task Queue" Click="btnQueue_Click" /> -->
<Frame Focusable="False" x:Name="Main" Grid.Column="0" Grid.ColumnSpan="6" Grid.Row="2" NavigationUIVisibility="Hidden"/>

</Grid>
Expand Down
5 changes: 5 additions & 0 deletions TwitchDownloaderWPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -29,10 +30,14 @@ public partial class MainWindow : Window
PageChatDownload pageChatDownload = new PageChatDownload();
PageChatRender pageChatRender = new PageChatRender();
PageQueue pageQueue = new PageQueue();
public ObservableCollection<ITwitchTask> taskList = new ObservableCollection<ITwitchTask>();

public MainWindow()
{
InitializeComponent();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

pageQueue.queueList.ItemsSource = taskList;
}

private void btnVodDownload_Click(object sender, RoutedEventArgs e)
Expand Down
2 changes: 2 additions & 0 deletions TwitchDownloaderWPF/PageChatDownload.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<StackPanel HorizontalAlignment="Left">
<TextBlock Text="Download Format:" HorizontalAlignment="Right" Margin="0,10,0,0"/>
<TextBlock Text="Crop Chat:" HorizontalAlignment="Right" Margin="0,10,0,0"/>
<!-- <TextBlock HorizontalAlignment="Right" Margin="0,29,0,0">Embed Emotes <Hyperlink ToolTipService.ShowDuration="30000"><Hyperlink.ToolTip>Embeds emotes into the JSON file so in the future when an emote is removed from Twitch or a 3rd party, it will still render correctly. Useful for archival purposes, file size will be larger.</Hyperlink.ToolTip>(?)</Hyperlink>:</TextBlock> -->
</StackPanel>
<StackPanel>
<StackPanel Margin="5,12,0,0" Orientation="Horizontal">
Expand All @@ -78,6 +79,7 @@
<xctk:IntegerUpDown Margin="3,-1,0,0" Value="0" Name="numEndSecond" />
</StackPanel>
</StackPanel>
<!-- <CheckBox x:Name="checkEmbed" Margin="5,8,0,0"></CheckBox> -->
</StackPanel>
</StackPanel>
<Button x:Name="btnDownload" Grid.Row="3" Grid.Column="2" Grid.ColumnSpan="2" Content="Download" Height="50" Width="100" Margin="0,0,0,20" VerticalAlignment="Bottom" Click="btnDownload_Click"/>
Expand Down
6 changes: 5 additions & 1 deletion TwitchDownloaderWPF/PageChatDownload.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,14 @@ private void BackgroundDownloadManager_DoWork(object sender, DoWorkEventArgs e)
double videoStart = clipInfo.offset;
double videoDuration = clipInfo.duration;
JObject result = new JObject();
JObject video = new JObject();
JArray comments = new JArray();
JObject streamer = new JObject();

streamer["name"] = clipInfo.streamer_name;
streamer["id"] = clipInfo.streamer_id;
video["start"] = videoStart;
video["end"] = videoStart + videoDuration;

while (latestMessage < (videoStart + videoDuration))
{
Expand All @@ -201,7 +204,7 @@ private void BackgroundDownloadManager_DoWork(object sender, DoWorkEventArgs e)

foreach (var comment in res["comments"])
{
if (latestMessage < (videoStart + videoDuration))
if (latestMessage < (videoStart + videoDuration) && comment["content_offset_seconds"].ToObject<double>() > videoStart)
comments.Add(comment);

latestMessage = comment["content_offset_seconds"].ToObject<double>();
Expand All @@ -221,6 +224,7 @@ private void BackgroundDownloadManager_DoWork(object sender, DoWorkEventArgs e)

result["streamer"] = streamer;
result["comments"] = comments;
result["video"] = video;

using (StreamWriter sw = new StreamWriter(clipInfo.path))
{
Expand Down
31 changes: 27 additions & 4 deletions TwitchDownloaderWPF/PageChatRender.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ private void BackgroundRenderManager_DoWork(object sender, DoWorkEventArgs e)
try
{
chatJson = JsonConvert.DeserializeObject<ChatRoot>(File.ReadAllText(renderOptions.json_path));
if (chatJson.streamer == null)
{
JObject json = JObject.Parse(File.ReadAllText(renderOptions.json_path));
chatJson.streamer = new Streamer();
chatJson.streamer.name = json["video"]["user_name"].ToString();
chatJson.streamer.id = json["video"]["user_id"].ToObject<int>();
}
chatJson.streamer.name = GetStreamerName(chatJson.streamer.id);
}
catch (JsonSerializationException)
Expand Down Expand Up @@ -203,7 +210,7 @@ private void BackgroundRenderManager_DoWork(object sender, DoWorkEventArgs e)
}

(sender as BackgroundWorker).ReportProgress(0, new Progress("Rendering Video 0%"));
RenderVideo(renderOptions, new Queue<TwitchComment>(finalComments.ToArray()), chatJson.comments, sender);
RenderVideo(renderOptions, new Queue<TwitchComment>(finalComments.ToArray()), chatJson, sender);

(sender as BackgroundWorker).ReportProgress(0, new Progress("Cleaning up..."));
try
Expand Down Expand Up @@ -268,14 +275,24 @@ private SKColor GenerateUserColor(SKColor userColor, SKColor background_color)
return userColor;
}

private void RenderVideo(RenderOptions renderOptions, Queue<TwitchComment> finalComments, List<Comment> comments, object sender)
private void RenderVideo(RenderOptions renderOptions, Queue<TwitchComment> finalComments, ChatRoot chatJson, object sender)
{
SKBitmap bufferBitmap = new SKBitmap(renderOptions.chat_width, renderOptions.chat_height);
SKCanvas bufferCanvas = new SKCanvas(bufferBitmap);
SKPaint gifBackgroundPaint = new SKPaint();
SKPaint gifPaint = new SKPaint();
int videoStart = (int)Math.Floor(comments.First().content_offset_seconds);
int duration = (int)Math.Ceiling(comments.Last().content_offset_seconds) - videoStart;
int videoStart;
int duration;
if (chatJson.video != null)
{
videoStart = (int)Math.Floor(chatJson.video.start);
duration = (int)Math.Ceiling(chatJson.video.end) - videoStart;
}
else
{
videoStart = (int)Math.Floor(chatJson.comments.First().content_offset_seconds);
duration = (int)Math.Ceiling(chatJson.comments.Last().content_offset_seconds) - videoStart;
}
List<GifEmote> displayedGifs = new List<GifEmote>();
Stopwatch stopwatch = new Stopwatch();

Expand Down Expand Up @@ -1368,6 +1385,12 @@ private bool ValidateInputs()
}
}

if (Int32.Parse(textHeight.Text) % 2 != 0 && Int32.Parse(textWidth.Text) % 2 != 0)
{
AppendLog("ERROR: Height and Width must be even");
return false;
}

return true;
}

Expand Down
16 changes: 16 additions & 0 deletions TwitchDownloaderWPF/PageQueue.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,21 @@
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>

<ItemsControl x:Name="queueList" Grid.Column="1" Grid.ColumnSpan="3">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="{Binding Preview}"></Image>
<TextBlock Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding Title}"></TextBlock>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Page>
2 changes: 1 addition & 1 deletion TwitchDownloaderWPF/PageVodDownload.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
</StackPanel>
</StackPanel>
<Button x:Name="btnDownload" Content="Download" Height="50" Width="100" Click="btnDownload_Click" Grid.Row="3" Grid.Column="2" Grid.ColumnSpan="2" HorizontalAlignment="Center" Margin="0,0,0,10" VerticalAlignment="Bottom"/>
<!-- <Button x:Name="btnQueue" Content="Queue" Height="25" Width="60" Margin="5,4,0,10" Grid.Row="3" Grid.Column="3" HorizontalAlignment="Center"/> -->
<Button x:Name="btnQueue" Content="Queue" Height="25" Width="60" Click="btnQueue_Click" Margin="5,4,0,10" Grid.Row="3" Grid.Column="3" HorizontalAlignment="Center" Visibility="Hidden"/>
<!-- RIGHT -->
<StackPanel Grid.Column="4" Grid.Row="2" Grid.RowSpan="2">
<TextBlock Text="Log:"/>
Expand Down
34 changes: 33 additions & 1 deletion TwitchDownloaderWPF/PageVodDownload.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using TwitchDownloader.Properties;
using TwitchDownloader.Tasks;
using Xabe.FFmpeg.Events;
using System.Collections.ObjectModel;

namespace TwitchDownloaderWPF
{
Expand Down Expand Up @@ -59,7 +60,7 @@ private void SetEnabled(bool isEnabled)
numEndMinute.IsEnabled = isEnabled;
numEndSecond.IsEnabled = isEnabled;
btnDownload.IsEnabled = isEnabled;
//btnQueue.IsEnabled = isEnabled;
btnQueue.IsEnabled = isEnabled;
}
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Expand Down Expand Up @@ -172,6 +173,7 @@ private async void btnDownload_Click(object sender, RoutedEventArgs e)
options.filename = saveFileDialog.FileName;

TaskVodDownload currentDownload = new TaskVodDownload(options);
currentDownload.Preview = imgThumbnail.Source;
Progress<ProgressReport> uploadProgress = new Progress<ProgressReport>(OnProgressChanged);

SetImage("Images/ppOverheat.gif", true);
Expand Down Expand Up @@ -341,6 +343,36 @@ private void checkCFR_Changed(object sender, RoutedEventArgs e)
Settings.Default.EncodeCFR = (bool)checkCFR.IsChecked;
Settings.Default.Save();
}

private void btnQueue_Click(object sender, RoutedEventArgs e)
{
bool isValid = ValidateInput();

if (isValid)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();

saveFileDialog.Filter = "MP4 Files | *.mp4";
saveFileDialog.RestoreDirectory = true;

if (saveFileDialog.ShowDialog() == true)
{
SetEnabled(false);
DownloadOptions options = new DownloadOptions();
options.UpdateValues(this);
options.filename = saveFileDialog.FileName;

TaskVodDownload currentDownload = new TaskVodDownload(options);
currentDownload.Preview = imgThumbnail.Source;
ObservableCollection<ITwitchTask> taskList = ((MainWindow)Window.GetWindow(this)).taskList;
taskList.Add(currentDownload);
}
}
else
{
AppendLog("ERROR: Invalid Crop Inputs");
}
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions TwitchDownloaderWPF/RenderObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ public GifEmote(Point Offset, string Name, SKCodec Codec, int ImageScale, List<S
total_duration = durations.Count * 10;
}

for (int i = 0; i < durations.Count; i++)
{
if (durations[i] == 0)
{
total_duration += 10;
durations[i] = 10;
}
}

width = image_frames.First().Width;
height = image_frames.First().Height;

Expand Down
8 changes: 5 additions & 3 deletions TwitchDownloaderWPF/Tasks/ITwitchTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media;
using TwitchDownloader.Tasks;

namespace TwitchDownloader
{
interface ITwitchTask
public interface ITwitchTask
{
string Title { get; set; }
string Information { get; set; }
ImageSource Preview { get; set; }
CancellationTokenSource CancellationTokenSource { get; set; }
string getTitle();
string getInformation();
Task runTask(IProgress<ProgressReport> progress);
void cancelTask();
}
Expand Down
Loading

0 comments on commit 15b341d

Please sign in to comment.