Skip to content

Commit 2256b7a

Browse files
author
Stefan Seeland
committed
Mencoder Bin updated to MPlayer r37905 for 32-bit Windows
Fixed - Progress isn't reported for mp4 files
1 parent 3010190 commit 2256b7a

File tree

11 files changed

+174
-123
lines changed

11 files changed

+174
-123
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,4 @@ $RECYCLE.BIN/
154154

155155
# Mac desktop service store files
156156
.DS_Store
157+
/packages/CreateNewNuGetPackageFromProjectAfterEachBuild.1.8.3

MencoderSharp/Mencoder.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ namespace MencoderSharp
88
/// </summary>
99
public class Mencoder : MencoderBase
1010
{
11+
/// <summary>
12+
/// The standard error
13+
/// </summary>
14+
public string standardError;
15+
16+
/// <summary>
17+
/// Mencoders the specified source.
18+
/// </summary>
19+
/// <param name="source">The source.</param>
20+
/// <param name="destination">The destination.</param>
21+
/// <returns></returns>
22+
public bool encodeToMp4(Uri source, Uri destination)
23+
{
24+
return mencoder(source, destination, "-vf dsize=16/9,scale=-10:-1,harddup -of lavf -lavfopts format=mp4 -ovc x264 -sws 9 -x264encopts nocabac:level_idc=30:bframes=0:bitrate=512:threads=auto:turbo=1:global_header:threads=auto", "-oac mp3lame");
25+
}
26+
1127
/// <summary>
1228
/// Mencoders the specified source.
1329
/// </summary>
@@ -34,25 +50,8 @@ public bool mencoder(Uri source, Uri destination, string videoParameter, string
3450
return true;
3551
return false;
3652
}
37-
38-
/// <summary>
39-
/// The standard error
40-
/// </summary>
41-
public string standardError;
42-
4353
// Howto create a Async Method that throws events when finished
4454
//http://msdn.microsoft.com/en-us/library/e7a34yad.aspx
4555
// The method to be executed asynchronously.
46-
47-
/// <summary>
48-
/// Mencoders the specified source.
49-
/// </summary>
50-
/// <param name="source">The source.</param>
51-
/// <param name="destination">The destination.</param>
52-
/// <returns></returns>
53-
public bool encodeToMp4(Uri source, Uri destination)
54-
{
55-
return mencoder(source, destination, "-vf dsize=16/9,scale=-10:-1,harddup -of lavf -lavfopts format=mp4 -ovc x264 -sws 9 -x264encopts nocabac:level_idc=30:bframes=0:bitrate=512:threads=auto:turbo=1:global_header:threads=auto", "-oac faac -faacopts mpeg=4:object=2:raw:br=128");
56-
}
5756
}
5857
}

MencoderSharp/MencoderAsync.cs

Lines changed: 108 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void cancelEncodeAsync()
6060
/// <param name="destination">The destination.</param>
6161
public void startEncodeAsync(Uri source, Uri destination)
6262
{
63-
startEncodeAsync(source, destination, "-vf dsize=16/9,scale=-10:-1,harddup -of lavf -lavfopts format=mp4 -ovc x264 -sws 9 -x264encopts nocabac:level_idc=30:bframes=0:bitrate=512:threads=auto:turbo=1:global_header:threads=auto", "-oac faac -faacopts mpeg=4:object=2:raw:br=128");
63+
startEncodeAsync(source, destination, "-vf dsize=16/9,scale=-10:-1,harddup -of lavf -lavfopts format=mp4 -ovc x264 -sws 9 -x264encopts nocabac:level_idc=30:bframes=0:bitrate=512:threads=auto:turbo=1:global_header:threads=auto", "-oac mp3lame");
6464
}
6565

