如何与流程输出交互?

问题描述 投票:0回答:2

好吧,目前我有一个程序使用 VB.net 中的进程运行 FFmpeg。我在启动信息中发送进程参数以及其他内容,例如文件位置。当我运行代码时,它将控制台输出发送到调试控制台;这可能是因为我有 .UseShellExecute = False 和 processInfo.RedirectStandardOutput = True

我的问题是:如何制作可以解释输出的东西?此外,对于 FFmpeg,该过程是连续的,因此该过程的大部分时间始终在运行,并不断在调试控制台中添加更多输出行。

我正在使用的代码:

Dim process As New Process
        Dim processInfo As New ProcessStartInfo
        processInfo.FileName = tempPath
        processInfo.Arguments = ("-r 1/.1 -i " + link + " -c copy " + saveLocation + "\" + streamerName + ".ts")
        processInfo.UseShellExecute = False
        processInfo.WindowStyle = ProcessWindowStyle.Hidden
        processInfo.CreateNoWindow = True
        processInfo.RedirectStandardOutput = True
        process.StartInfo = processInfo
        process.Start()

我试过了,但没有成功。

Dim output As String
        Using StreamReader As System.IO.StreamReader = process.StandardOutput
            output = StreamReader.ReadToEnd().ToString
        End Using

编辑:我现在有这个代码:

Dim process As New Process
        AddHandler process.OutputDataReceived, AddressOf CallbackProcesoAsync
        AddHandler process.ErrorDataReceived, AddressOf ErrorDataReceivedAsync
        Dim processInfo As New ProcessStartInfo
        processInfo.FileName = tempPath
        processInfo.Arguments = ("-r 1/.1 -i " + link + " -c copy " + saveLocation + "\" + streamerName + ".ts")
        processInfo.UseShellExecute = False
        processInfo.WindowStyle = ProcessWindowStyle.Hidden
        processInfo.CreateNoWindow = False
        processInfo.RedirectStandardOutput = True
        processInfo.RedirectStandardError = True
        process.StartInfo = processInfo
        process.Start()
        processes.Add(Tuple.Create(tempPath, streamerName))
        Debug.WriteLine("Attempting to record " + streamerName)
        Dim output As String
        Using StreamReader As System.IO.StreamReader = process.StandardOutput
            output = StreamReader.ReadToEnd().ToString
        End Using
    End If
End Sub

Private Sub CallbackProcesoAsync(sender As Object, args As System.Diagnostics.DataReceivedEventArgs)
    If Not args.Data Is Nothing AndAlso Not String.IsNullOrEmpty(args.Data) Then
        RichTextBox1.Text = args.Data
    End If
End Sub

Private Sub ErrorDataReceivedAsync(sender As Object, args As System.Diagnostics.DataReceivedEventArgs)
    If Not args.Data Is Nothing AndAlso Not String.IsNullOrEmpty(args.Data) Then
        RichTextBox2.Text = args.Data
    End If
End Sub

但是我还没有收到任何富文本框的输出?

我感觉和streamReader有关系所以我把它去掉了还是不行?我不知道它会是什么。

vb.net shell process ffmpeg output
2个回答
0
投票

Ffmpeg 将所有调试信息输出到 StdErr 而不是 StdOut。使用 RedirectStandardError 为 true 并尝试从进程中读取 StandardError。


0
投票

我知道这是一个老问题,但我出于同样的原因来到这里。 这对我有用。请注意,我只想将 ffmpeg 输出的最后一行捕获到文本框。我用它来将 .ts 文件转换为 .mp4。它非常快,因为它在编码时复制到新文件。非常快。 6GB 文件只需几分钟...

这是一个实际例子: https://www.youtube.com/watch?v=45iBOx8eFcs

大家加油

Using process As New Process()

argConvert = "-i " &
    """D:\" & VODFileName & ".ts""" &
    " -c:v copy -c:a copy  " &
    """D:\" & VODFileName & ".mp4"""

process.StartInfo.FileName = "ffmpeg.exe"
process.StartInfo.Arguments = argConvert
process.StartInfo.UseShellExecute = False
process.StartInfo.RedirectStandardError = True
process.StartInfo.CreateNoWindow = True

process.Start()

Dim reader As StreamReader = process.StandardError
Dim output As String = ""

While Not process.StandardError.EndOfStream
    output = process.StandardError.ReadLine()
    Label1.Text = output
    Application.DoEvents()
End While

Label1.Text = "Done"
Label1.Refresh()

结束使用

© www.soinside.com 2019 - 2024. All rights reserved.