From 70c8c57401bb8439618eeb00d2393cadd1ccd73a Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Wed, 27 Nov 2024 00:09:04 +0100 Subject: [PATCH] Dump ffmpeg stderr to ESPhome debug log (#130808) * dump the stderr from ffmpeg to debug log * add pid to indentify the ffmpeg process * be more explosive :) * move stderr task into _write_ffmpeg_data --- .../components/esphome/ffmpeg_proxy.py | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/esphome/ffmpeg_proxy.py b/homeassistant/components/esphome/ffmpeg_proxy.py index 2dacae52f75..9484d1e7593 100644 --- a/homeassistant/components/esphome/ffmpeg_proxy.py +++ b/homeassistant/components/esphome/ffmpeg_proxy.py @@ -212,6 +212,10 @@ class FFmpegConvertResponse(web.StreamResponse): assert proc.stdout is not None assert proc.stderr is not None + stderr_task = self.hass.async_create_background_task( + self._dump_ffmpeg_stderr(proc), "ESPHome media proxy dump stderr" + ) + try: # Pull audio chunks from ffmpeg and pass them to the HTTP client while ( @@ -230,18 +234,14 @@ class FFmpegConvertResponse(web.StreamResponse): raise # don't log error except: _LOGGER.exception("Unexpected error during ffmpeg conversion") - - # Process did not exit successfully - stderr_text = "" - while line := await proc.stderr.readline(): - stderr_text += line.decode() - _LOGGER.error("FFmpeg output: %s", stderr_text) - raise finally: # Allow conversion info to be removed self.convert_info.is_finished = True + # stop dumping ffmpeg stderr task + stderr_task.cancel() + # Terminate hangs, so kill is used if proc.returncode is None: proc.kill() @@ -250,6 +250,16 @@ class FFmpegConvertResponse(web.StreamResponse): if request.transport and not request.transport.is_closing(): await writer.write_eof() + async def _dump_ffmpeg_stderr( + self, + proc: asyncio.subprocess.Process, + ) -> None: + assert proc.stdout is not None + assert proc.stderr is not None + + while self.hass.is_running and (chunk := await proc.stderr.readline()): + _LOGGER.debug("ffmpeg[%s] output: %s", proc.pid, chunk.decode().rstrip()) + class FFmpegProxyView(HomeAssistantView): """FFmpeg web view to convert audio and stream back to client."""