@@ -221,6 +221,36 @@ private void RenderVideo(ChatRenderOptions renderOptions, Queue<TwitchComment> f
221
221
Stopwatch stopwatch = new Stopwatch ( ) ;
222
222
stopwatch . Start ( ) ;
223
223
224
+ Process maskProcess = null ;
225
+ BinaryWriter maskStream = null ;
226
+ if ( renderOptions . GenerateMask )
227
+ {
228
+ string outputArgsMask = renderOptions . OutputArgs . Replace ( "{fps}" , renderOptions . Framerate . ToString ( ) )
229
+ . Replace ( "{height}" , renderOptions . ChatHeight . ToString ( ) ) . Replace ( "{width}" , renderOptions . ChatWidth . ToString ( ) )
230
+ . Replace ( "{save_path}" , renderOptions . OutputFileMask ) . Replace ( "{max_int}" , int . MaxValue . ToString ( ) ) ;
231
+ maskProcess = new Process
232
+ {
233
+ StartInfo =
234
+ {
235
+ FileName = ffmpegFile ,
236
+ Arguments = $ "{ inputArgs } { outputArgsMask } ",
237
+ UseShellExecute = false ,
238
+ CreateNoWindow = true ,
239
+ RedirectStandardInput = true ,
240
+ RedirectStandardOutput = true ,
241
+ RedirectStandardError = true
242
+ }
243
+ } ;
244
+
245
+ if ( File . Exists ( renderOptions . OutputFileMask ) )
246
+ File . Delete ( renderOptions . OutputFileMask ) ;
247
+
248
+ maskProcess . Start ( ) ;
249
+ maskProcess . BeginErrorReadLine ( ) ;
250
+ maskProcess . BeginOutputReadLine ( ) ;
251
+ maskStream = new BinaryWriter ( maskProcess . StandardInput . BaseStream ) ;
252
+ }
253
+
224
254
using ( var ffmpegStream = new BinaryWriter ( process . StandardInput . BaseStream ) )
225
255
{
226
256
bufferCanvas . Clear ( renderOptions . BackgroundColor ) ;
@@ -363,6 +393,19 @@ private void RenderVideo(ChatRenderOptions renderOptions, Queue<TwitchComment> f
363
393
var data = SKData . Create ( pix . GetPixels ( ) , pix . Info . BytesSize ) ;
364
394
var bytes = data . ToArray ( ) ;
365
395
ffmpegStream . Write ( bytes ) ;
396
+ if ( renderOptions . GenerateMask )
397
+ {
398
+ SKBitmap maskBitmap = new SKBitmap ( renderOptions . ChatWidth , renderOptions . ChatHeight ) ;
399
+ using ( SKCanvas maskCanvas = new SKCanvas ( maskBitmap ) )
400
+ {
401
+ maskCanvas . Clear ( SKColors . White ) ;
402
+ maskCanvas . DrawBitmap ( bufferBitmap , 0 , 0 , new SKPaint ( ) { BlendMode = SKBlendMode . DstIn } ) ;
403
+ }
404
+ var pixMask = maskBitmap . PeekPixels ( ) ;
405
+ var dataMask = SKData . Create ( pixMask . GetPixels ( ) , pixMask . Info . BytesSize ) ;
406
+ var bytesMask = dataMask . ToArray ( ) ;
407
+ maskStream . Write ( bytesMask ) ;
408
+ }
366
409
367
410
foreach ( var emote in displayedGifs )
368
411
{
@@ -381,6 +424,11 @@ private void RenderVideo(ChatRenderOptions renderOptions, Queue<TwitchComment> f
381
424
}
382
425
}
383
426
}
427
+ if ( renderOptions . GenerateMask )
428
+ {
429
+ maskStream . Dispose ( ) ;
430
+ maskProcess . WaitForExit ( ) ;
431
+ }
384
432
stopwatch . Stop ( ) ;
385
433
progress . Report ( new ProgressReport ( ) { reportType = ReportType . Log , data = $ "FINISHED. RENDER TIME: { ( int ) stopwatch . Elapsed . TotalSeconds } s SPEED: { ( duration / stopwatch . Elapsed . TotalSeconds ) . ToString ( "0.##" ) } x" } ) ;
386
434
process . WaitForExit ( ) ;
0 commit comments