@@ -940,7 +940,11 @@ def serve_media_file(self, filename, head_only=False):
940940 return
941941
942942 def handle_range_request (self , file_path , file_size , mime_type , range_header ):
943- """Handle HTTP range requests for streaming"""
943+ """Handle HTTP range requests for streaming
944+
945+ Uses smaller chunk sizes compared to full file streaming to improve
946+ responsiveness during video resume/seek operations and reduce choppiness.
947+ """
944948 try :
945949 range_match = range_header .replace ("bytes=" , "" ).split ("-" )
946950 start = int (range_match [0 ]) if range_match [0 ] else 0
@@ -1007,22 +1011,25 @@ def handle_range_request(self, file_path, file_size, mime_type, range_header):
10071011 # Set a timeout for socket operations to prevent blocking
10081012 self .wfile .flush ()
10091013
1010- # Use larger chunks for better performance
1011- base_chunk_size = 65536 # 64KB base chunk size
1014+ # Use smaller chunks for range requests to improve resume/seek performance
1015+ # Large chunks can cause choppy playback when resuming video
1016+ base_chunk_size = 8192 # 8KB base chunk size for range requests
10121017
10131018 while remaining > 0 :
1014- # Adaptive chunk size - larger for big remaining data
1019+ # For range requests, use smaller chunks for better streaming
1020+ # This helps with choppy playback when resuming videos
10151021 chunk_size = min (base_chunk_size , remaining )
1016- if remaining > 1024 * 1024 : # If more than 1MB remaining
1017- chunk_size = min (128 * 1024 , remaining ) # Use 128KB chunks
1022+ if remaining > 2 * 1024 * 1024 : # If more than 2MB remaining
1023+ chunk_size = min (16 * 1024 , remaining ) # Use 16KB chunks max
10181024
10191025 chunk = f .read (chunk_size )
10201026 if not chunk :
10211027 break
10221028 try :
10231029 self .wfile .write (chunk )
1024- # Only flush periodically to improve performance
1025- if remaining % (base_chunk_size * 4 ) == 0 :
1030+ # Flush more frequently for range requests to reduce latency
1031+ # This helps with responsive seeking and resume functionality
1032+ if remaining % (base_chunk_size * 2 ) == 0 :
10261033 self .wfile .flush ()
10271034 remaining -= len (chunk )
10281035 except BrokenPipeError :
0 commit comments