Skip to content

Commit

Permalink
Dump ffmpeg stderr to ESPhome debug log (#130808)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mib1185 authored Nov 26, 2024
1 parent ce20670 commit 70c8c57
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions homeassistant/components/esphome/ffmpeg_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ async def _write_ffmpeg_data(
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 (
Expand All @@ -230,18 +234,14 @@ async def _write_ffmpeg_data(
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()
Expand All @@ -250,6 +250,16 @@ async def _write_ffmpeg_data(
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."""
Expand Down

0 comments on commit 70c8c57

Please sign in to comment.