From 8de3a95c6bdc98caca65a046d3a3faf2332d975f Mon Sep 17 00:00:00 2001
From: rlaphoenix <rlaphoenix@pm.me>
Date: Tue, 20 Feb 2024 01:35:58 +0000
Subject: [PATCH] Flush file buffers when merging DASH or HLS segments

---
 devine/core/manifests/dash.py | 1 +
 devine/core/manifests/hls.py  | 2 ++
 2 files changed, 3 insertions(+)

diff --git a/devine/core/manifests/dash.py b/devine/core/manifests/dash.py
index 93dde88..425292f 100644
--- a/devine/core/manifests/dash.py
+++ b/devine/core/manifests/dash.py
@@ -483,6 +483,7 @@ class DASH:
                     segment_data = try_ensure_utf8(segment_data)
                     segment_data = html.unescape(segment_data.decode("utf8")).encode("utf8")
                 f.write(segment_data)
+                f.flush()
                 segment_file.unlink()
                 progress(advance=1)
 
diff --git a/devine/core/manifests/hls.py b/devine/core/manifests/hls.py
index 580f924..d80d4a1 100644
--- a/devine/core/manifests/hls.py
+++ b/devine/core/manifests/hls.py
@@ -310,6 +310,7 @@ class HLS:
                         x.write(map_data[1])
                     for file in via:
                         x.write(file.read_bytes())
+                        x.flush()
                         if delete:
                             file.unlink()
 
@@ -485,6 +486,7 @@ class HLS:
                         continue
                     discontinuity_data = discontinuity_file.read_bytes()
                     f.write(discontinuity_data)
+                    f.flush()
             shutil.rmtree(save_dir)
 
         progress(downloaded="Downloaded")