Fix: extremely short audio clips are not working
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Thu, 6 Mar 2025 18:16:20 +0000 (19:16 +0100)
committerMarcel van der Veldt <m.vanderveldt@outlook.com>
Thu, 6 Mar 2025 18:16:20 +0000 (19:16 +0100)
music_assistant/controllers/streams.py
music_assistant/helpers/audio.py
music_assistant/helpers/ffmpeg.py

index 411ed2a09e541642a5fd733845cd9a9d7f11905c..d3b37837ac64cf85a424aae06aae39813457604a 100644 (file)
@@ -398,7 +398,10 @@ class StreamsController(CoreController):
             # some players do not like it when we dont return anything after an error
             # so we send some silence so they move on to the next track on their own (hopefully)
             async for chunk in get_silence(10, output_format):
-                await resp.write(chunk)
+                try:
+                    await resp.write(chunk)
+                except (BrokenPipeError, ConnectionResetError, ConnectionError):
+                    break
         return resp
 
     async def serve_queue_flow_stream(self, request: web.Request) -> web.Response:
index 9630dbdcb37ac8150ffd9a7645b2f33b65d34ab7..a6aea7603294a737c9e22c108ff398e68a74c8c6 100644 (file)
@@ -691,10 +691,6 @@ async def get_media_stream(
                 buffer = buffer[pcm_format.pcm_sample_size :]
 
         # end of audio/track reached
-        if bytes_sent == 0:
-            # edge case: no audio data was sent
-            raise AudioError("No audio was received")
-
         logger.log(VERBOSE_LOG_LEVEL, "End of stream reached.")
         if strip_silence_end and buffer:
             # strip silence from end of audio
@@ -710,6 +706,9 @@ async def get_media_stream(
         del buffer
         # wait until stderr also completed reading
         await ffmpeg_proc.wait_with_timeout(5)
+        if bytes_sent == 0:
+            # edge case: no audio data was sent
+            raise AudioError("No audio was received")
         finished = True
     except (Exception, GeneratorExit) as err:
         if isinstance(err, asyncio.CancelledError | GeneratorExit):
index 204b36d52a7c991a3c4b27e95a07ad38875222e7..bafa8e728da086319a599f5f890e74dd128da7e7 100644 (file)
@@ -287,7 +287,7 @@ def get_ffmpeg_args(  # noqa: PLR0915
             "-f",
             output_format.content_type.value,
         ]
-    elif input_format == output_format and not extra_args:
+    elif input_format == output_format and not filter_params and not extra_args:
         # passthrough-mode (e.g. for creating the cache)
         if output_format.content_type in (
             ContentType.MP4,