MathewSachin/Captura · error · Exception

Cannot connect Audio pipe to FFmpeg

Error message

Cannot connect Audio pipe to FFmpeg

What it means

Thrown by FFmpegWriter.WriteAudio (src/Captura.FFmpeg/Video/FFmpegVideoWriter.cs:157) when _audioPipe.WaitForConnection(5000) returns false on the first audio block. FFmpegService.WaitForConnection (src/Captura.FFmpeg/FFmpegService.cs:101-113) waits up to the timeout for the client (ffmpeg) to connect to the `captura-<guid>` named pipe; if ffmpeg never opens the audio input, the 5s window elapses.

Source

Thrown at src/Captura.FFmpeg/Video/FFmpegVideoWriter.cs:157

        /// </summary>
        /// <param name="Buffer">Buffer containing audio data.</param>
        /// <param name="Length">Length of audio data in bytes.</param>
        public void WriteAudio(byte[] Buffer, int Offset, int Length)
        {
            // Might happen when writing Gif
            if (_audioPipe == null)
                return;

            if (_ffmpegProcess.HasExited)
            {
                throw new FFmpegException( _ffmpegProcess.ExitCode);
            }

            if (_firstAudio)
            {
                if (!_audioPipe.WaitForConnection(5000))
                {
                    throw new Exception("Cannot connect Audio pipe to FFmpeg");
                }

                _firstAudio = false;
            }

            // We don't need semaphores for audio since audio frames arrive less often.
            _lastAudio?.Wait();

            // Drop audio bytes to sync with video once we've reached stability from frame side.
            if (_initialStability)
            {
                var audioBytesToDrop = _skippedFrames * _audioBytesPerFrame - _audioBytesDropped;

                // Drop whole buffer
                if (audioBytesToDrop >= Length)
                {
                    _audioBytesDropped += Length;
                    return;

View on GitHub (pinned to 3fdf41529b)

Solutions

  1. Confirm _ffmpegProcess.HasExited immediately before WaitForConnection and, if dead, read the log instead.
  2. Ensure at least one WriteFrame happens (video pipe connects) before audio is expected, matching ffmpeg's input ordering.
  3. Increase the 5000 ms timeout on slow/loaded machines.
  4. Verify the audioPipeName passed to AddInputPipe matches the NamedPipeServerStream name.

Example fix

// before
if (!_audioPipe.WaitForConnection(5000))
    throw new Exception("Cannot connect Audio pipe to FFmpeg");
// after - fail with the real reason if ffmpeg already died
if (!_audioPipe.WaitForConnection(5000))
    throw new Exception(_ffmpegProcess.HasExited
        ? $"FFmpeg exited (code {_ffmpegProcess.ExitCode}) before opening the audio pipe; see ffmpeg log"
        : "Audio pipe connect timed out after 5s");
Defensive patterns

Strategy: retry

Validate before calling

// ensure ffmpeg opened the video pipe first, and is still alive, before expecting audio
if (_ffmpegProcess.HasExited) throw new FFmpegException(_ffmpegProcess.ExitCode);
// send at least one frame so ffmpeg proceeds to open its inputs

Try / catch

try { /* first WriteAudio */ }
catch (Exception e) when (e.Message.Contains("Audio pipe")) { if (_ffmpegProcess.HasExited) throw new FFmpegException(_ffmpegProcess.ExitCode); else /* raise timeout / retry once */ }

Prevention

When it happens

Trigger: The first WriteAudio call when ffmpeg has not connected to the audio named pipe within 5000 ms. Happens if ffmpeg exited (but the HasExited guard at line 148 passed), if ffmpeg is still alive but blocked on the video pipe, or if the pipe name in the args does not match the created pipe.

Common situations: ffmpeg is stuck waiting on the video input pipe first (it had not received a frame yet) while audio arrives; ffmpeg parsed the audio pipe name incorrectly; system under heavy load so ffmpeg's startup exceeded 5s.

Related errors


AI-assisted analysis of MathewSachin/Captura@3fdf41529b (2026-08-13). Data as JSON: /api/errors/6e91a827d7b24e04. Report an issue: GitHub.