Sharon Gabriel Sharon Gabriel - 5 months ago 14
Vb.net Question

What part from the block of code in vb when converting it to c# should i put in the loop?

This is the code in vb:

Loop until done

Loop Until prcFFMPEG.HasExited And strFFOUT = Nothing Or strFFOUT = ""

If ProgressBar1.Value <> ProgressBar1.Maximum Then
ProgressBar1.Value = ProgressBar1.Maximum 'make the progressbar hit its maximum
'on completion
End If

MsgBox("The Conversion Process has Completed.", _
MsgBoxStyle.Information, "Finished.") 'display on completion

Button5.Enabled = False 'Disable the cancel button
Button3.Enabled = True 'Reenable the convert button
ProgressBar1.Value = 0 'reset the progressbar value to 0
Label11.Text = _ProgressBar1.Value & "%" 'get the progressbar value
'and report it as percent for visual purposes

Label12.Text = "Current Encoded Frame: "
If Me.Height = 508 Then 'if the show detailed information button
'was pressed reset the form height back to normal
Me.Height = 441
RichTextBox1.Visible = False 'Disable the richtextbox
End If


So i converted the loop to c# like this:
If the convertion to c# is right then what part of the code i should put in the loop ?

while(prcFFMPEG.HasExited && strFFOUT == "")
{

}


