FFmpegKit iOS / macOS / tvOS API  4.4
FFmpegKit.m
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021 Taner Sener
3  *
4  * This file is part of FFmpegKit.
5  *
6  * FFmpegKit is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * FFmpegKit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with FFmpegKit. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #import "fftools_ffmpeg.h"
21 #import "ArchDetect.h"
22 #import "AtomicLong.h"
23 #import "FFmpegKit.h"
24 #import "FFmpegKitConfig.h"
25 #import "Packages.h"
26 
27 @implementation FFmpegKit
28 
29 + (void)initialize {
30  NSLog(@"Loading ffmpeg-kit.\n");
31 
32  [FFmpegKitConfig class];
33 
34  NSLog(@"Loaded ffmpeg-kit-%@-%@-%@-%@\n", [Packages getPackageName], [ArchDetect getArch], [FFmpegKitConfig getVersion], [FFmpegKitConfig getBuildDate]);
35 }
36 
37 + (FFmpegSession*)executeWithArguments:(NSArray*)arguments {
38  FFmpegSession* session = [[FFmpegSession alloc] init:arguments];
40  return session;
41 }
42 
43 + (FFmpegSession*)executeWithArgumentsAsync:(NSArray*)arguments withExecuteCallback:(ExecuteCallback)executeCallback {
44  FFmpegSession* session = [[FFmpegSession alloc] init:arguments withExecuteCallback:executeCallback];
46  return session;
47 }
48 
49 + (FFmpegSession*)executeWithArgumentsAsync:(NSArray*)arguments withExecuteCallback:(ExecuteCallback)executeCallback withLogCallback:(LogCallback)logCallback withStatisticsCallback:(StatisticsCallback)statisticsCallback {
50  FFmpegSession* session = [[FFmpegSession alloc] init:arguments withExecuteCallback:executeCallback withLogCallback:logCallback withStatisticsCallback:statisticsCallback];
52  return session;
53 }
54 
55 + (FFmpegSession*)executeWithArgumentsAsync:(NSArray*)arguments withExecuteCallback:(ExecuteCallback)executeCallback onDispatchQueue:(dispatch_queue_t)queue {
56  FFmpegSession* session = [[FFmpegSession alloc] init:arguments withExecuteCallback:executeCallback];
58  return session;
59 }
60 
61 + (FFmpegSession*)executeWithArgumentsAsync:(NSArray*)arguments withExecuteCallback:(ExecuteCallback)executeCallback withLogCallback:(LogCallback)logCallback withStatisticsCallback:(StatisticsCallback)statisticsCallback onDispatchQueue:(dispatch_queue_t)queue {
62  FFmpegSession* session = [[FFmpegSession alloc] init:arguments withExecuteCallback:executeCallback withLogCallback:logCallback withStatisticsCallback:statisticsCallback];
64  return session;
65 }
66 
67 + (FFmpegSession*)execute:(NSString*)command {
68  FFmpegSession* session = [[FFmpegSession alloc] init:[FFmpegKit parseArguments:command]];
70  return session;
71 }
72 
73 + (FFmpegSession*)executeAsync:(NSString*)command withExecuteCallback:(ExecuteCallback)executeCallback {
74  FFmpegSession* session = [[FFmpegSession alloc] init:[FFmpegKit parseArguments:command] withExecuteCallback:executeCallback];
76  return session;
77 }
78 
79 + (FFmpegSession*)executeAsync:(NSString*)command withExecuteCallback:(ExecuteCallback)executeCallback withLogCallback:(LogCallback)logCallback withStatisticsCallback:(StatisticsCallback)statisticsCallback {
80  FFmpegSession* session = [[FFmpegSession alloc] init:[FFmpegKit parseArguments:command] withExecuteCallback:executeCallback withLogCallback:logCallback withStatisticsCallback:statisticsCallback];
82  return session;
83 }
84 
85 + (FFmpegSession*)executeAsync:(NSString*)command withExecuteCallback:(ExecuteCallback)executeCallback onDispatchQueue:(dispatch_queue_t)queue {
86  FFmpegSession* session = [[FFmpegSession alloc] init:[FFmpegKit parseArguments:command] withExecuteCallback:executeCallback];
88  return session;
89 }
90 
91 + (FFmpegSession*)executeAsync:(NSString*)command withExecuteCallback:(ExecuteCallback)executeCallback withLogCallback:(LogCallback)logCallback withStatisticsCallback:(StatisticsCallback)statisticsCallback onDispatchQueue:(dispatch_queue_t)queue {
92  FFmpegSession* session = [[FFmpegSession alloc] init:[FFmpegKit parseArguments:command] withExecuteCallback:executeCallback withLogCallback:logCallback withStatisticsCallback:statisticsCallback];
94  return session;
95 }
96 
97 + (void)cancel {
98 
99  /*
100  * ZERO (0) IS A SPECIAL SESSION ID
101  * WHEN IT IS PASSED TO THIS METHOD, A SIGINT IS GENERATED WHICH CANCELS ALL ONGOING SESSIONS
102  */
103  cancel_operation(0);
104 }
105 
106 + (void)cancel:(long)sessionId {
107  cancel_operation(sessionId);
108 }
109 
110 + (NSArray*)listSessions {
112 }
113 
114 + (NSArray*)parseArguments:(NSString*)command {
115  NSMutableArray *argumentArray = [[NSMutableArray alloc] init];
116  NSMutableString *currentArgument = [[NSMutableString alloc] init];
117 
118  bool singleQuoteStarted = false;
119  bool doubleQuoteStarted = false;
120 
121  for (int i = 0; i < command.length; i++) {
122  unichar previousChar;
123  if (i > 0) {
124  previousChar = [command characterAtIndex:(i - 1)];
125  } else {
126  previousChar = 0;
127  }
128  unichar currentChar = [command characterAtIndex:i];
129 
130  if (currentChar == ' ') {
131  if (singleQuoteStarted || doubleQuoteStarted) {
132  [currentArgument appendFormat: @"%C", currentChar];
133  } else if ([currentArgument length] > 0) {
134  [argumentArray addObject: currentArgument];
135  currentArgument = [[NSMutableString alloc] init];
136  }
137  } else if (currentChar == '\'' && (previousChar == 0 || previousChar != '\\')) {
138  if (singleQuoteStarted) {
139  singleQuoteStarted = false;
140  } else if (doubleQuoteStarted) {
141  [currentArgument appendFormat: @"%C", currentChar];
142  } else {
143  singleQuoteStarted = true;
144  }
145  } else if (currentChar == '\"' && (previousChar == 0 || previousChar != '\\')) {
146  if (doubleQuoteStarted) {
147  doubleQuoteStarted = false;
148  } else if (singleQuoteStarted) {
149  [currentArgument appendFormat: @"%C", currentChar];
150  } else {
151  doubleQuoteStarted = true;
152  }
153  } else {
154  [currentArgument appendFormat: @"%C", currentChar];
155  }
156  }
157 
158  if ([currentArgument length] > 0) {
159  [argumentArray addObject: currentArgument];
160  }
161 
162  return argumentArray;
163 }
164 
165 + (NSString*)argumentsToString:(NSArray*)arguments {
166  if (arguments == nil) {
167  return @"nil";
168  }
169 
170  NSMutableString *string = [NSMutableString stringWithString:@""];
171  for (int i=0; i < [arguments count]; i++) {
172  NSString *argument = [arguments objectAtIndex:i];
173  if (i > 0) {
174  [string appendString:@" "];
175  }
176  [string appendString:argument];
177  }
178 
179  return string;
180 }
181 
182 @end
void(^ ExecuteCallback)(id< Session > session)
void(^ LogCallback)(Log *log)
Definition: LogCallback.h:31
void(^ StatisticsCallback)(Statistics *statistics)
void cancel_operation(long id)
void asyncFFmpegExecute:onDispatchQueue:(FFmpegSession *ffmpegSession,[onDispatchQueue] dispatch_queue_t queue)
NSArray * getFFmpegSessions()
void asyncFFmpegExecute:(FFmpegSession *ffmpegSession)
void ffmpegExecute:(FFmpegSession *ffmpegSession)
NSArray * listSessions()
Definition: FFmpegKit.m:110
void initialize()
Definition: FFmpegKit.m:29
void cancel()
Definition: FFmpegKit.m:97
NSArray * parseArguments:(NSString *command)
Definition: FFmpegKit.m:114