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

.NET调用ffmpeg编码webm无响应及整合实时文本输出问题求助

问题原因

1. 未设置进程工作目录,FFmpeg找不到输入文件

你代码里指定输入文件是1.mp4,没有写绝对路径的情况下,FFmpeg会从进程的工作目录查找该文件,而你没有手动设置WorkingDirectory属性,默认工作目录是你的VB程序的启动目录,不是1.mp4存放的目录,FFmpeg找不到输入文件会直接报错退出。

2. 未捕获FFmpeg的标准错误输出,无法看到报错信息

FFmpeg默认所有运行日志、报错信息全部输出到StandardError流,你只绑定了OutputDataReceived事件处理标准输出,完全没有处理标准错误流,所以看不到任何FFmpeg的报错提示,也没办法定位问题。

3. 没有对进程退出状态做校验

你直接在进程退出后提示完成,没有判断P.ExitCode的返回值,FFmpeg执行出错时ExitCode会返回非0值,正常执行完成才会返回0。


修复方案

你可以按照以下步骤修改代码:

  • 给进程对象设置WorkingDirectory属性,指定为你存放1.mp4的目录
  • 新增ErrorDataReceived事件绑定,捕获FFmpeg的运行日志和报错
  • 进程退出后先判断退出码确认是否执行成功
  • 如果参数里的输入/输出文件路径带空格,需要用英文双引号包裹路径

修复后的核心代码示例

Dim currentMesssage As String
Dim WithEvents P As Process
Dim WithEvents UpdateTimer As New Timer
Dim PThread As Threading.Thread

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If boxfilename.Text <> "" AndAlso IO.File.Exists(pathTodownloadto & boxfilename.Text & ".webm") = False Then
        P = New Process
        P.StartInfo.UseShellExecute = False
        P.StartInfo.RedirectStandardOutput = True
        P.StartInfo.RedirectStandardError = True
        P.StartInfo.CreateNoWindow = True
        ' 新增:设置工作目录为你存放1.mp4的目录
        P.StartInfo.WorkingDirectory = "C:\你存放1.mp4的目录路径"
        P.StartInfo.FileName = "C:\Users\myname\Downloads\video stuff\ffmpeg.exe"
        ' 路径如果有空格建议加双引号包裹
        P.StartInfo.Arguments = "-i ""1.mp4"" -c:v libvpx -b:v 1M -c:a libvorbis ""NewVideo.webm"""
        PThread = New Threading.Thread(AddressOf RunProcess)

        UpdateTimer.Start()
        PThread.Start()
    Else
        MsgBox("Filename is either empty or file already exists.", MsgBoxStyle.Exclamation)
    End If
End Sub

Sub RunProcess()
    P.Start()
    P.BeginOutputReadLine()
    P.BeginErrorReadLine()
    P.WaitForExit()
    ' 新增:判断退出码确认执行结果
    If P.ExitCode = 0 Then
        MsgBox("编码完成。", MsgBoxStyle.Information)
    Else
        MsgBox($"编码出错,错误码:{P.ExitCode}", MsgBoxStyle.Critical)
    End If
End Sub

Sub DataReceived(sender As Object, e As DataReceivedEventArgs) Handles P.OutputDataReceived, P.ErrorDataReceived
    If Not String.IsNullOrEmpty(e.Data) Then
        currentMesssage = e.Data
    End If
End Sub

Sub TimerTick(sender As Object, e As EventArgs) Handles UpdateTimer.Tick
    lblProgress.Text = currentMesssage
    lblProgress.Update()
End Sub

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 02:06:04