Only the two lines( i will convert the lines later to c# )

If ProgressBar1.Value <> ProgressBar1.Maximum Then
ProgressBar1.Value = ProgressBar1.Maximum


Or all the code to put in the loop ?

This is the original method in vb where the loop is in:

Private Sub Convert() ' function Convert() here is the actuall conversion process

Control.CheckForIllegalCrossThreadCalls = False 'Check for illegal cross threads
''''''''''''''''
'set presets here 'Note Combobox indexes start at 0 not 1
'mobile Devices
If ComboBox1.SelectedIndex = 3 Then ' 480x320 option

strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 480x320 -pix_fmt yuv420p -qp 23 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 " & OutputFile
End If
If ComboBox1.SelectedIndex = 4 Then ' 640x480 option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 640x480 -pix_fmt yuv420p -qp 22 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 " & OutputFile
End If
If ComboBox1.SelectedIndex = 5 Then ' 800x600 option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 800x600 -pix_fmt yuv420p -qp 21 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 " & OutputFile
End If
If ComboBox1.SelectedIndex = 6 Then ' 1024x768 option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1024x768 -pix_fmt yuv420p -qp 20 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 " & OutputFile
End If
If ComboBox1.SelectedIndex = 7 Then ' 1280x720 option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1280x720 -pix_fmt yuv420p -qp 19 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
End If
If ComboBox1.SelectedIndex = 8 Then ' 1920x1080 option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1920x1080 -pix_fmt yuv420p -qp 18 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
End If
''''''''''''''
If ComboBox1.SelectedIndex = 12 Then ' Ps3 720p option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1280x720 -pix_fmt yuv420p -qp 18 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
End If
If ComboBox1.SelectedIndex = 13 Then ' ps3 1080p option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1920x1080 -pix_fmt yuv420p -qp 18 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
End If
''''''''''''''
If ComboBox1.SelectedIndex = 17 Then ' TV Standard Def 720x576p PAL AVI option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v mpeg4 -s 720x576 -pix_fmt yuv420p -b:v 1500k -r 30 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
End If
If ComboBox1.SelectedIndex = 18 Then ' TV Standard Def 720x576p PAL MP4 option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 720x576 -pix_fmt yuv420p -qp 19 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
End If
''''''''''''''
If ComboBox1.SelectedIndex = 19 Then ' TV High Def 1280x720p PAL AVI option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v mpeg4 -s 1280x720 -pix_fmt yuv420p -b:v 2500k -r 30 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
End If
If ComboBox1.SelectedIndex = 20 Then ' TV High Def 1280x720p PAL MP4 option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1280x720 -pix_fmt yuv420p -qp 18 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
End If
''''''''''''''
If ComboBox1.SelectedIndex = 21 Then ' TV High Def 1920x1080p PAL AVI option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v mpeg4 -s 1920x1080 -pix_fmt yuv420p -b:v 3000k -r 30 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
End If
If ComboBox1.SelectedIndex = 22 Then ' TV High Def 1920x1080p PAL MP4 option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1920x1080 -pix_fmt yuv420p -qp 18 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
End If

'''''''''''''''''''

If ComboBox1.SelectedIndex = 26 Then ' MKV 1 option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1280x720 -pix_fmt yuv420p -b:v 3000k -r 30 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
End If
If ComboBox1.SelectedIndex = 27 Then ' MKV 2 option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 1920x1080 -pix_fmt yuv420p -b:v 4000k -r 30 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
End If
If ComboBox1.SelectedIndex = 28 Then 'SD flv option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 480x320 -pix_fmt yuv420p -b:v 1000k -r 30 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
End If

''''''''''''''''
If ComboBox1.SelectedIndex = 30 Then 'low qual flv option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 320x240 -pix_fmt yuv420p -b:v 800k -r 30 -profile high444 -c:a libvo_aacenc -b:a 96k -ar 44100 -ac 2 -y " & OutputFile
End If
If ComboBox1.SelectedIndex = 31 Then 'medium qual flv option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 480x320 -pix_fmt yuv420p -b:v 1000k -r 30 -profile high444 -c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
End If
If ComboBox1.SelectedIndex = 32 Then 'high qual flv option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -s 720x576 -pix_fmt yuv420p -b:v 1500k -r 30 -profile high444 -c:a libvo_aacenc -b:a 192k -ar 44100 -ac 2 -y " & OutputFile
End If

''''''''''''''''
If ComboBox1.SelectedIndex = 34 Then '320kbs mp3 only option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -vn -c:a libmp3lame -b:a 320k -ar 44100 -ac 2 -y " & OutputFile
End If
If ComboBox1.SelectedIndex = 35 Then '192kbs mp3 only option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -vn -c:a libmp3lame -b:a 192k -ar 44100 -ac 2 -y " & OutputFile
End If
If ComboBox1.SelectedIndex = 36 Then '128kbs mp3 only option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -vn -c:a libmp3lame -b:a 128k -ar 44100 -ac 2 -y " & OutputFile
End If

If ComboBox1.SelectedIndex = 38 Then 'Lossless Copy option
strFFCMD = " -i " & Chr(34) & InputFile & Chr(34) & " -c:v libx264 -qp 0 -c:a libvo_aacenc -b:a 320k -ar 44100 -ac 2 -y " & OutputFile
End If



'''''''''''''''''
'start Main Process
psiProcInfo.FileName = Application.StartupPath + "\ffmpeg.exe" 'Location Of FFMPEG.EXE
psiProcInfo.Arguments = strFFCMD 'start ffmpeg with command strFFCMD string
psiProcInfo.UseShellExecute = False 'use the shell execute command we always want no
psiProcInfo.WindowStyle = ProcessWindowStyle.Hidden 'hide the ffmpeg process window
psiProcInfo.RedirectStandardError = True 'Redirect the error out so we can read it
psiProcInfo.RedirectStandardOutput = True 'Redirect the standard out so we can read it
psiProcInfo.CreateNoWindow = True 'We dont create the ffmpeg window
prcFFMPEG.StartInfo = psiProcInfo 'ffmpeg process start information = all above informaton
prcFFMPEG.Start() 'Start Process

ffReader = prcFFMPEG.StandardError 'Enable Error Checking For FFMPEG.EXE


Do '' Bgw1 = Backgroundworker1
If Bgw1.CancellationPending Then ' Have we pressed Cancel?
Exit Sub

End If

Button5.Enabled = True 'Enable the cancel button
Button3.Enabled = False 'Disable the convert button

strFFOUT = ffReader.ReadLine ' read each line from ffreader
RichTextBox1.Text = strFFOUT 'Show each read line in the richtextbox

'Get the progress from the current encoded frame
If strFFOUT.Contains("frame=") Then 'if the strFFOut contains the string
currentFramestr = Mid(strFFOUT, 7, 6) 'grab the next part after the string 'frame='
currentFrameInt = CInt(currentFramestr) 'the next part is a string so convert the numbers back to an integer
End If

'Calculate the percentage of the progressbar
Dim percentage As String = CType((ProgressBar1.Value / ProgressBar1.Maximum * 100), Integer).ToString & "%"

' report progress
ProgressBar1.Maximum = FCount + 1000 'MediaInfo does not always get a accurate frame count so always add 1000 on top
ProgressBar1.Value = (currentFrameInt) 'the current value is the current encoded frame
Label12.Text = "Current Encoded Frame: " & currentFrameInt 'show the current frame in a label
Label11.Text = percentage 'show the perecentage in a label
'Label12.Text = ProgressBar1.Maximum 'for testing purposes


' Loop untill done
Loop Until prcFFMPEG.HasExited And strFFOUT = Nothing Or strFFOUT = ""

If ProgressBar1.Value <> ProgressBar1.Maximum Then
ProgressBar1.Value = ProgressBar1.Maximum 'make the progressbar get to it's maximum on completion
End If


MsgBox("The Conversion Process has Completed.", MsgBoxStyle.Information, "Finished.") 'display on completion
Button5.Enabled = False 'Disable the cancel button
Button3.Enabled = True 'Reenable the convert button
ProgressBar1.Value = 0 'reset the progressbar value to 0
Label11.Text = ProgressBar1.Value & "%" 'get the progressbar value and report it as percent for visual purposes
Label12.Text = "Current Encoded Frame: "
If Me.Height = 508 Then 'if the show detailed information button was pressed reset the form height back to normal
Me.Height = 441
RichTextBox1.Visible = False 'Disable the richtextbox
End If

End Sub


In the bottom you can see the line:

Loop Until prcFFMPEG.HasExited And strFFOUT = Nothing Or strFFOUT = ""


But it's not clear where the loop start and end.

Answer

This is your loop in C#. in VB it starts with the Do and ends with the Loop Until [Condition].

do { //' Bgw1 = Backgroundworker1
    // Have we pressed Cancel?
    if (Bgw1.CancellationPending) {
        return;
    }

    //Enable the cancel button
    Button5.Enabled = true;
    //Disable the convert button
    Button3.Enabled = false;

    // read each line from ffreader
    strFFOUT = ffReader.ReadLine();
    //Show each read line in the richtextbox
    RichTextBox1.Text = strFFOUT;

    //Get the progress from the current encoded frame
    //if the strFFOut contains the string
    if (strFFOUT.Contains("frame=")) {
        //grab the next part after the string 'frame='
        currentFramestr = String.Mid(strFFOUT, 7, 6);
        //the next part is a string so convert the numbers back to an integer
        currentFrameInt = Convert.ToInt32(currentFramestr);
    }

    //Calculate the percentage of the progressbar
    string percentage = Convert.ToInt32((ProgressBar1.Value / ProgressBar1.Maximum * 100)).ToString + "%";

    // report progress
    //MediaInfo does not always get a accurate frame count so always add 1000 on top
    ProgressBar1.Maximum = FCount + 1000;
    //the current value is the current encoded frame
    ProgressBar1.Value = (currentFrameInt);
    //show the current frame in a label
    Label12.Text = "Current Encoded Frame: " + currentFrameInt;
    //show the perecentage in a label
    Label11.Text = percentage;
    //Label12.Text = ProgressBar1.Maximum 'for testing purposes

    // Loop until done
} while (!(prcFFMPEG.HasExited || string.IsNullOrEmpty(strFFOUT)));