)delegate andDispatchQueue:(dispatch_queue_t)queue {
return [self executeWithArgumentsAsync:[self parseArguments:command] withCallback:delegate andDispatchQueue:queue];
}
/**
* Synchronously executes FFmpeg command provided. Delimiter parameter is used to split command into arguments.
*
* @param command FFmpeg command
* @param delimiter arguments delimiter
* @return zero on successful execution, 255 on user cancel and non-zero on error
*/
+ (int)execute:(NSString*)command delimiter:(NSString*)delimiter {
// SPLITTING ARGUMENTS
NSArray* argumentArray = [command componentsSeparatedByString:(delimiter == nil ? @" ": delimiter)];
return [self executeWithArguments:argumentArray];
}
/**
* Cancels an ongoing operation.
*
* This function does not wait for termination to complete and returns immediately.
*/
+ (void)cancel {
cancel_operation(DEFAULT_EXECUTION_ID);
}
/**
* Cancels an ongoing operation.
*
* This function does not wait for termination to complete and returns immediately.
*
* @param executionId execution id
*/
+ (void)cancel:(long)executionId {
cancel_operation(executionId);
}
/**
* Parses the given command into arguments.
*
* @param command string command
* @return array of arguments
*/
+ (NSArray*)parseArguments:(NSString*)command {
NSMutableArray *argumentArray = [[NSMutableArray alloc] init];
NSMutableString *currentArgument = [[NSMutableString alloc] init];
bool singleQuoteStarted = false;
bool doubleQuoteStarted = false;
for (int i = 0; i < command.length; i++) {
unichar previousChar;
if (i > 0) {
previousChar = [command characterAtIndex:(i - 1)];
} else {
previousChar = 0;
}
unichar currentChar = [command characterAtIndex:i];
if (currentChar == ' ') {
if (singleQuoteStarted || doubleQuoteStarted) {
[currentArgument appendFormat: @"%C", currentChar];
} else if ([currentArgument length] > 0) {
[argumentArray addObject: currentArgument];
currentArgument = [[NSMutableString alloc] init];
}
} else if (currentChar == '\'' && (previousChar == 0 || previousChar != '\\')) {
if (singleQuoteStarted) {
singleQuoteStarted = false;
} else if (doubleQuoteStarted) {
[currentArgument appendFormat: @"%C", currentChar];
} else {
singleQuoteStarted = true;
}
} else if (currentChar == '\"' && (previousChar == 0 || previousChar != '\\')) {
if (doubleQuoteStarted) {
doubleQuoteStarted = false;
} else if (singleQuoteStarted) {
[currentArgument appendFormat: @"%C", currentChar];
} else {
doubleQuoteStarted = true;
}
} else {
[currentArgument appendFormat: @"%C", currentChar];
}
}
if ([currentArgument length] > 0) {
[argumentArray addObject: currentArgument];
}
return argumentArray;
}
/**
* Combines arguments into a string.
*
* @param arguments arguments
* @return string containing all arguments
*/
+ (NSString*)argumentsToString:(NSArray*)arguments {
if (arguments == nil) {
return @"null";
}
NSMutableString *string = [NSMutableString stringWithString:@""];
for (int i=0; i < [arguments count]; i++) {
NSString *argument = [arguments objectAtIndex:i];
if (i > 0) {
[string appendString:@" "];
}
[string appendString:argument];
}
return string;
}
/**
*
Lists ongoing executions.
*
* @return list of ongoing executions
*/
+ (NSArray*)listExecutions {
[executionsLock lock];
NSArray *array = [NSArray arrayWithArray:executions];
[executionsLock unlock];
return array;
}
@end