6666
/// <summary>
@@ -72,20 +72,19 @@ public void startEncodeAsync(Uri source, Uri destination)
7272
/// <param name="audioParameter">e.g. -oac mp3lame</param>
7373
public void startEncodeAsync(Uri source, Uri destination, string videoParameter, string audioParameter)
7474
{
75-
backgroundWorker1 = new BackgroundWorker();
76-
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
77-
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
78-
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
79-
80-
Result = new mencoderResults();
81-
MencoderParameters parameters;
82-
parameters.source = source.OriginalString;
83-
parameters.destination = destination.OriginalString;
84-
parameters.audioParameter = audioParameter;
85-
parameters.videoParameter = videoParameter;
86-
backgroundWorker1.RunWorkerAsync(parameters);
87-
backgroundWorker1.WorkerReportsProgress = true;
88-
backgroundWorker1.WorkerSupportsCancellation = true;
75+
MencoderParameters originalString = new MencoderParameters();
76+
this.backgroundWorker1 = new BackgroundWorker();
77+
this.backgroundWorker1.DoWork += new DoWorkEventHandler(this.backgroundWorker1_DoWork);
78+
this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
79+
this.backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
80+
this.Result = new mencoderResults();
81+
originalString.source = source.OriginalString;
82+
originalString.destination = destination.OriginalString;
83+
originalString.audioParameter = audioParameter;
84+
originalString.videoParameter = videoParameter;
85+
this.backgroundWorker1.RunWorkerAsync(originalString);
86+
this.backgroundWorker1.WorkerReportsProgress = true;
87+
this.backgroundWorker1.WorkerSupportsCancellation = true;
8988
}
9089

