{"record":{"id":"0df59109376a3723","repo":"MathewSachin/Captura","slug":"an-error-occurred-with-ffmpeg-exit-code-exitcod","errorCode":null,"errorMessage":"An Error Occurred with FFmpeg, Exit Code: {ExitCode}.\\nSee FFmpeg Log for more info.","messagePattern":"An Error Occurred with FFmpeg, Exit Code: (.+?)\\.\\\\nSee FFmpeg Log for more info\\.","errorType":"exception","errorClass":"FFmpegException","httpStatus":null,"severity":"error","filePath":"src/Captura.FFmpeg/Audio/FFmpegAudioWriter.cs","lineNumber":54,"sourceCode":"\n        public void Dispose()\n        {\n            Flush();\n\n            _ffmpegIn.Close();\n            _ffmpegProcess.WaitForExit();\n        }\n\n        public void Flush()\n        {\n            _ffmpegIn.Flush();\n        }\n\n        public void Write(byte[] Data, int Offset, int Count)\n        {\n            if (_ffmpegProcess.HasExited)\n            {\n                throw new FFmpegException(_ffmpegProcess.ExitCode);\n            }\n\n            _ffmpegIn.Write(Data, Offset, Count);\n        }\n    }\n}\n","sourceCodeStart":36,"sourceCodeEnd":61,"githubUrl":"https://github.com/MathewSachin/Captura/blob/3fdf41529bf4d50cd7a85aedd856f30e34279a47/src/Captura.FFmpeg/Audio/FFmpegAudioWriter.cs#L36-L61","documentation":"Thrown by FFmpegAudioWriter.Write (src/Captura.FFmpeg/Audio/FFmpegAudioWriter.cs:54) as new FFmpegException(exitCode) when _ffmpegProcess.HasExited is true at write time. FFmpegException (src/Captura.FFmpeg/FFmpegException.cs:5-9) only carries the exit code; the real cause is in the ffmpeg log captured by FFmpegService.StartFFmpeg via ErrorDataReceived into IFFmpegLogRepository.","triggerScenarios":"Calling Write(byte[]) after the ffmpeg child process has already terminated with a non-zero exit code. The process died during encoding (bad codec/args, missing encoder build, unwritable output path) and the next audio chunk write detects HasExited.","commonSituations":"Codec args reference an encoder not compiled into this ffmpeg build (e.g. libmp3lame absent); output directory is read-only or the file is locked by another process; sample rate/channel count args are inconsistent with the PCM stream; disk full.","solutions":["Read the matching IFFmpegLogRepository entry for this output file to get ffmpeg's stderr (the actual error line).","Confirm the requested audio codec/encoder is supported: run `ffmpeg.exe -encoders` and match the name exactly.","Verify the output path is writable and on a volume with free space.","Cross-check Frequency/Channels passed to FFmpegAudioWriter against the source audio."],"exampleFix":"// before\n_ffmpegIn.Write(Data, Offset, Count); // happens after HasExited guard throws\n// after - surface the captured ffmpeg log instead of only the exit code\ncatch (FFmpegException e)\n{\n    var log = ServiceProvider.Get<IFFmpegLogRepository>().Items.LastOrDefault();\n    throw new FFmpegException(e.Message + $\"\\nFFmpeg stderr:\\n{log}\", e);\n}","handlingStrategy":"try-catch","validationCode":"// before each write, confirm ffmpeg is still alive (cheaper than catching)\nif (_writer == null || _ffmpegProcessHasExitedFlag) return;\n// (the writer already checks HasExited internally; pre-check avoids a wasted call)","typeGuard":null,"tryCatchPattern":"try { _writer.Write(data, 0, data.Length); }\ncatch (FFmpegException e) { var log = ServiceProvider.Get<IFFmpegLogRepository>().Items.Last(); /* show log, stop recording */ }","preventionTips":["Validate the audio codec string against `ffmpeg.exe -encoders` before recording.","Confirm the output path is on a writable volume with free space.","Surface the IFFmpegLogRepository entry to the user instead of only the exit code."],"tags":["ffmpeg","encoding","process-exit","csharp"],"backgroundTag":null,"analyzedSha":"3fdf41529bf4d50cd7a85aedd856f30e34279a47","analyzedAt":"2026-08-13T19:35:27.486Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}