Force UTF-8 output from yt-dlp to fix mojibake in redirected streams - #81
Open
Aimeast wants to merge 1 commit into
Open
Force UTF-8 output from yt-dlp to fix mojibake in redirected streams#81Aimeast wants to merge 1 commit into
Aimeast wants to merge 1 commit into
Conversation
Aimeast
force-pushed
the
fix/utf8-pipe-encoding
branch
from
August 10, 2026 14:45
2ccc3e6 to
797f44c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On Windows, yt-dlp output captured through the library is garbled (mojibake).
For example the YouTube bot-check error message
is reported as
Root cause
yt-dlp is a Python program. When its stdout/stderr are redirected to a pipe,
Python picks their encoding from the system locale at process startup
(
locale.getpreferredencoding()-> the ANSI code page, e.g. GBK/cp936 onChinese Windows) instead of always using UTF-8. The library always reads the
process output as UTF-8 (
StandardOutputEncoding = Encoding.UTF8), so everynon-ASCII byte sequence was decoded into U+FFFD replacement characters.
Confirmed with
yt-dlp.exe -v:Reading the pipes as GBK works on a GBK-locale machine but breaks on machines
with a different ANSI code page (e.g. cp1252) and is not future-proof once
yt-dlp ships builds that default to UTF-8 (Python 3.15+).
Fix
Append
--encoding utf-8to the yt-dlp arguments unless the caller alreadyset
OptionSet.Encoding, which is respected and left untouched. This forcesyt-dlp to emit UTF-8 on both stdout and stderr regardless of the machine's
locale, matching the encoding the output is always read with.
The public API is unchanged - only the internal
ConvertToArgsmethod inYoutubeDLSharp/YoutubeDLProcess.cswas modified.Verification
dotnet buildpasses with 0 errors.(
B2 E2 CA D4for 测试); with--encoding utf-8they are UTF-8(
E6 B5 8B E8 AF 95).YoutubeDLProcess.RunAsyncwith a realyt-dlp.exe(2026.07.04, PyInstaller build) on a URL whose error output contains
non-ASCII text - before the fix every non-ASCII char became
U+FFFD;after the fix the output decodes correctly with zero
U+FFFD.OptionSetproduces--encoding utf-8;OptionSet.Encoding = "gbk"produces--encoding "gbk"(not overridden).