C++下ffmpeg踩坑记录

汉字问题

[AVFilterGraph @ 00000203fcb199c0] Error parsing filterchain '[a0][a1][a2][a3]amix=inputs=4:duration=longest:dropout_transition=2[afinal] -map [vfinal] -map [afinal] -c:v libx264 -pix_fmt yuv420p -r 30 -t 4 C:\Users\Eddy\Desktop\Video_20260409_102222.mp4' around: -map [vfinal] -map [afinal] -c:v libx264 -pix_fmt yuv420p -r 30 -t 4 C:\Users\Eddy\Desktop\Video_20260409_102222.mp4
Error : Invalid argument

这个错误,竟然是由于字幕是汉字的问题,如果换成英文就ok了。

windows下ffmpeg drawtext中的文件路径

在 FFmpeg 中,: 是参数分隔符,所以需要用 \: 把它转义为普通字符;而在 Windows 的 cmd / PowerShell 中,反斜线本身又会被 shell 解析掉一层,因此必须写成 \\:,这样经过 shell 处理后,FFmpeg 实际接收到的是 \:,从而正确把 C:/... 识别为完整路径,而不是被错误拆分成多个参数。

filter << "[vcollage]drawtext="
<< "fontfile=\"C\\\\:/Windows/Fonts/msyh.ttc\":" // <-- 改这里!双引号转义
<< "text=\"" << escapeFFmpegText("AA") << "\":" // <-- 改这里!
<< "x=250:y=250:"
<< "fontsize=20:fontcolor=white:"
<< "borderw=4:"
<< "bordercolor=blue:"
<< "line_spacing=10"
<< "[vtxt];";

如果fontFile单独写为字符串,那么"C\\:/Windows/",C盘符后面两道斜线是对的。

中文字幕乱码

这是因为用了createProcessA来创阿金线程的原因,要用createProcessW;

用了旧版 API CreateProcessA,它不支持 UTF-8!换成 CreateProcessW + wchar_t = 立刻解决

wxString wcmd = wxString::FromUTF8(cmd);
wchar_t *cmdline = _wcsdup(wcmd.wc_str());

BOOL ok = CreateProcessW(
NULL,
cmdline,
NULL,
NULL,
TRUE,
CREATE_NO_WINDOW,
NULL,
NULL,
&si,
&pi
);

读取config文本要这样,不然会在ffmpeg中乱码

config->Read("/Caption/CaptionText", &str);
captionText = static_cast<std::string>(str.utf8_string());

乱码还会导致ui卡死,软件直接闪退。