Skip to content

Commit 923c58f

Browse files
committed
Added TimeLeftCalculator, renamed FFmpegProgress as FFmpegStatus
1 parent df46b54 commit 923c58f

File tree

12 files changed

+129
-67
lines changed

12 files changed

+129
-67
lines changed

ExampleApplication/FFmpegWindow.xaml.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static FFmpegWindow Instance(Window parent, string title, bool autoClose)
2121
private FFmpegProcess task;
2222
private bool autoClose;
2323
private string title { get; set; }
24+
private TimeLeftCalculator timeCalc;
2425

2526
public void Stop() {
2627
Dispatcher.Invoke(() => this.Close());
@@ -39,7 +40,7 @@ public void DisplayTask(FFmpegProcess taskArg) {
3940
if (taskArg.Options.IsMainTask) {
4041
host = taskArg;
4142
host.InfoUpdated += FFmpeg_InfoUpdated;
42-
host.ProgressUpdated += FFmpeg_ProgressUpdated;
43+
host.StatusUpdated += FFmpeg_StatusUpdated;
4344
host.Completed += FFmpeg_Completed;
4445
PercentText.Text = 0.ToString("p1");
4546
SetPageTitle(PercentText.Text);
@@ -66,21 +67,27 @@ private void SetPageTitle(string status) {
6667
}
6768

6869
private void FFmpeg_InfoUpdated(object sender, EventArgs e) {
69-
Dispatcher.Invoke(() => WorkProgressBar.Maximum = host.FrameCount + host.Options.ResumePos);
70+
Dispatcher.Invoke(() => {
71+
WorkProgressBar.Maximum = host.FrameCount + host.Options.ResumePos;
72+
timeCalc = new TimeLeftCalculator(host.FrameCount + host.Options.ResumePos);
73+
});
7074
}
7175

7276
private bool EstimatedTimeLeftToggle = false;
73-
private void FFmpeg_ProgressUpdated(object sender, FFmpeg.ProgressUpdatedEventArgs e) {
77+
private void FFmpeg_StatusUpdated(object sender, FFmpeg.StatusUpdatedEventArgs e) {
7478
Dispatcher.Invoke(() => {
75-
WorkProgressBar.Value = e.Progress.Frame + host.Options.ResumePos;
79+
WorkProgressBar.Value = e.Status.Frame + host.Options.ResumePos;
7680
PercentText.Text = (WorkProgressBar.Value / WorkProgressBar.Maximum).ToString("p1");
7781
SetPageTitle(PercentText.Text);
78-
FpsText.Text = e.Progress.Fps.ToString();
82+
FpsText.Text = e.Status.Fps.ToString();
7983

8084
// Time left will be updated only 1 out of 2 to prevent changing too quick.
8185
EstimatedTimeLeftToggle = !EstimatedTimeLeftToggle;
82-
if (e.Progress.EstimatedTimeLeft != TimeSpan.Zero && EstimatedTimeLeftToggle)
83-
TimeLeftText.Text = e.Progress.EstimatedTimeLeft.ToString(e.Progress.EstimatedTimeLeft.TotalHours < 1 ? "m\\:ss" : "h\\:mm\\:ss");
86+
if (EstimatedTimeLeftToggle) {
87+
TimeSpan TimeLeft = timeCalc?.Calculate(e.Status.Frame + host.Options.ResumePos) ?? TimeSpan.Zero;
88+
if (TimeLeft > TimeSpan.Zero)
89+
TimeLeftText.Text = TimeLeft.ToString(TimeLeft.TotalHours < 1 ? "m\\:ss" : "h\\:mm\\:ss");
90+
}
8491
});
8592
}
8693

ExampleApplication/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@
5151
// You can specify all the values or you can default the Build and Revision Numbers
5252
// by using the '*' as shown below:
5353
// [assembly: AssemblyVersion("1.0.*")]
54-
[assembly: AssemblyVersion("1.0.0.0")]
55-
[assembly: AssemblyFileVersion("1.0.0.0")]
54+
[assembly: AssemblyVersion("1.0.1.0")]
55+
[assembly: AssemblyFileVersion("1.0.1.0")]

FFmpeg/EventHandlers.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33

44
namespace EmergenceGuardian.FFmpeg {
55
/// <summary>
6-
/// Represents the method that will handle the ProgressUpdated event.
6+
/// Represents the method that will handle the StatusUpdated event.
77
/// </summary>
8-
public delegate void ProgressUpdatedEventHandler(object sender, ProgressUpdatedEventArgs e);
8+
public delegate void StatusUpdatedEventHandler(object sender, StatusUpdatedEventArgs e);
99

1010
/// <summary>
11-
/// Provides progress information for the ProgressUpdated event.
11+
/// Provides progress information for the StatusUpdated event.
1212
/// </summary>
13-
public class ProgressUpdatedEventArgs : EventArgs {
14-
public FFmpegProgress Progress { get; set; }
13+
public class StatusUpdatedEventArgs : EventArgs {
14+
public FFmpegStatus Status { get; set; }
1515

16-
public ProgressUpdatedEventArgs() {
16+
public StatusUpdatedEventArgs() {
1717
}
1818

19-
public ProgressUpdatedEventArgs(FFmpegProgress progress) {
20-
Progress = progress;
19+
public StatusUpdatedEventArgs(FFmpegStatus progress) {
20+
Status = progress;
2121
}
2222
}
2323

FFmpeg/FFmpeg.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@
5151
<Compile Include="FFmpegConfig.cs" />
5252
<Compile Include="FFmpegDisplayMode.cs" />
5353
<Compile Include="FFmpegParser.cs" />
54+
<Compile Include="FFmpegStatus.cs" />
5455
<Compile Include="FFmpegStream.cs" />
5556
<Compile Include="FFmpegStreamInfo.cs" />
5657
<Compile Include="FFmpegProcess.cs" />
5758
<Compile Include="FFmpegStreamType.cs" />
5859
<Compile Include="MediaProcesses.cs" />
60+
<Compile Include="TimeLeftCalculator.cs" />
5961
<Compile Include="UserInterfaceManagerBase.cs" />
6062
<Compile Include="MediaInfo.cs" />
6163
<Compile Include="MediaEncoder.cs" />

FFmpeg/FFmpeg.nuspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
<package >
33
<metadata>
44
<id>EmergenceGuardian.FFmpeg</id>
5-
<version>1.0.0.0</version>
5+
<version>1.0.1.0</version>
66
<title>FFmpeg.NET</title>
77
<authors>Etienne Charland</authors>
88
<owners>Etienne Charland</owners>
99
<licenseUrl>https://github.com/mysteryx93/FFmpeg.NET/blob/master/LICENSE</licenseUrl>
1010
<projectUrl>https://github.com/mysteryx93/FFmpeg.NET</projectUrl>
1111
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1212
<description>.NET Wrapper for FFmpeg</description>
13-
<releaseNotes>Initial release.</releaseNotes>
13+
<releaseNotes>Sample and usage info available here https://github.com/mysteryx93/FFmpeg.NET</releaseNotes>
1414
<copyright>Copyright 2017, Emergence Guardian</copyright>
1515
<tags>ffmpeg ffmpeg-wrapper video-encoding video library .net</tags>
1616
</metadata>

FFmpeg/FFmpegParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ internal static FFmpegStreamInfo ParseStreamInfo(string item) {
159159
/// </summary>
160160
/// <param name="text">The raw output line from FFmpeg.</param>
161161
/// <returns>A FFmpegProgress object.</returns>
162-
internal static FFmpegProgress ParseProgress(string text) {
163-
FFmpegProgress Result = new FFmpegProgress();
162+
internal static FFmpegStatus ParseProgress(string text) {
163+
FFmpegStatus Result = new FFmpegStatus();
164164
// frame= 929 fps=0.0 q=-0.0 size= 68483kB time=00:00:37.00 bitrate=15162.6kbits/s speed= 74x
165165
string[] Values = text.Split('=');
166166
try {

FFmpeg/FFmpegProcess.cs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public class FFmpegProcess {
3131
/// </summary>
3232
public event EventHandler InfoUpdated;
3333
/// <summary>
34-
/// Occurs when progress update is received through FFmpeg's output stream.
34+
/// Occurs when status update is received through FFmpeg's output stream.
3535
/// </summary>
36-
public event ProgressUpdatedEventHandler ProgressUpdated;
36+
public event StatusUpdatedEventHandler StatusUpdated;
3737
/// <summary>
3838
/// Occurs when the process has terminated its work.
3939
/// </summary>
@@ -65,8 +65,6 @@ public string Output {
6565
private bool isFFmpeg;
6666
private bool isStarted;
6767
private CancellationTokenSource cancelWork;
68-
private List<KeyValuePair<DateTime, long>> progressHistory;
69-
private bool nestedProcess;
7068

7169
/// <summary>
7270
/// Initializes a new instances of the FFmpegProcess class.
@@ -151,14 +149,12 @@ public CompletionStatus Run(string fileName, string arguments, bool isFFmpeg, bo
151149
Process P = new Process();
152150
this.isFFmpeg = isFFmpeg;
153151
WorkProcess = P;
154-
this.nestedProcess = nestedProcess;
155152
output = new StringBuilder();
156153
isStarted = false;
157154
FileStreams = null;
158155
FrameCount = 0;
159156
FileDuration = TimeSpan.Zero;
160157
cancelWork = new CancellationTokenSource();
161-
progressHistory = new List<KeyValuePair<DateTime, long>>();
162158
if (Options == null)
163159
Options = new ProcessStartOptions();
164160

@@ -193,7 +189,6 @@ public CompletionStatus Run(string fileName, string arguments, bool isFFmpeg, bo
193189

194190
isStarted = false;
195191
cancelWork = null;
196-
progressHistory = null;
197192
LastCompletionStatus = Result;
198193
Completed?.Invoke(this, new CompletedEventArgs(Result));
199194
if ((Result == CompletionStatus.Error || Result == CompletionStatus.Timeout) && Options.DisplayMode == FFmpegDisplayMode.ErrorOnly)
@@ -264,9 +259,8 @@ private void FFmpeg_DataReceived(object sender, DataReceivedEventArgs e) {
264259
isStarted = true;
265260

266261
if (isStarted && e.Data.StartsWith("frame=")) {
267-
FFmpegProgress ProgressInfo = FFmpegParser.ParseProgress(e.Data);
268-
CalculateTimeLeft(ProgressInfo);
269-
ProgressUpdated?.Invoke(this, new ProgressUpdatedEventArgs(ProgressInfo));
262+
FFmpegStatus ProgressInfo = FFmpegParser.ParseProgress(e.Data);
263+
StatusUpdated?.Invoke(this, new StatusUpdatedEventArgs(ProgressInfo));
270264
}
271265
}
272266
}
@@ -282,25 +276,6 @@ private void ParseFileInfo() {
282276
InfoUpdated?.Invoke(this, new EventArgs());
283277
}
284278

285-
/// <summary>
286-
/// Calculates the time left.
287-
/// </summary>
288-
/// <param name="progress">An object containing the latest progress info.</param>
289-
private void CalculateTimeLeft(FFmpegProgress progress) {
290-
// progressHistory contains the last 10 progress values.
291-
progressHistory.Add(new KeyValuePair<DateTime, long>(DateTime.Now, progress.Frame));
292-
if (progressHistory.Count > 10)
293-
progressHistory.RemoveAt(0);
294-
if (progressHistory.Count > 1) {
295-
TimeSpan SampleWorkTime = progressHistory.Last().Key - progressHistory.First().Key;
296-
long SampleWorkFrame = progressHistory.Last().Value - progressHistory.First().Value;
297-
double ProcessingFps = SampleWorkFrame / SampleWorkTime.TotalSeconds;
298-
long WorkLeft = FrameCount - progress.Frame;
299-
if (WorkLeft > 0)
300-
progress.EstimatedTimeLeft = TimeSpan.FromSeconds(WorkLeft / ProcessingFps);
301-
}
302-
}
303-
304279
/// <summary>
305280
/// Gets the first video stream from FileStreams.
306281
/// </summary>

FFmpeg/FFmpegStatus.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace EmergenceGuardian.FFmpeg {
7+
/// <summary>
8+
/// Contains progress information returned from FFmpeg's output.
9+
/// </summary>
10+
public class FFmpegStatus {
11+
public long Frame { get; set; }
12+
public float Fps { get; set; }
13+
public float Quantizer { get; set; }
14+
public string Size { get; set; }
15+
public TimeSpan Time { get; set; }
16+
public string Bitrate { get; set; }
17+
public float Speed { get; set; }
18+
}
19+
}

FFmpeg/FFmpegStreamInfo.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,4 @@ public class FFmpegAudioStreamInfo : FFmpegStreamInfo {
5151
public int Bitrate { get; set; }
5252
}
5353

54-
/// <summary>
55-
/// Contains progress information returned from FFmpeg's output.
56-
/// </summary>
57-
public class FFmpegProgress {
58-
public long Frame { get; set; }
59-
public float Fps { get; set; }
60-
public float Quantizer { get; set; }
61-
public string Size { get; set; }
62-
public TimeSpan Time { get; set; }
63-
public string Bitrate { get; set; }
64-
public float Speed { get; set; }
65-
public TimeSpan EstimatedTimeLeft { get; set; }
66-
}
6754
}

FFmpeg/MediaInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public static long GetFrameCount(string source) {
4949
public static long GetFrameCount(string source, ProcessStartOptions options) {
5050
long Result = 0;
5151
FFmpegProcess Worker = new FFmpeg.FFmpegProcess(options);
52-
Worker.ProgressUpdated += (sender, e) => {
53-
Result = e.Progress.Frame;
52+
Worker.StatusUpdated += (sender, e) => {
53+
Result = e.Status.Frame;
5454
};
5555
Worker.RunFFmpeg(string.Format(@"-i ""{0}"" -f null /dev/null", source));
5656
return Result;

0 commit comments

Comments
 (0)