You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python subprocess.check_output调用yt-dlp时输出内容损坏的问题

yt-dlp通过Python subprocess调用时中文文件名丢失的问题

问题原因

Windows系统中,直接运行Python脚本时,subprocess.check_output默认使用系统的OEM编码(比如GBK)解码输出,但yt-dlp输出的文件名是UTF-8编码的中文内容,编码不匹配导致中文乱码甚至被截断丢失。而VSCode调试时,终端环境默认使用UTF-8编码,所以能正确解码输出。

解决办法

方法1:指定subprocess的解码编码为UTF-8

调用check_output时,直接通过encoding参数指定用UTF-8解码:

import subprocess
filename = subprocess.check_output(
    ["yt-dlp", "--get-filename", "https://www.youtube.com/watch?v=XKuL5xaKZHM"],
    encoding="utf-8"
).strip()
print(filename)

方法2:手动解码字节流

先获取字节形式的输出,再用UTF-8手动解码:

import subprocess
output_bytes = subprocess.check_output(
    ["yt-dlp", "--get-filename", "https://www.youtube.com/watch?v=XKuL5xaKZHM"]
)
filename = output_bytes.decode("utf-8").strip()
print(filename)

方法3:强制yt-dlp输出UTF-8编码

给yt-dlp添加--encoding utf-8参数,确保输出编码统一:

import subprocess
filename = subprocess.check_output(
    ["yt-dlp", "--encoding", "utf-8", "--get-filename", "https://www.youtube.com/watch?v=XKuL5xaKZHM"],
    encoding="utf-8"
).strip()
print(filename)

内容的提问来源于stack exchange,提问作者user3705497

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 12:04:55