{"record":{"id":"6e91a827d7b24e04","repo":"MathewSachin/Captura","slug":"cannot-connect-audio-pipe-to-ffmpeg","errorCode":null,"errorMessage":"Cannot connect Audio pipe to FFmpeg","messagePattern":"Cannot connect Audio pipe to FFmpeg","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/Captura.FFmpeg/Video/FFmpegVideoWriter.cs","lineNumber":157,"sourceCode":"        /// </summary>\n        /// <param name=\"Buffer\">Buffer containing audio data.</param>\n        /// <param name=\"Length\">Length of audio data in bytes.</param>\n        public void WriteAudio(byte[] Buffer, int Offset, int Length)\n        {\n            // Might happen when writing Gif\n            if (_audioPipe == null)\n                return;\n\n            if (_ffmpegProcess.HasExited)\n            {\n                throw new FFmpegException( _ffmpegProcess.ExitCode);\n            }\n\n            if (_firstAudio)\n            {\n                if (!_audioPipe.WaitForConnection(5000))\n                {\n                    throw new Exception(\"Cannot connect Audio pipe to FFmpeg\");\n                }\n\n                _firstAudio = false;\n            }\n\n            // We don't need semaphores for audio since audio frames arrive less often.\n            _lastAudio?.Wait();\n\n            // Drop audio bytes to sync with video once we've reached stability from frame side.\n            if (_initialStability)\n            {\n                var audioBytesToDrop = _skippedFrames * _audioBytesPerFrame - _audioBytesDropped;\n\n                // Drop whole buffer\n                if (audioBytesToDrop >= Length)\n                {\n                    _audioBytesDropped += Length;\n                    return;","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/MathewSachin/Captura/blob/3fdf41529bf4d50cd7a85aedd856f30e34279a47/src/Captura.FFmpeg/Video/FFmpegVideoWriter.cs#L139-L175","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Confirm _ffmpegProcess.HasExited immediately before WaitForConnection and, if dead, read the log instead.","Ensure at least one WriteFrame happens (video pipe connects) before audio is expected, matching ffmpeg's input ordering.","Increase the 5000 ms timeout on slow/loaded machines.","Verify the audioPipeName passed to AddInputPipe matches the NamedPipeServerStream name."],"exampleFix":"// before\nif (!_audioPipe.WaitForConnection(5000))\n    throw new Exception(\"Cannot connect Audio pipe to FFmpeg\");\n// after - fail with the real reason if ffmpeg already died\nif (!_audioPipe.WaitForConnection(5000))\n    throw new Exception(_ffmpegProcess.HasExited\n        ? $\"FFmpeg exited (code {_ffmpegProcess.ExitCode}) before opening the audio pipe; see ffmpeg log\"\n        : \"Audio pipe connect timed out after 5s\");","handlingStrategy":"retry","validationCode":"// ensure ffmpeg opened the video pipe first, and is still alive, before expecting audio\nif (_ffmpegProcess.HasExited) throw new FFmpegException(_ffmpegProcess.ExitCode);\n// send at least one frame so ffmpeg proceeds to open its inputs","typeGuard":null,"tryCatchPattern":"try { /* first WriteAudio */ }\ncatch (Exception e) when (e.Message.Contains(\"Audio pipe\")) { if (_ffmpegProcess.HasExited) throw new FFmpegException(_ffmpegProcess.ExitCode); else /* raise timeout / retry once */ }","preventionTips":["Make sure the video frame path connects before audio is expected (ffmpeg input ordering).","On slow machines, raise the 5000 ms WaitForConnection timeout.","Verify the audioPipeName passed to AddInputPipe matches the NamedPipeServerStream."],"tags":["ffmpeg","named-pipe","recording","audio","csharp"],"backgroundTag":null,"analyzedSha":"3fdf41529bf4d50cd7a85aedd856f30e34279a47","analyzedAt":"2026-08-13T19:35:27.486Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}