6
6
using System . Threading ;
7
7
using System . Threading . Tasks ;
8
8
using System . Web ;
9
+ using TwitchDownloaderCore . Extensions ;
9
10
using TwitchDownloaderCore . Options ;
10
11
using TwitchDownloaderCore . Tools ;
11
12
@@ -40,12 +41,18 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
40
41
TwitchHelper . CreateDirectory ( clipDirectory . FullName ) ;
41
42
}
42
43
43
- _progress . Report ( new ProgressReport ( ReportType . NewLineStatus , "Downloading Clip" ) ) ;
44
+ _progress . Report ( new ProgressReport ( ReportType . NewLineStatus , "Downloading Clip 0%" ) ) ;
45
+
46
+ void DownloadProgressHandler ( StreamCopyProgress streamProgress )
47
+ {
48
+ var percent = ( int ) ( streamProgress . BytesCopied / ( double ) streamProgress . SourceLength * 100 ) ;
49
+ _progress . Report ( new ProgressReport ( ReportType . SameLineStatus , $ "Downloading Clip { percent } %") ) ;
50
+ _progress . Report ( new ProgressReport ( percent ) ) ;
51
+ }
44
52
45
53
if ( ! downloadOptions . EncodeMetadata )
46
54
{
47
- await DownloadFileTaskAsync ( downloadUrl , downloadOptions . Filename , downloadOptions . ThrottleKib , cancellationToken ) ;
48
- _progress . Report ( new ProgressReport ( 100 ) ) ;
55
+ await DownloadFileTaskAsync ( downloadUrl , downloadOptions . Filename , downloadOptions . ThrottleKib , new Progress < StreamCopyProgress > ( DownloadProgressHandler ) , cancellationToken ) ;
49
56
return ;
50
57
}
51
58
@@ -57,12 +64,14 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
57
64
var tempFile = Path . Combine ( downloadOptions . TempFolder , $ "clip_{ DateTimeOffset . Now . ToUnixTimeMilliseconds ( ) } _{ Path . GetRandomFileName ( ) } ") ;
58
65
try
59
66
{
60
- await DownloadFileTaskAsync ( downloadUrl , tempFile , downloadOptions . ThrottleKib , cancellationToken ) ;
67
+ await DownloadFileTaskAsync ( downloadUrl , tempFile , downloadOptions . ThrottleKib , new Progress < StreamCopyProgress > ( DownloadProgressHandler ) , cancellationToken ) ;
61
68
62
- _progress . Report ( new ProgressReport ( ReportType . NewLineStatus , "Encoding Clip Metadata" ) ) ;
69
+ _progress . Report ( new ProgressReport ( ReportType . NewLineStatus , "Encoding Clip Metadata 0%" ) ) ;
70
+ _progress . Report ( new ProgressReport ( 0 ) ) ;
63
71
64
72
await EncodeClipMetadata ( tempFile , downloadOptions . Filename , cancellationToken ) ;
65
73
74
+ _progress . Report ( new ProgressReport ( ReportType . SameLineStatus , "Encoding Clip Metadata 100%" ) ) ;
66
75
_progress . Report ( new ProgressReport ( 100 ) ) ;
67
76
}
68
77
finally
@@ -103,22 +112,26 @@ private async Task<string> GetDownloadUrl()
103
112
return downloadUrl + "?sig=" + listLinks [ 0 ] . data . clip . playbackAccessToken . signature + "&token=" + HttpUtility . UrlEncode ( listLinks [ 0 ] . data . clip . playbackAccessToken . value ) ;
104
113
}
105
114
106
- private static async Task DownloadFileTaskAsync ( string url , string destinationFile , int throttleKib , CancellationToken cancellationToken )
115
+ private static async Task DownloadFileTaskAsync ( string url , string destinationFile , int throttleKib , IProgress < StreamCopyProgress > progress , CancellationToken cancellationToken )
107
116
{
108
117
var request = new HttpRequestMessage ( HttpMethod . Get , url ) ;
109
118
using var response = await HttpClient . SendAsync ( request , HttpCompletionOption . ResponseHeadersRead , cancellationToken ) . ConfigureAwait ( false ) ;
110
119
response . EnsureSuccessStatusCode ( ) ;
111
120
121
+ var contentLength = response . Content . Headers . ContentLength ;
122
+
112
123
if ( throttleKib == - 1 )
113
124
{
114
125
await using var fs = new FileStream ( destinationFile , FileMode . Create , FileAccess . Write , FileShare . Read ) ;
115
- await response . Content . CopyToAsync ( fs , cancellationToken ) . ConfigureAwait ( false ) ;
126
+ await using var contentStream = await response . Content . ReadAsStreamAsync ( cancellationToken ) ;
127
+ await contentStream . ProgressCopyToAsync ( fs , contentLength , progress , cancellationToken ) . ConfigureAwait ( false ) ;
116
128
}
117
129
else
118
130
{
119
- await using var throttledStream = new ThrottledStream ( await response . Content . ReadAsStreamAsync ( cancellationToken ) , throttleKib ) ;
120
131
await using var fs = new FileStream ( destinationFile , FileMode . Create , FileAccess . Write , FileShare . Read ) ;
121
- await throttledStream . CopyToAsync ( fs , cancellationToken ) . ConfigureAwait ( false ) ;
132
+ await using var contentStream = await response . Content . ReadAsStreamAsync ( cancellationToken ) ;
133
+ await using var throttledStream = new ThrottledStream ( contentStream , throttleKib ) ;
134
+ await throttledStream . ProgressCopyToAsync ( fs , contentLength , progress , cancellationToken ) . ConfigureAwait ( false ) ;
122
135
}
123
136
}
124
137
@@ -137,7 +150,7 @@ await FfmpegMetadata.SerializeAsync(metadataFile, clipInfo.data.clip.broadcaster
137
150
StartInfo =
138
151
{
139
152
FileName = downloadOptions . FfmpegPath ,
140
- Arguments = $ "-i \" { inputFile } \" -i \" { metadataFile } \" -map_metadata 1 -c copy \" { destinationFile } \" ",
153
+ Arguments = $ "-i \" { inputFile } \" -i \" { metadataFile } \" -map_metadata 1 -y - c copy \" { destinationFile } \" ",
141
154
UseShellExecute = false ,
142
155
CreateNoWindow = true ,
143
156
RedirectStandardInput = false ,
0 commit comments