AppendText to get command and target player in c# - c#

So I would like to make a console exploit for roblox (I know its been donje before and all, but I just want to learn more!) and if you type "kill me" it would kill the player. For now I have got:
private void flatButton1_Click(object sender, EventArgs e)
{
var input = (cmdTxt.Text).ToLower();
var nl = Environment.NewLine;
if (input == "cmds")
{
consoleBox.Text = consoleBox.Text + nl + nl + "=====CMDS=====" +
nl + "kill [p] - Kills a player";
}
else if (input == "kill")
{ }
}
but I have no clue how to make it so the application reads the first command and then reads the second bit (the [p] bit), and runs that.
To make it more clear (I hope) what I would like to do is when a user types a command into the "console" it would split all the words, then recognise them, so when a user types "kill username" it would split the two, and it would get the username and kill them

You can use a Dictionary where the key is the command you want to execute and the value is a function to execute for the matching key.
void Main()
{
Dictionary<string, Action<string>> commands = new Dictionary<string, Action<string>>
{
{ "kill", RunCommand1},
{ "run", RunCommand2},
{ "jump", RunCommand3},
};
string input = Console.ReadLine();
string[] parts = input.ToLower().Split();
if (commands.ContainsKey(parts[0]))
{
string args = (parts.Length > 1 ? parts[1] : "");
commands[parts[0]].Invoke(args);
}
else
Console.WriteLine($"Command {parts[0]} not recognized");
}
void RunCommand1(string value)
{
Console.WriteLine("kill:" + value);
}
void RunCommand2(string value)
{
Console.WriteLine("run:" + value);
}
void RunCommand3(string value)
{
Console.WriteLine("jump:" + value);
}
Here you can find more info on the Action<T> delegate

Related

C# Toggle bool upon key press