9190
/// <summary>
@@ -97,20 +96,19 @@ public void startEncodeAsync(Uri source, Uri destination, string videoParameter,
9796
/// <param name="audioParameter">The audio parameter.</param>
9897
public void startEncodeAsync(string source, string destination, string videoParameter, string audioParameter)
9998
{
100-
backgroundWorker1 = new BackgroundWorker();
101-
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
102-
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
103-
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
104-
105-
Result = new mencoderResults();
106-
MencoderParameters parameters;
107-
parameters.source = source;
108-
parameters.destination = destination;
109-
parameters.audioParameter = audioParameter;
110-
parameters.videoParameter = videoParameter;
111-
backgroundWorker1.RunWorkerAsync(parameters);
112-
backgroundWorker1.WorkerReportsProgress = true;
113-
backgroundWorker1.WorkerSupportsCancellation = true;
99+
MencoderParameters mencoderParameter = new MencoderParameters();
100+
this.backgroundWorker1 = new BackgroundWorker();
101+
this.backgroundWorker1.DoWork += new DoWorkEventHandler(this.backgroundWorker1_DoWork);
102+
this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
103+
this.backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
104+
this.Result = new mencoderResults();
105+
mencoderParameter.source = source;
106+
mencoderParameter.destination = destination;
107+
mencoderParameter.audioParameter = audioParameter;
108+
mencoderParameter.videoParameter = videoParameter;
109+
this.backgroundWorker1.RunWorkerAsync(mencoderParameter);
110+
this.backgroundWorker1.WorkerReportsProgress = true;
111+
this.backgroundWorker1.WorkerSupportsCancellation = true;
114112
}
115113

116114
/// <summary>
@@ -139,19 +137,19 @@ protected virtual void OnProgress(EventArgs e)
139137

140138
private static int parseAndReportProgress(BackgroundWorker worker, string standardOut, int progressReporting)
141139
{
142-
if (standardOut.StartsWith("Pos:"))
140+
if (!standardOut.StartsWith("Pos:"))
143141
{
144-
int progress1 = 0;
145-
if (int.TryParse(standardOut.Split('(')[1].Substring(0, 2).Trim(), out progress1))
146-
if (progress1 > progressReporting)
147-
{
148-
progressReporting = progress1;
149-
worker.ReportProgress(progressReporting, standardOut);
150-
}
142+
worker.ReportProgress(progressReporting, standardOut);
151143
}
152144
else
153145
{
154-
worker.ReportProgress(progressReporting, standardOut);
146+
int num = 0;
147+
char[] chrArray = new char[] { '(' };
148+
if (int.TryParse(standardOut.Split(chrArray)[1].Substring(0, 2).Trim(), out num) && num > progressReporting)
149+
{
150+
progressReporting = num;
151+
worker.ReportProgress(progressReporting, standardOut);
152+
}
155153
}
156154
return progressReporting;
157155
}
@@ -163,52 +161,78 @@ private static int parseAndReportProgress(BackgroundWorker worker, string standa
163161
/// <param name="e">The <see cref="DoWorkEventArgs" /> instance containing the event data.</param>
164162
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
165163
{
164+
string str;
166165
// Get the BackgroundWorker that raised this event.
167-
BackgroundWorker worker = sender as BackgroundWorker;
166+
BackgroundWorker backgroundWorker = sender as BackgroundWorker;
168167
// Assign the result of the computation
169168
// to the Result property of the DoWorkEventArgs
170169
// object. This is will be available to the
171170
// RunWorkerCompleted eventhandler.
172-
MencoderParameters parameter = (MencoderParameters)(e.Argument);
171+
MencoderParameters argument = (MencoderParameters)e.Argument;
172+
173173
try
174174
{
175-
Process p = new Process();
176-
//Asynchron read of standardoutput:
177-
//http://msdn.microsoft.com/de-de/library/system.diagnostics.process.beginoutputreadline.aspx
178-
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
179-
//p.StartInfo.FileName = @"mencoder.exe";
180-
p.StartInfo.FileName = pathToMencoderExe;
181-
//http://msdn.microsoft.com/de-de/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx
182-
p.StartInfo.RedirectStandardOutput = true;
183-
p.StartInfo.RedirectStandardError = true;
184-
p.StartInfo.UseShellExecute = false;
185-
p.StartInfo.CreateNoWindow = true;
186-
p.StartInfo.Arguments = "\"" + parameter.source + "\" " + parameter.videoParameter + " " + parameter.audioParameter + " -o \"" + parameter.destination + "\"";
187-
p.Start();
188-
//nur eins darf synchron gelesen werden!! http://msdn.microsoft.com/de-de/library/system.diagnostics.processstartinfo.redirectstandarderror.aspx
189-
p.BeginErrorReadLine();
190-
string standardOut;
191-
int progressReporting = 0;
192-
while (((standardOut = p.StandardOutput.ReadLine()) != null) && (!worker.CancellationPending))
175+
Process process = new Process();
176+
process.ErrorDataReceived += new DataReceivedEventHandler(this.p_ErrorDataReceived);
177+
process.StartInfo.FileName = this.pathToMencoderExe;
178+
process.StartInfo.RedirectStandardOutput = true;
179+
process.StartInfo.RedirectStandardError = true;
180+
process.StartInfo.UseShellExecute = false;
181+
process.StartInfo.CreateNoWindow = true;
182+
ProcessStartInfo startInfo = process.StartInfo;
183+
string[] strArrays = new string[] { "\"", argument.source, "\" ", argument.videoParameter, " ", argument.audioParameter, " -o \"", argument.destination, "\"" };
184+
startInfo.Arguments = string.Concat(strArrays);
185+
process.Start();
186+
process.BeginErrorReadLine();
187+
int num = 0;
188+
while (true)
193189
{
194-
progressReporting = parseAndReportProgress(worker, standardOut, progressReporting);
190+
string str1 = process.StandardOutput.ReadLine();
191+
str = str1;
192+
if (str1 == null || backgroundWorker.CancellationPending)
193+
{
194+
break;
195+
}
196+
num = MencoderAsync.parseAndReportProgress(backgroundWorker, str, num);
195197
}
196-
if (!worker.CancellationPending)
198+
if (backgroundWorker.CancellationPending)
197199
{
198-
p.WaitForExit();
199-
e.Result = new mencoderResults { Exitcode = p.ExitCode, StandardError = standardError, ExecutionWasSuccessfull = (p.ExitCode == 0), StandardOutput = standardOut };
200+
mencoderResults mencoderResult = new mencoderResults()
201+
{
202+
Exitcode = 99,
203+
StandardError = this.standardError,
204+
ExecutionWasSuccessfull = false,
205+
StandardOutput = str
206+
};
207+
e.Result = mencoderResult;
208+
process.Close();
209+
process.CancelErrorRead();
210+
process.Dispose();
200211
}
201212
else
202213
{
203-
e.Result = new mencoderResults { Exitcode = 99, StandardError = standardError, ExecutionWasSuccessfull = false, StandardOutput = standardOut };
204-
p.Close();
205-
p.CancelErrorRead();
206-
p.Dispose();
214+
process.WaitForExit();
215+
mencoderResults mencoderResult1 = new mencoderResults()
216+
{
217+
Exitcode = process.ExitCode,
218+
StandardError = this.standardError,
219+
ExecutionWasSuccessfull = process.ExitCode == 0,
220+
StandardOutput = str
221+
};
222+
e.Result = mencoderResult1;
207223
}
208224
}
209-
catch (Exception ex)
225+
catch (Exception exception1)
210226
{
211-
e.Result = new mencoderResults { Exitcode = 99, StandardError = standardError, ExecutionWasSuccessfull = false, StandardOutput = ex.Message };
227+
Exception exception = exception1;
228+
mencoderResults mencoderResult2 = new mencoderResults()
229+
{
230+
Exitcode = 99,
231+
StandardError = this.standardError,
232+
ExecutionWasSuccessfull = false,
233+
StandardOutput = exception.Message
234+
};
235+
e.Result = mencoderResult2;
212236
}
213237
}
214238

@@ -219,26 +243,25 @@ private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
219243
/// <param name="e">The <see cref="ProgressChangedEventArgs" /> instance containing the event data.</param>
220244
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
221245
{
222-
string userstate = (string)e.UserState;
223-
progress = e.ProgressPercentage;
224-
if (progress == 0)
246+
string userState = (string)e.UserState;
247+
this.progress = e.ProgressPercentage;
248+
if (this.progress == 0)
249+
{
250+
MencoderAsync mencoderAsync = this;
251+
mencoderAsync.standardError = string.Concat(mencoderAsync.standardError, userState, "\n");
252+
}
253+
else if (!string.IsNullOrEmpty(this.rememberLastLine))
225254
{
226-
standardError += userstate + "\n";
255+
this.standardError = this.standardError.Replace(this.rememberLastLine, userState);
256+
this.rememberLastLine = userState;
227257
}
228258
else
229259
{
230-
if (rememberLastLine == null)
231-
{
232-
rememberLastLine = userstate;
233-
standardError += userstate + "\n";
234-
}
235-
else
236-
{
237-
standardError = standardError.Replace(rememberLastLine, userstate);
238-
rememberLastLine = userstate;
239-
}
260+
this.rememberLastLine = userState;
261+
MencoderAsync mencoderAsync1 = this;
262+
mencoderAsync1.standardError = string.Concat(mencoderAsync1.standardError, userState, "\n");
240263
}
241-
OnProgress(EventArgs.Empty);
264+
this.OnProgress(EventArgs.Empty);
242265
}
243266

244267
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
@@ -259,7 +282,8 @@ private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerComple
259282
/// <param name="e">The <see cref="DataReceivedEventArgs" /> instance containing the event data.</param>
260283
private void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
261284
{
262-
standardError += e.Data;
285+
MencoderAsync mencoderAsync = this;
286+
mencoderAsync.standardError = string.Concat(mencoderAsync.standardError, e.Data);
263287
}
264288
}
265289
}

