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

调整StandardInput.WriteLine编码:Process调用ffmpeg转MP3遇土耳其字符问题

使用C# Process调用FFmpeg处理含土耳其字符的路径问题

刚好之前踩过非ASCII字符路径的坑,给你整理了能直接用的解决方案——核心就是解决编码不匹配的问题:Windows cmd默认用土耳其语OEM编码(cp857),而.NET默认的UTF-8传参过去肯定会导致ş、ö、ç、ğ这类字符乱码,FFmpeg自然找不到文件。

先理清楚问题根源

你在cmd里能正常运行是因为cmd自动适配了对应语言的编码,但用Process调用时,.NET的StreamWriter默认用UTF-8输出参数,导致带土耳其字符的路径被转成乱码,最终FFmpeg无法识别文件路径。

完整解决代码(带注释)

先纠正一个小细节:你写的ffmpeg.dll应该是笔误吧?要调用的是ffmpeg.exe(动态链接库不能直接通过Process启动),下面是标准FFmpeg可执行文件的调用代码:

using System;
using System.Diagnostics;
using System.Text;

class AudioConverter
{
    static void ConvertToMp3(string inputPath, string outputPath)
    {
        // 替换成你实际的ffmpeg.exe路径
        string ffmpegExePath = @"C:\Tools\ffmpeg\ffmpeg.exe";

        var process = new Process();
        process.StartInfo.FileName = ffmpegExePath;
        
        // 关键:设置编码为土耳其语OEM编码,和cmd的编码完全匹配
        process.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(857);
        process.StartInfo.StandardErrorEncoding = Encoding.GetEncoding(857);
        
        // 必须禁用ShellExecute才能指定编码和重定向输出
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.CreateNoWindow = true;

        // 用双引号包裹路径,避免空格/特殊字符被当成参数分隔符
        process.StartInfo.Arguments = $"-i \"{inputPath}\" -vn -ar 44100 -ac 2 -ab 320k -f mp3 -y \"{outputPath}\"";

        try
        {
            process.Start();
            // 读取输出/错误日志,方便调试排错
            string outputLog = process.StandardOutput.ReadToEnd();
            string errorLog = process.StandardError.ReadToEnd();
            process.WaitForExit();

            if (process.ExitCode != 0)
            {
                Console.WriteLine($"转换出错:{errorLog}");
            }
            else
            {
                Console.WriteLine("音频转MP3完成!");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"调用FFmpeg异常:{ex.Message}");
        }
        finally
        {
            process.Dispose();
        }
    }

    static void Main()
    {
        // 测试带土耳其字符的路径
        string inputFilePath = @"C:\Müzik\giriş.wav";
        string outputFilePath = @"C:\Çıktı\çıkış.mp3";
        ConvertToMp3(inputFilePath, outputFilePath);
    }
}

必注意的核心要点

  • 编码必须匹配:一定要设置StandardOutputEncoding和StandardErrorEncoding为Encoding.GetEncoding(857),这是解决土耳其字符乱码的关键。
  • 参数转义:用双引号包裹输入输出路径,避免路径中的空格或特殊字符被Process误判为参数分隔符。
  • 禁用ShellExecute:只有设为false,自定义编码和重定向输出的设置才会生效,否则编码配置会被忽略。
  • FFmpeg路径正确性:确保调用的是ffmpeg.exe,而非ffmpeg.dll——动态链接库无法直接通过Process启动。

备选方案:UTF-8兼容(仅适用于新Windows版本)

如果你的系统是Windows 10 1903及以上,且开启了UTF-8全局支持,也可以尝试用UTF-8编码,兼容性更广:

process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
process.StartInfo.StandardErrorEncoding = Encoding.UTF8;
// 可选:设置环境变量让FFmpeg优先用UTF-8解析路径
process.StartInfo.EnvironmentVariables["LC_ALL"] = "en_US.UTF-8";

不过这种方式在旧Windows系统上可能出问题,还是用OEM编码更稳妥。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:22:28