From 797f44c58d54dd1a8856bf17fb40458343bf68b8 Mon Sep 17 00:00:00 2001 From: Aimeast Date: Mon, 10 Aug 2026 22:44:57 +0800 Subject: [PATCH] fix: force yt-dlp pipe output to UTF-8 --- YoutubeDLSharp/YoutubeDLProcess.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/YoutubeDLSharp/YoutubeDLProcess.cs b/YoutubeDLSharp/YoutubeDLProcess.cs index dbd1133..79b5a88 100644 --- a/YoutubeDLSharp/YoutubeDLProcess.cs +++ b/YoutubeDLSharp/YoutubeDLProcess.cs @@ -57,7 +57,18 @@ public YoutubeDLProcess(string executablePath = "yt-dlp.exe") } internal string ConvertToArgs(string[] urls, OptionSet options) - => options.ToString() + " -- " + (urls != null ? String.Join(" ", urls.Select(s => $"\"{s}\"")) : String.Empty); + { + var args = options.ToString(); + // By default, force yt-dlp to write its standard output and error output in UTF-8, + // which is the encoding the process output is always read with below. Without this, + // yt-dlp (a Python program) uses the system locale's ANSI code page (e.g. GBK on + // Chinese Windows) for redirected pipes, and decoding those bytes as UTF-8 turns + // every non-ASCII character into a U+FFFD replacement character. An explicit + // OptionSet.Encoding is respected and left untouched. + if (options.Encoding == null) + args += " --encoding utf-8"; + return args + " -- " + (urls != null ? String.Join(" ", urls.Select(s => $"\"{s}\"")) : String.Empty); + } internal void RedirectToError(DataReceivedEventArgs e) => ErrorReceived?.Invoke(this, e);