MencoderSharp/MencoderSharp.nuspec

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
33
<metadata>
44
<id>$id$</id>
5-
<version>1.1.4</version>
5+
<version>1.1.6</version>
66
<title>$id$</title>
77
<authors>$author$</authors>
88
<owners>$author$</owners>
9-
<licenseUrl>http://www.gnu.org/licenses/gpl.html</licenseUrl>
10-
<projectUrl>http://code.google.com/p/mencoder-sharp/</projectUrl>
9+
<licenseUrl>https://www.gnu.org/licenses/agpl.html</licenseUrl>
10+
<projectUrl>https://github.com/Codeuctivity/mencoder-sharp</projectUrl>
1111
<iconUrl>https://avatars3.githubusercontent.com/u/8453155?v=2&amp;s=200</iconUrl>
1212
<requireLicenseAcceptance>true</requireLicenseAcceptance>
1313
<description>$description$</description>
@@ -28,6 +28,11 @@
2828
Publishing as stable
2929
1.1.4
3030
Typo
31+
1.1.5
32+
Fixed Bug which can cause exceptions on finishing encoding
33+
1.1.6
34+
Mencoder Bin updated to MPlayer r37905 for 32-bit Windows
35+
Fixed - Progress isn't reported for mp4 files
3136
</releaseNotes>
3237
<tags>.net reencode mencoder multimedia convert ffmpeg mp4 xvid</tags>
3338
</metadata>

0 commit comments

Comments
 (0)