ffmpeg_output.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """Turning an ffmpeg subprocess's stderr into a log line worth reading (#2968).
  2. ffmpeg opens every run with ~20 lines of version, build and library banner and
  3. prints its diagnosis *last*. Truncating that from the front -- ``stderr[:200]``,
  4. which is what most call sites did -- keeps the banner and throws the diagnosis
  5. away. A reporter's H2D produced twelve of these, and every one of them read
  6. ffmpeg frame bytes capture failed (code 183): ffmpeg version 7.1.4-0+deb13u1
  7. Copyright (c) 2000-2026 the FFmpeg developers built with gcc 14 (Debian
  8. 14.2.0-19) configuration: --prefix=/usr --extra-version=0+deb13u1 --toolch
  9. -- 200 characters that are identical on every install and say nothing about why
  10. the capture failed. The exit code was the only usable byte in the whole line.
  11. The banner-stripping summariser this module holds was written for #925 and
  12. lived as a private helper in ``api/routes/camera.py``, where the streaming
  13. endpoint used it. Ten other places log ffmpeg or ffprobe stderr -- snapshot
  14. capture, last-frame extraction, the layer-timelapse stitch, the archive's MP4
  15. conversion, external USB and RTSP capture and streaming, and timelapse
  16. post-processing. Seven of them truncated from the front, two logged the whole
  17. banner, and one already kept the tail. They all come here now, so they cannot
  18. drift again.
  19. Redaction is part of the summary rather than each caller's job. ffmpeg echoes
  20. its input URL back in the ``Input #0`` line, so a camera password or a printer
  21. access code reaches stderr on any failure; seven of those ten logged it
  22. unmasked. A helper that redacts is one that cannot be called wrong.
  23. Kept as a leaf module -- stdlib plus :mod:`core.logging_filters`, which is
  24. itself stdlib-only -- so the services and the route can all reach it without
  25. pulling a startup graph behind them.
  26. """
  27. from __future__ import annotations
  28. from backend.app.core.logging_filters import redact_url_credentials
  29. # What ffmpeg prints before it has anything to say. Every line of the banner is
  30. # either the version line or an indented continuation, and a real diagnostic is
  31. # never indented this way, so the match is on the exact prefixes rather than on
  32. # indentation alone -- `` Duration: ...`` and `` Stream #0:0 ...`` are
  33. # indented too and are worth keeping.
  34. _BANNER_PREFIXES = (
  35. "ffmpeg version ",
  36. "ffprobe version ",
  37. " built with ",
  38. " configuration:",
  39. " libavutil ",
  40. " libavcodec ",
  41. " libavformat ",
  42. " libavdevice ",
  43. " libavfilter ",
  44. " libswscale ",
  45. " libswresample ",
  46. " libpostproc ",
  47. )
  48. # How much of the tail to keep. ffmpeg's diagnosis is the last thing it writes,
  49. # and ten lines is enough to carry the error plus the input analysis that
  50. # explains it without letting a chatty decoder rotate the log file.
  51. _MAX_LINES = 10
  52. # And a ceiling on the whole thing. Ten lines is only a bound on the log record
  53. # if the lines are a sane length, and ffmpeg quotes what the peer sent it back
  54. # at us -- a printer's RTSP response is not something Bambuddy controls. Well
  55. # above any real diagnosis, so this only ever trims a line that was already not
  56. # going to be read.
  57. _MAX_CHARACTERS = 2000
  58. # What to log when the summary is empty. A failure whose stderr held nothing but
  59. # the banner still deserves a line saying so -- ``failed: `` with an empty tail
  60. # reads like a truncation bug rather than a printer that closed the connection.
  61. NO_FFMPEG_OUTPUT = "no diagnostic output"
  62. def summarize_ffmpeg_stderr(text: str | bytes | None) -> str:
  63. """Strip ffmpeg's boilerplate banner and keep the last lines that matter.
  64. Accepts raw ``bytes`` as well as ``str`` and decodes with ``errors=
  65. "replace"``: ffmpeg copies fragments of the stream into its error messages,
  66. so a bare ``.decode()`` at the call site can raise ``UnicodeDecodeError``
  67. while reporting an unrelated failure. Losing the diagnosis to a second
  68. exception is the one outcome worse than logging the banner.
  69. Returns ``""`` when there is nothing left after the banner, which is the
  70. signal the streaming endpoint uses to stay quiet. One-shot callers that log
  71. unconditionally should fall back to :data:`NO_FFMPEG_OUTPUT`.
  72. """
  73. if not text:
  74. return ""
  75. if isinstance(text, (bytes, bytearray)):
  76. text = text.decode(errors="replace")
  77. # Redaction runs on the whole string before anything is dropped: a
  78. # credentialed URL that straddles the cut would otherwise leave its tail in
  79. # the log with no ``@`` left for the pattern to anchor on.
  80. text = redact_url_credentials(text) or ""
  81. meaningful = [line for line in text.splitlines() if line.strip() and not line.startswith(_BANNER_PREFIXES)]
  82. summary = "\n".join(meaningful[-_MAX_LINES:])
  83. if len(summary) > _MAX_CHARACTERS:
  84. # From the end, for the same reason the whole module exists.
  85. summary = "..." + summary[-_MAX_CHARACTERS:]
  86. return summary