SubtitleEdit/subtitleedit · error · InvalidOperationException
Tesseract exited with code {proc.ExitCode}: {err}
Error message
Tesseract exited with code {proc.ExitCode}: {err} What it means
Thrown after the Tesseract process ran but exited with a non-zero code. The captured stderr (drained concurrently to avoid pipe deadlock) is appended to the message, so it usually names the real cause (missing language data, corrupt image, bad --psm, model errors).
Source
Thrown at src/seconv/Core/TesseractOcrEngine.cs:100
ArgumentList = { pngPath, "stdout", "-l", Language, "--psm", "6" },
RedirectStandardOutput = true,
RedirectStandardError = true,
StandardOutputEncoding = System.Text.Encoding.UTF8,
StandardErrorEncoding = System.Text.Encoding.UTF8,
UseShellExecute = false,
CreateNoWindow = true,
};
using var proc = Process.Start(psi)
?? throw new InvalidOperationException("Failed to start tesseract process.");
// Drain stderr concurrently — Tesseract emits warnings to stderr, and reading
// stdout to completion while stderr fills the pipe buffer would deadlock.
var stderrTask = proc.StandardError.ReadToEndAsync();
var stdout = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
if (proc.ExitCode != 0)
{
var err = stderrTask.GetAwaiter().GetResult();
throw new InvalidOperationException($"Tesseract exited with code {proc.ExitCode}: {err}");
}
return stdout.Trim();
}
finally
{
try { File.Delete(pngPath); } catch { /* best-effort */ }
prepped.Dispose();
}
}
private static SKBitmap Preprocess(SKBitmap source)
{
const int scale = 2;
var w = source.Width * scale;
var h = source.Height * scale;
var prepped = new SKBitmap(new SKImageInfo(w, h, SKColorType.Rgba8888, SKAlphaType.Opaque));
using var canvas = new SKCanvas(prepped);
canvas.Clear(SKColors.White);View on GitHub (pinned to 17a9f07487)
Solutions
- Install the requested language pack (apt install tesseract-ocr-fra / brew install tesseract-lang, or drop the .traineddata into the tessdata folder).
- Set TESSDATA_PREFIX to the directory holding the .traineddata files and retry.
- Read the stderr in the message — it states the exact Tesseract error.
- Verify the Tesseract version matches the model format.
Example fix
// before: -l fra but only eng installed
engine = TesseractOcrEngine.Create("fra");
// after: install pack + keep language
$ apt install tesseract-ocr-fra
engine = TesseractOcrEngine.Create("fra"); Defensive patterns
Strategy: try-catch
Validate before calling
// Verify the requested language's traineddata exists
var tessdata = Environment.GetEnvironmentVariable("TESSDATA_PREFIX") ?? Path.Combine(Path.GetDirectoryName(ExecutablePath) ?? ".", "tessdata");
if (!File.Exists(Path.Combine(tessdata, Language + ".traineddata")))
throw new InvalidOperationException($"Missing {Language}.traineddata in {tessdata}"); Type guard
null
Try / catch
try { return engine.Recognize(bmp); }
catch (InvalidOperationException ex) when (ex.Message.StartsWith("Tesseract exited")) {
logger.Error("Tesseract stderr: " + ex.Message);
return string.Empty;
} Prevention
- Install every language pack you intend to request.
- Keep TESSDATA_PREFIX pointing at the populated tessdata dir.
- Match model versions to the installed Tesseract major version.
When it happens
Trigger: proc.ExitCode != 0 after Process.Start of tesseract with arguments [pngPath, stdout, -l Language, --psm 6]. stderr is awaited and folded into the message.
Common situations: Requested a language whose .traineddata is not installed (e.g. -l fra with only eng present); Tesseract version mismatch with the model; malformed/expired PNG written to the temp work dir; insufficient tessdata dir / TESSDATA_PREFIX unset.
Related errors
- Tesseract not found on PATH. Install it from https://tessera
- Failed to start tesseract process.
- No image handler for format '{options.Format}'
- API key invalid (or perhaps billing/API is not enabled)?
- "Perhaps billing is not enabled (or API not enabled or API k
AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13).
Data as JSON: /api/errors/908f44a940bb23b8.
Report an issue: GitHub.