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

FFmpeg音视频合并正常但返回错误的问题排查求助

问题解决:FFmpeg合并成功却返回错误判定

核心问题

你的代码错误地将FFmpeg的标准错误流(stderr)内容等同于执行失败,但实际上FFmpeg会把绝大多数运行日志(包括流信息、处理进度、非致命警告)都输出到stderr,而非仅错误信息。这就导致即使合并成功,只要stderr有内容,MergeVideo就会返回false。

解决方案

1. 以FFmpeg退出码作为成功判定标准

FFmpeg执行成功时退出码为0,只有退出码非0时才代表真正的执行失败。修改MergeVideo中的判定逻辑,优先检查进程退出码。

2. 移除冗余参数

新版本FFmpeg中-strict experimental参数已无必要(AAC编码已成为标准支持),可以从FFmpeg命令参数中移除。

修改后的MergeVideo代码

private static async Task<bool> MergeVideo(string videoPath, string audioPath, string outputPath)
{
    try
    {
        Console.WriteLine("Starting MergeVideo method.");

        var ffmpeg = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = @"C:\Users\romap\source\repos\VideoDownloader\Ffmpeg\ffmpeg.exe",
                // 移除-strict experimental参数
                Arguments = $"-i \"{videoPath}\" -i \"{audioPath}\" -c:v copy -c:a aac \"{outputPath}\"",
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            }
        };

        ffmpeg.Start();

        var outputTask = ffmpeg.StandardOutput.ReadToEndAsync();
        var errorTask = ffmpeg.StandardError.ReadToEndAsync();

        await ffmpeg.WaitForExitAsync();

        string output = await outputTask;
        string error = await errorTask;

        Console.WriteLine("FFmpeg process completed.");

        // 优先检查退出码,0代表成功
        if (ffmpeg.ExitCode != 0)
        {
            Console.WriteLine($"FFmpeg执行失败,错误信息: {error}");
            return false;
        }

        // 可选:打印FFmpeg运行日志(非错误)
        if (!string.IsNullOrEmpty(error))
        {
            Console.WriteLine($"FFmpeg运行日志: {error}");
        }

        return true;
    }
    catch (Exception ex)
    {
        Console.WriteLine($"MergeVideo方法异常: {ex.Message}");
        return false;
    }
}

额外排查建议

  • 若使用旧版FFmpeg,建议升级到最新稳定版,减少不必要的警告输出。
  • 只有当合并后的视频存在音画不同步、文件损坏等实际问题时,再针对性分析stderr中的警告信息;若视频正常,仅需关注退出码即可。

内容的提问来源于stack exchange,提问作者Роман Парсанов

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 16:49:51