08-10-25, 12:47 PM
PHP كود :
Imports System.Text.RegularExpressions
Public Class Form1
Dim ffprobe As String = "ffprobe.exe" ' Path to ffprobe.exe
Dim ffmpeg As String = "ffmpeg.exe" ' Path to ffmpeg.exe
Dim inputFile As String = "input.mp4" ' Path to input file
Dim outputFile As String = "output.mp4" ' Path to output file
Dim totalDurationSeconds As Double ' total duration seconds
Private Sub ButtonStart_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonStart.Click
If IO.File.Exists(outputFile) Then
MsgBox(String.Format(" The file '{0}' exists.", outputFile))
Exit Sub
End If
ProgressBar1.Value = 0
LabelProgress.Text = "0%"
Dim processingThread As New System.Threading.Thread(AddressOf StartFFmpegProcessing)
processingThread.Start()
' ButtonStart disable
ButtonStart.Enabled = False
End Sub
Private Sub StartFFmpegProcessing()
' Get total duration seconds
GetTotalDurationSeconds(inputFile)
' ffmpeg Process
Dim ffmpegProcess As New Process()
Dim startInfo As New ProcessStartInfo()
startInfo.FileName = ffmpeg ' Path to ffmpeg.exe
startInfo.Arguments = "-i """ & inputFile & """ -progress pipe:1 -vf scale=640:-1 """ & outputFile & """" ' Example arguments, including -progress pipe:1 for machine-readable output
startInfo.UseShellExecute = False
startInfo.RedirectStandardOutput = True
startInfo.CreateNoWindow = True
ffmpegProcess.StartInfo = startInfo
ffmpegProcess.EnableRaisingEvents = True
AddHandler ffmpegProcess.OutputDataReceived, AddressOf FFmpegOutputHandler
ffmpegProcess.Start()
ffmpegProcess.BeginOutputReadLine() ' Start asynchronous reading of output
' Wait for FFmpeg to exit (optional, can be handled in a separate thread)
ffmpegProcess.WaitForExit()
' Clean up event handler
RemoveHandler ffmpegProcess.OutputDataReceived, AddressOf FFmpegOutputHandler
' ButtonStart enable
ButtonStart.Invoke(Sub() ButtonStart.Enabled = True)
End Sub
Private Sub FFmpegOutputHandler(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
If Not String.IsNullOrEmpty(e.Data) Then
' Parse FFmpeg output to extract progress information
' For example, look for "out_time_ms=" or "time=" to get current time
' Example: Assuming totalDurationSeconds is available (e.g., from a class-level variable)
Dim currentTimeMatch As Match = Regex.Match(e.Data, "out_time_ms=(\d+)")
If currentTimeMatch.Success Then
Dim currentTimeMilliseconds As Long = Long.Parse(currentTimeMatch.Groups(1).Value)
Dim currentTimeSeconds As Double = currentTimeMilliseconds / 1000.0
If totalDurationSeconds > 0 Then
Dim progressPercentage As Integer = CInt((currentTimeSeconds / totalDurationSeconds) * 100)
If progressPercentage > 100 Then progressPercentage = 100
If progressPercentage < 0 Then progressPercentage = 0
' Update ProgressBar on the UI thread
Me.Invoke(Sub()
ProgressBar1.Value = progressPercentage
LabelProgress.Text = progressPercentage.ToString() & "%"
End Sub)
End If
End If
End If
End Sub
Private Sub GetTotalDurationSeconds(ByVal inputFile As String)
' Get total duration seconds (from ffprobe)
Dim proc As New Process()
Dim startInfo As New ProcessStartInfo()
startInfo.FileName = ffprobe ' Path to ffprobe.exe
startInfo.Arguments = "-v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 """ & inputFile & """"
startInfo.RedirectStandardInput = True
startInfo.RedirectStandardOutput = True
startInfo.CreateNoWindow = True
startInfo.UseShellExecute = False
proc.StartInfo = startInfo
proc.Start()
If Not IsNothing(proc) Then totalDurationSeconds = Val(proc.StandardOutput.ReadToEnd) * 1000.0
proc.WaitForExit()
End Sub
End Class