I currently have a bool called debug. I want it so that when I press F10 it will set the bool to true, then if I press it again back to false and so on.
This is the code I am using:
bool debug = false;
if (cVersion < oVersion)
{
Process.Start("http://consol.cf/update.php");
return;
}
for (; ; )
{
if (debug)
{
Console.WriteLine("Please type in a command");
cmd = Console.ReadLine();
p.Send(cmd);
}
else
{
Console.WriteLine("Press enter to execute config");
Console.ReadLine();
WebConfigReader conf =
new WebConfigReader(url);
string[] tokens = Regex.Split(conf.ReadString(), #"\r?\n|\r");
foreach (string s in tokens)
//ConsoleConfig cons = new ConsoleConfig();
{
p.Send(s);
//p.Send(test);
}
}
Thanks in advance.
It depends on the exact behaviour you want. You're probably best off rolling your own version of Console.WriteLine.
The following toggles debug and instantly switch to the other mode, ignoring any partially entered command.
private static bool InterruptableReadLine(out string result)
{
var builder = new StringBuilder();
var info = Console.ReadKey(true);
while (info.Key != ConsoleKey.Enter && info.Key != ConsoleKey.F10)
{
Console.Write(info.KeyChar);
builder.Append(info.KeyChar);
info = Console.ReadKey(true);
}
Console.WriteLine();
result = builder.ToString();
return info.Key == ConsoleKey.F10;
}
// reading input, or just waiting for enter in your infinite loop
string command;
var interrupted = InterruptableReadLine(out command);
if (interrupted)
{
debug = !debug;
continue;
}
// do stuff with command if necessary
bool debug = false;
public void Toggle()
{
ConsoleKeyInfo keyinfo = Console.ReadKey();
if (keyinfo.Key == ConsoleKey.F10)
{
debug = !debug;
if(debug)
{
//Your code here if debug = true
}
else
{
//Your code here if debug = false
}
}
else
{
//Your code here if key press other than F10
}
}
ConsoleKeyInfo: Describes the console key that was pressed, including the character represented by the console key and the state of the SHIFT, ALT, and CTRL modifier keys.
Try once may it help you.

Train own classifier IBM Watson Visual Recognition Unity3d

I already followed the steps from installing the SDK and configuring the service credentials. The problem is I cant train my own classifier. I'm getting this error: No overload for method 'TrainClassifier' takes 5 arguments.
void Start()
{
string m_positiveExamplesPath = Application.dataPath + "/testData/cpu_positive_examples.zip";
string m_negativeExamplesPath = Application.dataPath + "/testData/negative_examples.zip";
if(!m_VisualRecognition.TrainClassifier("components", "cpu", m_positiveExamplesPath, m_negativeExamplesPath, OnTrainClassifier))
Log.Debug("ExampleVisualRecognition", "Train classifier failed!");
}
private void OnTrainClassifier(GetClassifiersPerClassifierVerbose classifier)
{
if(classifier != null)
{
Log.Debug("ExampleVisualRecognition", "Classifier is training! " + classifier);
}
else
{
Log.Debug("ExampleVisualRecognition", "Failed to train classifier!");
}
}
Here's the link of the SDK in GitHub. Thanks!
You copied that code from the example page but looks like that everything on that page is outdated. It needs to be updated by IBM.
The VisualRecognition class has 2 overloads of TrainClassifier:
public bool TrainClassifier(OnTrainClassifier callback, string classifierName, Dictionary<string, string> positiveExamples, string negativeExamplesPath = default(string), string mimeType = "application/zip", string customData = default(string))
and
public bool TrainClassifier(OnTrainClassifier callback, string classifierName, Dictionary<string, byte[]> positiveExamplesData, byte[] negativeExamplesData = null, string mimeType = "application/zip", string customData = default(string))
You have the SDK right in front of you. Next time you get an error like this, select the function, right click Go To Definition. It will show you the overload of the function then you will be able to pass the right parameter inside it.
Your code should be something like this:
private VisualRecognition m_VisualRecognition = new VisualRecognition();
void Start()
{
string m_positiveExamplesPath = Application.dataPath + "/testData/cpu_positive_examples.zip";
string m_negativeExamplesPath = Application.dataPath + "/testData/negative_examples.zip";
Dictionary<string, string> positiveExamples = new Dictionary<string, string>();
positiveExamples.Add("giraffe", m_positiveExamplesPath);
if (!m_VisualRecognition.TrainClassifier(OnTrainClassifier, "unity-test-classifier-example", positiveExamples, m_negativeExamplesPath))
Log.Debug("ExampleVisualRecognition", "Train classifier failed!");
}
private void OnTrainClassifier(GetClassifiersPerClassifierVerbose classifier, string data)
{
if (classifier != null)
{
Log.Debug("ExampleVisualRecognition", "Classifier is training! " + classifier);
}
else
{
Log.Debug("ExampleVisualRecognition", "Failed to train classifier!");
}
}
If you need any other example, don't get it from the example page. Get it from the Example folder that comes with the plugin.

CodeFluent vs Interop.MSScriptControl.dll

We had a 32 bits service that we are trying to migrate to 64 bits.
We were using Interop.MSScriptControl.dll to evaluate vb script written by users.
Since there is no 64 bits version of the MSScriptControl. I created a process that was called inside the service. Each time that we need to evaluate users scripts, we call the process. After trying this solution, I found it really slow.
I just discovered the CodeFluentRuntimeClient library that can evaluate vb script as well as JavaScript. However, the way it evaluates the script is complety different from MSScriptControl library.
I created a simple test program to evaluate the old vb script wrote by users.
public class VBScriptEvaluator
{
public static dynamic Evaluate(string key, string script, IDictionary<string, object> parameterValuePair)
{
try
{
using (ScriptEngine engine = new ScriptEngine(ScriptEngine.VBScriptLanguage))
{
ParsedScript parsed = engine.Parse(string.Format(#"Function {0}()
{1}
End Function", key, script));
if (script.Contains("NecUserProfile"))
engine.SetNamedItem("NecUserProfile", #"" + "ADMIN" + #""); //Hardcoded For now
if (parameterValuePair != null && parameterValuePair.Count > 0)
{
foreach (var para in parameterValuePair)
engine.SetNamedItem(para.Key, para.Value);
}
dynamic value = parsed.CallMethod(key);
return (value != null) ? value.ToString() : string.Empty;
}
}
catch (Exception ex)
{
throw;
}
}
}
If I use like this, it's working fine:
static void Main(string[] args)
{
string key = "necGlobalValue";
string script = #"necGlobalValue = ""ADMIN""";
var result = VBScriptEvaluator.Evaluate(key, script, null); //ADMIN
}
Like this it works well too:
static void Main(string[] args)
{
Dictionary<string, object> parameterValuePair = new Dictionary<string, object>();
parameterValuePair.Add("ZINVOICE_MARGIN_0", 141615427.8);
parameterValuePair.Add("ZINVOICE_AMTNOTLIN_0", 187260276.84);
var script = #"If (ZINVOICE_AMTNOTLIN_0) <> 0 Then
SERVER_FLD0000001 = Abs(ZINVOICE_MARGIN_0) / ZINVOICE_AMTNOTLIN_0
else
SERVER_FLD0000001 = 0
End If";
var key = "SERVER_FLD0000001";
var result = VBScriptEvaluator.Evaluate(key, script, parameterValuePair);
}
In the previous library it was detecting automatically the type of the variables that will be evaluated. I can pass integers as string and it will work just fine.
If I replace the value of the dictionary like by using the ScripEngine, it will fail:
Dictionary<string, object> parameterValuePair = new Dictionary<string, object>();
parameterValuePair.Add("ZINVOICE_MARGIN_0", "141615427.8");
parameterValuePair.Add("ZINVOICE_AMTNOTLIN_0", "187260276.84");
Also, If I do this I'm not getting the user ADMIN.
string key = "necGlobalValue";
string script = #"necGlobalValue = ""NecUserProfile""";
var result = VBScriptEvaluator.Evaluate(key, script, null); // output NecUserProfile instead of ADMIN
And BTW I tried to give as much details, that's why the question is that long.
I made it work by passing the parameters to the function instead of using the SetNamedItem function.
public class VBScriptEvaluator
{
public static dynamic Evaluate(string key, string script, IDictionary<string, object> parameterValuePair = null)
{
try
{
using (ScriptEngine engine = new ScriptEngine(ScriptEngine.VBScriptLanguage))
{
List<object> parameters = new List<object>() { "ADMIN" };
string extraParameters = string.Empty;
if (parameterValuePair != null && parameterValuePair.Count > 0)
{
extraParameters = "," + string.Join(",", parameterValuePair.Select(e => e.Key));
foreach (var para in parameterValuePair)
parameters.Add(para.Value);
}
string parsedScript = string.Format(#"Function {0}(NecUserProfile {2})
{1}
End Function", key, script, extraParameters);
ParsedScript parsed = engine.Parse(parsedScript);
dynamic value = parsed.CallMethod(key, parameters.ToArray());
return (value != null) ? value.ToString() : string.Empty;
}
}
catch (Exception ex)
{
throw;
}
}
}
And here's how to use it:
Dictionary<string, object> parameterValuePair = new Dictionary<string, object>()
{
{"Param1", 100.0 },
{"Param2", 10.0}
};
var script = #"If (Param2) <> 0 Then
result = Param1 + Param2
else
result = 1 + 2
End If";
var key = "result";
var result = VBScriptEvaluator.Evaluate(key, script, parameterValuePair); // output 110

How to loop a Console.ReadLine?

I cannot figure out how to read user-input in a loop (with Console.ReadLine). I'm trying to create a note that lets me store what ever the user inputs, and exits if he types exit.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Note myNote = new Note();
Note otherNote = new Note();
myNote.addText("Hi there");
Console.WriteLine(myNote.display());
otherNote.addText(Console.ReadLine());
Console.WriteLine(otherNote.display());
if (otherNote = "exit")
{
}
}
}
}
class Note
{
private string text = "";
private DateTime timeStamp = DateTime.Now;
private DateTime modifiedStamp = DateTime.Now;
int maxLength = 10;
public void addText(string sometext)
{
if (text.Length + sometext.Length < maxLength)
{
text += sometext;
modifiedStamp = DateTime.Now;
}
}
public string display()
{
return "Created: " + timeStamp.ToString() + "\n" +
"Modified: " + modifiedStamp.ToString() + "\n" +
"Content: " + text;
}
}
You need List of Notes in order to add as many notes as you want.
Additionally, you need to first save ReadLine input check if the user really asked to exit otherwise keep adding notes.
var myNotes = new List<Note>();
var firstNote = new Note();
firstNote.addText("Hi there");
Note note;
while (true)
{
var input = Console.ReadLine();
if (input.Equals("exit", StringComparison.OrdinalIgnoreCase))
{
break;
}
note = new Note();
note.addText(input);
myNotes.Add(note);
}
The general format is to use something like this (a while loop with a break condition):
// put code above while loop that only needs to be executed once
while (true) {
// get the user input for every iteration, allowing to exit at will
String line = Console.ReadLine();
if (line.Equals("exit")) {
// exit the method.
return; // use "break" if you just want to exit the loop
}
// this is what will happen in the loop body since we didn't exit
// put whatever note stuff you want to execute again and again in here
}
You'll want to edit what goes into the body of this loop depending on what exactly you want done with your note instances. But generally, you repeatedly prompt a user for input until some condition is met and then you break out of the loop. You may decided that condition (e.g. "enter 10 notes"; "type exit"; etc.)
Per #n0rd's comment, here's how a do...while loop could work:
string input;
var myNotes = new List<Note>();
do{
input = Console.ReadLine();
if (!input.Equals("exit", StringComparison.OrdinalIgnoreCase)){
var note = new Note();
note.addText(input);
myNotes.Add(note);
}
} while (!input.Equals("exit", StringComparison.OrdinalIgnoreCase));
To loop Console.ReadLine() you can use this
`List<string> al = new List<string>(); //list to store string values
while(true)
{
string f = Console.ReadLine();
if(f == null) //check if string is null or not
{
break;
}
else
al.Add(f); //add strings to list
}`
One way to do it is this:
List<string> simpleList = new List<string> { "Alpha", "Bravo", "Charlie", "Delta", "Echo" }; //Dummy data source
Console.WriteLine("Enter a call sign to find in the list. Press X to exit: "); //Prompt
string callSign;
string exitKey = "x";
while ((callSign = Console.ReadLine().ToLower()) != exitKey)
{ //This is where the "Magic" happens
if (simpleList.Contains(callSign))
{
Console.WriteLine($"\"{callSign}\" exists in our simple list");//Output should the list contain our entry
Console.WriteLine(""); //Not really relevant, just needed to added spacing between input and output
}
else
{
Console.WriteLine($"\"{callSign}\" does not exist in our simple list"); //Output should the list not contain our entry
}
Console.WriteLine("");
Console.WriteLine("Enter a call sign to find in the list. Press X to exit: ");//Prompt
}
The line:
while ((callSign = Console.ReadLine().ToLower()) != exitKey) {
...
is where the loop happens. If the entry does not equal the exitKey, the steps are repeated.

Solid FFmpeg wrapper for C#/.NET

I have been searching the web for some time for a solid FFmpeg wrapper for C#/.NET. But I have yet to come up with something useful. I have found the following three projects, but all of them apears to be dead in early alpha stage.
FFmpeg.NET
ffmpeg-sharp
FFLIB.NET
So my question is if anyone knows of a wrapper project that is more mature?
I am not looking for a full transcoding engine with job queues and more.
Just a simple wrapper so I do not have to make a command line call and then parse the console output, but can make method calls and use eventlisteners for progress.
And please feel free to mention any active projects, even if they are stil in the early stages.
This is a wrapper of my own: https://github.com/AydinAdn/MediaToolkit
MediaToolkit can:
Convert video files into various other video formats.
Perform video transcoding tasks.
Options configurable: Bit rate, Frame rate, Resolution / size, Aspect ratio, Duration of video
Perform audio transcoding tasks.
Options configurable: Audio sample rate
Convert video to physical formats using FILM, PAL or NTSC tv standards
Mediums include: DVD, DV, DV50, VCD, SVCD
I'm updating it as I go along, and you're welcome to use it, you can also install it using the Package Manager Console.
PM> Install-Package MediaToolkit
After trying several wrappers, I went with this: FFmpeg auto generated unsafe bindings for C#/.NET and Mono.
It's a set of low-level interop bindings for every class in the FFmpeg namespace.
Maybe not as convenient to use as an actual wrapper, but IMO it's the best solution for working with FFmpeg in .Net, if you want to do non-trivial things.
Pros:
Works
Trustworthy - no 3rd party wrapper code to introduce bugs, assuming you trust FFMpeg itself.
It's always updated to the latest version of FFmpeg
Single nuget package for all of the bindings
XML documentation is included but you still can use the online documentation FFmpeg documentation.
Cons:
Low level: You have to know how to work with pointers to c structs.
Requires some work initially to get it working. I suggest to learn from the official examples.
Note: this thread is about using the FFmpeg API, but for some use cases, it's best to simply use ffmpeg.exe's command line interface.
I have used FFmpeg from a ASP.NET / Windows service (.NET) application.
But I ended up using the command-line, without parsing the console.
By using this - I had an easy way to control - updates of FFmpeg and running multiple conversions on multiple Cores.
Try this, I think I might have written something you can use for a simple wrapper.
http://jasonjano.wordpress.com/2010/02/09/a-simple-c-wrapper-for-ffmpeg/
You can use this nuget package:
I know that you asked about mature project, but i haven't seen any project fullfilling my expectaction so i decided to make my own.
You can easily queue conversions and run it parallel, methods to convert media to different formats, send your own arguments to ffmpeg and parse output from ffmpeg + event listener with current progress.
Install-Package Xabe.FFmpeg
I'm trying to make easy to use, cross-platform FFmpeg wrapper.
You can find more information about this at https://xabe.net/product/xabe_ffmpeg/
More info here: https://xabe.net/product/xabe_ffmpeg/#documentation
Conversion is simple:
IConversionResult result = await Conversion.ToMp4(Resources.MkvWithAudio, output).Start();
If you want progress:
IConversion conversion = Conversion.ToMp4(Resources.MkvWithAudio, output);
conversion.OnProgress += (duration, length) => { currentProgress = duration; }
await conversion.Start();
I'm playing around with an ffmpeg wrapper library called MediaHandler Pro from
http://www.mediasoftpro.com
seems promising so far.
I have been researching the same thing and originally used MediaToolKit (mentioned in another answer) which worked great for conversions but now I need something a bit more robust.
One option that seems mature and still active is:
https://github.com/hudl/HudlFfmpeg
Which you can read more about here:
http://public.hudl.com/bits/archives/2014/08/15/announcing-hudlffmpeg-a-c-framework-to-make-ffmpeg-interaction-simple/
Another option, which may not suit many cases, is to invoke the exe directly from your c# code:
http://www.codeproject.com/Articles/774093/Another-FFmpeg-exe-Csharp-Wrapper
There is another simple one here: http://ivolo.mit.edu/post/Metamorph-Convert-Audio-Video-to-Any-Format-on-Windows-Linux-and-Mac.aspx
Here ya go... Most of this code is 2+ years old so missing a lot of asynchronous stuff, and using an outdated naming convention. Running in a production enviornment for quite some time
~ JT
internal static class FFMpegArgUtils
{
public static string GetEncodeVideoFFMpegArgs(string sSourceFile, MP4Info objMp4Info, double nMbps, int iWidth, int iHeight, bool bIncludeAudio, string sOutputFile)
{
//Ensure file contains a video stream, otherwise this command will fail
if (objMp4Info != null && objMp4Info.VideoStreamCount == 0)
{
throw new Exception("FFMpegArgUtils::GetEncodeVideoFFMpegArgs - mp4 does not contain a video stream");
}
int iBitRateInKbps = (int)(nMbps * 1000);
StringBuilder sbArgs = new StringBuilder();
sbArgs.Append(" -y -threads 2 -i \"" + sSourceFile + "\" -strict -2 "); // 0 tells it to choose how many threads to use
if (bIncludeAudio == true)
{
//sbArgs.Append(" -acodec libmp3lame -ab 96k");
sbArgs.Append(" -acodec aac -ar 44100 -ab 96k");
}
else
{
sbArgs.Append(" -an");
}
sbArgs.Append(" -vcodec libx264 -level 41 -r 15 -crf 25 -g 15 -keyint_min 45 -bf 0");
//sbArgs.Append(" -vf pad=" + iWidth + ":" + iHeight + ":" + iVideoOffsetX + ":" + iVideoOffsetY);
sbArgs.Append(String.Format(" -vf \"scale=iw*min({0}/iw\\,{1}/ih):ih*min({0}/iw\\,{1}/ih),pad={0}:{1}:({0}-iw)/2:({1}-ih)/2\"",iWidth, iHeight));
//Output File
sbArgs.Append(" \"" + sOutputFile + "\"");
return sbArgs.ToString();
}
public static string GetEncodeAudioFFMpegArgs(string sSourceFile, string sOutputFile)
{
var args = String.Format(" -y -threads 2 -i \"{0}\" -strict -2 -acodec aac -ar 44100 -ab 96k -vn \"{1}\"", sSourceFile, sOutputFile);
return args;
//return GetEncodeVideoFFMpegArgs(sSourceFile, null, .2, 854, 480, true, sOutputFile);
//StringBuilder sbArgs = new StringBuilder();
//int iWidth = 854;
//int iHeight = 480;
//sbArgs.Append(" -y -i \"" + sSourceFile + "\" -strict -2 "); // 0 tells it to choose how many threads to use
//sbArgs.Append(" -acodec aac -ar 44100 -ab 96k");
//sbArgs.Append(" -vcodec libx264 -level 41 -r 15 -crf 25 -g 15 -keyint_min 45 -bf 0");
//sbArgs.Append(String.Format(" -vf \"scale=iw*min({0}/iw\\,{1}/ih):ih*min({0}/iw\\,{1}/ih),pad={0}:{1}:({0}-iw)/2:({1}-ih)/2\"", iWidth, iHeight));
//sbArgs.Append(" \"" + sOutputFile + "\"");
//return sbArgs.ToString();
}
}
internal class CreateEncodedVideoCommand : ConsoleCommandBase
{
public event ProgressEventHandler OnProgressEvent;
private string _sSourceFile;
private string _sOutputFolder;
private double _nMaxMbps;
public double BitrateInMbps
{
get { return _nMaxMbps; }
}
public int BitrateInKbps
{
get { return (int)Math.Round(_nMaxMbps * 1000); }
}
private int _iOutputWidth;
private int _iOutputHeight;
private bool _bIsConverting = false;
//private TimeSpan _tsDuration;
private double _nPercentageComplete;
private string _sOutputFile;
private string _sOutputFileName;
private bool _bAudioEnabled = true;
private string _sFFMpegPath;
private string _sExePath;
private string _sArgs;
private MP4Info _objSourceInfo;
private string _sOutputExt;
/// <summary>
/// Encodes an MP4 to the specs provided, quality is a value from 0 to 1
/// </summary>
/// <param name="nQuality">A value from 0 to 1</param>
///
public CreateEncodedVideoCommand(string sSourceFile, string sOutputFolder, string sFFMpegPath, double nMaxBitrateInMbps, MP4Info objSourceInfo, int iOutputWidth, int iOutputHeight, string sOutputExt)
{
_sSourceFile = sSourceFile;
_sOutputFolder = sOutputFolder;
_nMaxMbps = nMaxBitrateInMbps;
_objSourceInfo = objSourceInfo;
_iOutputWidth = iOutputWidth;
_iOutputHeight = iOutputHeight;
_sFFMpegPath = sFFMpegPath;
_sOutputExt = sOutputExt;
}
public void SetOutputFileName(string sOutputFileName)
{
_sOutputFileName = sOutputFileName;
}
public override void Execute()
{
try
{
_bIsConverting = false;
string sFileName = _sOutputFileName != null ? _sOutputFileName : Path.GetFileNameWithoutExtension(_sSourceFile) + "_" + _iOutputWidth + "." + _sOutputExt;
_sOutputFile = _sOutputFolder + "\\" + sFileName;
_sExePath = _sFFMpegPath;
_sArgs = FFMpegArgUtils.GetEncodeVideoFFMpegArgs(_sSourceFile, _objSourceInfo,_nMaxMbps, _iOutputWidth, _iOutputHeight, _bAudioEnabled, _sOutputFile);
InternalExecute(_sExePath, _sArgs);
}
catch (Exception objEx)
{
DispatchException(objEx);
}
}
public override string GetCommandInfo()
{
StringBuilder sbInfo = new StringBuilder();
sbInfo.AppendLine("CreateEncodeVideoCommand");
sbInfo.AppendLine("Exe: " + _sExePath);
sbInfo.AppendLine("Args: " + _sArgs);
sbInfo.AppendLine("[ConsoleOutput]");
sbInfo.Append(ConsoleOutput);
sbInfo.AppendLine("[ErrorOutput]");
sbInfo.Append(ErrorOutput);
return base.GetCommandInfo() + "\n" + sbInfo.ToString();
}
protected override void OnInternalCommandComplete(int iExitCode)
{
DispatchCommandComplete( iExitCode == 0 ? CommandResultType.Success : CommandResultType.Fail);
}
override protected void OnOutputRecieved(object sender, ProcessOutputEventArgs objArgs)
{
//FMPEG out always shows as Error
base.OnOutputRecieved(sender, objArgs);
if (_bIsConverting == false && objArgs.Data.StartsWith("Press [q] to stop encoding") == true)
{
_bIsConverting = true;
}
else if (_bIsConverting == true && objArgs.Data.StartsWith("frame=") == true)
{
//Capture Progress
UpdateProgressFromOutputLine(objArgs.Data);
}
else if (_bIsConverting == true && _nPercentageComplete > .8 && objArgs.Data.StartsWith("frame=") == false)
{
UpdateProgress(1);
_bIsConverting = false;
}
}
override protected void OnProcessExit(object sender, ProcessExitedEventArgs args)
{
_bIsConverting = false;
base.OnProcessExit(sender, args);
}
override public void Abort()
{
if (_objCurrentProcessRunner != null)
{
//_objCurrentProcessRunner.SendLineToInputStream("q");
_objCurrentProcessRunner.Dispose();
}
}
#region Helpers
//private void CaptureSourceDetailsFromOutput()
//{
// String sInputStreamInfoStartLine = _colErrorLines.SingleOrDefault(o => o.StartsWith("Input #0"));
// int iStreamInfoStartIndex = _colErrorLines.IndexOf(sInputStreamInfoStartLine);
// if (iStreamInfoStartIndex >= 0)
// {
// string sDurationInfoLine = _colErrorLines[iStreamInfoStartIndex + 1];
// string sDurantionTime = sDurationInfoLine.Substring(12, 11);
// _tsDuration = VideoUtils.GetDurationFromFFMpegDurationString(sDurantionTime);
// }
//}
private void UpdateProgressFromOutputLine(string sOutputLine)
{
int iTimeIndex = sOutputLine.IndexOf("time=");
int iBitrateIndex = sOutputLine.IndexOf(" bitrate=");
string sCurrentTime = sOutputLine.Substring(iTimeIndex + 5, iBitrateIndex - iTimeIndex - 5);
double nCurrentTimeInSeconds = double.Parse(sCurrentTime);
double nPercentageComplete = nCurrentTimeInSeconds / _objSourceInfo.Duration.TotalSeconds;
UpdateProgress(nPercentageComplete);
//Console.WriteLine("Progress: " + _nPercentageComplete);
}
private void UpdateProgress(double nPercentageComplete)
{
_nPercentageComplete = nPercentageComplete;
if (OnProgressEvent != null)
{
OnProgressEvent(this, new ProgressEventArgs( _nPercentageComplete));
}
}
#endregion
//public TimeSpan Duration { get { return _tsDuration; } }
public double Progress { get { return _nPercentageComplete; } }
public string OutputFile { get { return _sOutputFile; } }
public bool AudioEnabled
{
get { return _bAudioEnabled; }
set { _bAudioEnabled = value; }
}
}
public abstract class ConsoleCommandBase : CommandBase, ICommand
{
protected ProcessRunner _objCurrentProcessRunner;
protected List<String> _colOutputLines;
protected List<String> _colErrorLines;
private int _iExitCode;
public ConsoleCommandBase()
{
_colOutputLines = new List<string>();
_colErrorLines = new List<string>();
}
protected void InternalExecute(string sExePath, string sArgs)
{
InternalExecute(sExePath, sArgs, null, null, null);
}
protected void InternalExecute(string sExePath, string sArgs, string sDomain, string sUsername, string sPassword)
{
try
{
if (_objCurrentProcessRunner == null || _bIsRunning == false)
{
StringReader objStringReader = new StringReader(string.Empty);
_objCurrentProcessRunner = new ProcessRunner(sExePath, sArgs);
_objCurrentProcessRunner.SetCredentials(sDomain, sUsername, sPassword);
_objCurrentProcessRunner.OutputReceived += new ProcessOutputEventHandler(OnOutputRecieved);
_objCurrentProcessRunner.ProcessExited += new ProcessExitedEventHandler(OnProcessExit);
_objCurrentProcessRunner.Run();
_bIsRunning = true;
_bIsComplete = false;
}
else
{
DispatchException(new Exception("Processor Already Running"));
}
}
catch (Exception objEx)
{
DispatchException(objEx);
}
}
protected virtual void OnOutputRecieved(object sender, ProcessOutputEventArgs args)
{
try
{
if (args.Error == true)
{
_colErrorLines.Add(args.Data);
//Console.WriteLine("Error: " + args.Data);
}
else
{
_colOutputLines.Add(args.Data);
//Console.WriteLine(args.Data);
}
}
catch (Exception objEx)
{
DispatchException(objEx);
}
}
protected virtual void OnProcessExit(object sender, ProcessExitedEventArgs args)
{
try
{
Console.Write(ConsoleOutput);
_iExitCode = args.ExitCode;
_bIsRunning = false;
_bIsComplete = true;
//Some commands actually fail to succeed
//if(args.ExitCode != 0)
//{
// DispatchException(new Exception("Command Failed: " + this.GetType().Name + "\nConsole: " + ConsoleOutput + "\nConsoleError: " + ErrorOutput));
//}
OnInternalCommandComplete(_iExitCode);
if (_objCurrentProcessRunner != null)
{
_objCurrentProcessRunner.Dispose();
_objCurrentProcessRunner = null;
}
}
catch (Exception objEx)
{
DispatchException(objEx);
}
}
abstract protected void OnInternalCommandComplete(int iExitCode);
protected string JoinLines(List<String> colLines)
{
StringBuilder sbOutput = new StringBuilder();
colLines.ForEach( o => sbOutput.AppendLine(o));
return sbOutput.ToString();
}
#region Properties
public int ExitCode
{
get { return _iExitCode; }
}
#endregion
public override string GetCommandInfo()
{
StringBuilder sbCommandInfo = new StringBuilder();
sbCommandInfo.AppendLine("Command: " + this.GetType().Name);
sbCommandInfo.AppendLine("Console Output");
if (_colOutputLines != null)
{
foreach (string sOutputLine in _colOutputLines)
{
sbCommandInfo.AppendLine("\t" + sOutputLine);
}
}
sbCommandInfo.AppendLine("Error Output");
if (_colErrorLines != null)
{
foreach (string sErrorLine in _colErrorLines)
{
sbCommandInfo.AppendLine("\t" + sErrorLine);
}
}
return sbCommandInfo.ToString();
}
public String ConsoleOutput { get { return JoinLines(_colOutputLines); } }
public String ErrorOutput { get { return JoinLines(_colErrorLines);} }
}
CommandBase : ICommand
{
protected IDedooseContext _context;
protected Boolean _bIsRunning = false;
protected Boolean _bIsComplete = false;
#region Custom Events
public event CommandCompleteEventHandler OnCommandComplete;
event CommandCompleteEventHandler ICommand.OnCommandComplete
{
add { if (OnCommandComplete != null) { lock (OnCommandComplete) { OnCommandComplete += value; } } else { OnCommandComplete = new CommandCompleteEventHandler(value); } }
remove { if (OnCommandComplete != null) { lock (OnCommandComplete) { OnCommandComplete -= value; } } }
}
public event UnhandledExceptionEventHandler OnCommandException;
event UnhandledExceptionEventHandler ICommand.OnCommandException
{
add { if (OnCommandException != null) { lock (OnCommandException) { OnCommandException += value; } } else { OnCommandException = new UnhandledExceptionEventHandler(value); } }
remove { if (OnCommandException != null) { lock (OnCommandException) { OnCommandException -= value; } } }
}
public event ProgressEventHandler OnProgressUpdate;
event ProgressEventHandler ICommand.OnProgressUpdate
{
add { if (OnProgressUpdate != null) { lock (OnProgressUpdate) { OnProgressUpdate += value; } } else { OnProgressUpdate = new ProgressEventHandler(value); } }
remove { if (OnProgressUpdate != null) { lock (OnProgressUpdate) { OnProgressUpdate -= value; } } }
}
#endregion
protected CommandBase()
{
_context = UnityGlobalContainer.Instance.Context;
}
protected void DispatchCommandComplete(CommandResultType enResult)
{
if (enResult == CommandResultType.Fail)
{
StringBuilder sbMessage = new StringBuilder();
sbMessage.AppendLine("Command Commpleted with Failure: " + this.GetType().Name);
sbMessage.Append(GetCommandInfo());
Exception objEx = new Exception(sbMessage.ToString());
DispatchException(objEx);
}
else
{
if (OnCommandComplete != null)
{
OnCommandComplete(this, new CommandCompleteEventArgs(enResult));
}
}
}
protected void DispatchException(Exception objEx)
{
if (OnCommandException != null)
{
OnCommandException(this, new UnhandledExceptionEventArgs(objEx, true));
}
else
{
_context.Logger.LogException(objEx, MethodBase.GetCurrentMethod());
throw objEx;
}
}
protected void DispatchProgressUpdate(double nProgressRatio)
{
if (OnProgressUpdate != null) { OnProgressUpdate(this, new ProgressEventArgs(nProgressRatio)); }
}
public virtual string GetCommandInfo()
{
return "Not Implemented: " + this.GetType().Name;
}
public virtual void Execute() { throw new NotImplementedException(); }
public virtual void Abort() { throw new NotImplementedException(); }
public Boolean IsRunning { get { return _bIsRunning; } }
public Boolean IsComplete { get { return _bIsComplete; } }
public double GetProgressRatio()
{
throw new NotImplementedException();
}
}
public delegate void CommandCompleteEventHandler(object sender, CommandCompleteEventArgs e);
public interface ICommand
{
event CommandCompleteEventHandler OnCommandComplete;
event UnhandledExceptionEventHandler OnCommandException;
event ProgressEventHandler OnProgressUpdate;
double GetProgressRatio();
string GetCommandInfo();
void Execute();
void Abort();
}
// for the process runner stuff look up ProcessRunner by Roger Knapp
string result = String.Empty;
StreamReader srOutput = null;
var oInfo = new ProcessStartInfo(exePath, parameters)
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
var output = string.Empty;
try
{
Process process = System.Diagnostics.Process.Start(oInfo);
output = process.StandardError.ReadToEnd();
process.WaitForExit();
process.Close();
}
catch (Exception)
{
output = string.Empty;
}
return output;
This wrapper will not let the method fall into a loop.
Try this,it worked for me.
I forked FFPMEG.net from codeplex.
Still actively being worked on.
https://github.com/spoiledtechie/FFMpeg.Net
It doesn't use the dlls, but rather the exe. So it tends to be more stable.
See Auto Generated FFmpeg wrapper for C#/.NET and Mono, an awesome project which seems like the only true, complete .NET wrapper for FFmpeg interop out there.

Categories

Resources