Reputation: 11329
In jumps of 1 steps 1%...2%...3%...4%...5% What happen now is few things. First the code is working fine the backgroundworker is start calling the Convert method and it's getting to the backgroundworker completed event. Not sure if the completed event is needed at all.
You can see in button1 click event i set this labels first time with the number of frames. and progressBar1 maximum.
The problems are:
On label12 i see 290 as maximum of progressBar1 but in fact there are only 280 frames, On label14 it's showing that there are 280 frames count.
In the end of the process i see on label12 the value 281 but there are 280 frames why it's counting to 281 ?
The percentages on label11 not moving at all it's staying on 0% all the time.
On form1 load event:
private void Form1_Load(object sender, EventArgs e)
{
RichTextBox1.Visible = false;
ProgressBar1.Value = 0;
Button5.Enabled = false;
KB = 1024;
MB = KB * 1024;
GB = MB * 1024;
psiProcInfo = new ProcessStartInfo();
prcFFMPEG = new Process();
}
On button1 click event
private void Button1_Click(object sender, EventArgs e)
{
OFD = new OpenFileDialog();
if (OFD.ShowDialog() == DialogResult.OK)
{
mFile = new MediaFile(OFD.FileName);
InputFile = OFD.FileName;
pathResult = Path.GetFileNameWithoutExtension(OFD.FileName);
Label3.Text = "Name: " + pathResult;
if (mFile.FileSize > 1070000000)
{
Label4.Text = "FileSize: " + String.Format("{0:0.00}", mFile.FileSize / KB / MB) + " GB.";
}
else
{
Label4.Text = "FileSize: " + String.Format("{0:0.00}", mFile.FileSize / MB) + " MB.";
}
Label5.Text = "Duration: " + mFile.Video[0].DurationString;
Label6.Text = "Format: " + mFile.Video[0].Format;
Label7.Text = "Codec: " + mFile.Video[0].CodecID;
Label8.Text = "Resolution: " + mFile.Video[0].FrameSize;
Label9.Text = "Framerate: " + mFile.Video[0].FrameRate;
Label14.Text = "NumberOfFrames: " + mFile.Video[0].SourceFile.FrameCount.ToString();
Label15.Text = "VideoStreams: " + mFile.Video[0].StreamType + " " + mFile.Video[0].StreamIndex;
FCount = int.Parse(mFile.Video[0].SourceFile.FrameCount.ToString());
ProgressBar1.Maximum = FCount + 10;
Label12.Text = ProgressBar1.Maximum.ToString();
}
}
The Convert method that in there i'm doing the calculations of the reporting the progress to the progressBar1 and labels.
private void Convert()
{
Control.CheckForIllegalCrossThreadCalls = false;
if (ComboBox1.SelectedIndex == 3)
{
strFFCMD = " -i \"" + InputFile + "\" \"" + OutputFile + "\"";
}
if (ComboBox1.SelectedIndex == 2)
{
strFFCMD = " -i " + (char)34 + InputFile + (char)34 +
" -c:v libx264 -s 1280x720 -pix_fmt yuv420p -qp 20 -profile high444-c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 " + OutputFile;
}
psiProcInfo.FileName = exepath;
psiProcInfo.Arguments = strFFCMD;
psiProcInfo.UseShellExecute = false;
psiProcInfo.WindowStyle = ProcessWindowStyle.Hidden;
psiProcInfo.RedirectStandardError = true;
psiProcInfo.RedirectStandardOutput = true;
psiProcInfo.CreateNoWindow = true;
prcFFMPEG.StartInfo = psiProcInfo;
prcFFMPEG.Start();
ffReader = prcFFMPEG.StandardError;
do
{
if (Bgw1.CancellationPending)
{
return;
}
Button5.Enabled = true;
Button3.Enabled = false;
strFFOUT = ffReader.ReadLine();
//Show each read line in the richtextbox
RichTextBox1.Text = strFFOUT;
if (strFFOUT != null)
{
if (strFFOUT.Contains("frame="))
{
currentFramestr = strFFOUT.Substring(7, 6).Trim();
Regex rx = new Regex(@"^\d+");
Match m = rx.Match(currentFramestr);
if (m.Success)
{
currentFrameInt = System.Convert.ToInt32(m.Value);
}
}
}
string percentage = System.Convert.ToInt32((ProgressBar1.Value / ProgressBar1.Maximum * 100)).ToString() + "%";
ProgressBar1.Maximum = FCount + 1000;
ProgressBar1.Value = (currentFrameInt);
Label12.Text = "Current Encoded Frame: " + currentFrameInt;
Label11.Text = percentage;
} while (!(prcFFMPEG.HasExited || string.IsNullOrEmpty(strFFOUT)));
}
Then the backgroundworker dowork event:
private void Bgw1_DoWork(object sender, DoWorkEventArgs e)
{
Convert();
}
I'm not using the backgroundworker progresschanged event and completed event. Just used a break point on the completed event and saw it's getting there in the end.
Now some values i get using a break point inside the Convert method. I added a breakpoint on the line:
if (m.Success)
{
currentFrameInt = System.Convert.ToInt32(m.Value);
}
First time the value in m.Value is "9" Then "17"
And it finish stop getting to the completed event on "17" Now i'm not using a break point at all.
The first value is "9" in the end it's 281 i could see that near the end it jumped from 269 to 281.
This is a screenshot of the form1 when the process completed using a break point:
Current Encoded Frame : 17 thats Label12
NumberOfFrames: 280 thats Label14
The 0% on the right of the progressBar1 that never change and stay on 0% all the time is Label11
And this is a screenshot of form1 without using any break point:
In top of form1:
private Process prcFFMPEG;
private ProcessStartInfo psiProcInfo;
private string strFFCMD;
private StreamReader ffReader;
private string strFFOUT;
private string currentFramestr;
private int currentFrameInt;
Upvotes: 0
Views: 120
Reputation: 1893
label12 = ProgressBar1.Maximum = FCount + 10 = Framecount + 10 = 280 + 10 = 290
label12
from ffReader = prcFFMPEG.StandardError;
. It is hard to say anything without knowing this process prcFFMPEG
. But I guess it is returning the error value frame=281
at the end. But there could also be something wrong with your Trim
or your RegEx
.ProgressBar1
values to double
:
label11 = percentage = ... = ((double)ProgressBar1.Value / (double)ProgressBar1.Maximum * 100.0)
Upvotes: 2