From eaa1547b10f439a037866cee054a7810567aa6fc Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 8 Sep 2022 02:34:34 +0800 Subject: Support custom file write buffer size. --- recording/runner.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'recording/runner.go') diff --git a/recording/runner.go b/recording/runner.go index 7b82a5c..c95e559 100644 --- a/recording/runner.go +++ b/recording/runner.go @@ -8,6 +8,7 @@ package recording import ( "bilibili-livestream-archiver/bilibili" "bilibili-livestream-archiver/common" + "bufio" "context" "encoding/json" "errors" @@ -165,15 +166,25 @@ func record( ) filePath := path.Join(task.Download.SaveDirectory, fileName) file, err := os.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) - defer func() { _ = file.Close() }() if err != nil { logger.Printf("ERROR: Cannot open file for writing: %v", err) cancelled = true return } + defer func() { _ = file.Close() }() + + // buffered writer + fWriter := bufio.NewWriterSize(file, task.Download.DiskWriteBufferBytes) + defer func() { + err := fWriter.Flush() + if err != nil { + logger := log.Default() + logger.Printf("Failed to flush buffered file write data: %v\n", err) + } + }() logger.Printf("Recording live stream to file \"%v\"...", filePath) - err = bi.CopyLiveStream(ctx, task.RoomId, streamSource, file) + err = bi.CopyLiveStream(ctx, task.RoomId, streamSource, fWriter) cancelled = err == nil || errors.Is(err, context.Canceled) if !cancelled { // real error happens -- cgit v1.2.3