-
Notifications
You must be signed in to change notification settings - Fork 2
/
MovieCapturer.m
executable file
·355 lines (250 loc) · 8.79 KB
/
MovieCapturer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Most movie code adapted or taken from http://developer.apple.com/samplecode/QTKitCreateMovie/index.html
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#import "MovieCapturer.h"
#import "FileUtils.h"
#import "QTMovieExtensions.h"
#import "StillCapturer.h"
@interface MovieCapturer (Private)
- (void)destroyTimer;
- (void)addImageToMovie:(NSImage *)image;
- (void)saveToDesktop;
- (BOOL)writeToPath:(NSString *)path ofType:(NSString *)typeName forSaveOperation:(NSSaveOperationType)saveOperation error:(NSError **)outError;
- (void)buildQTKitMovie;
-(Movie)quicktimeMovieFromTempFile:(DataHandler *)outDataHandler error:(OSErr *)outErr;
@end
@implementation MovieCapturer (Private)
- (void)destroyTimer {
[captureTimer invalidate];
[captureTimer release];
captureTimer = nil;
}
- (void)addImageToMovie:(NSImage *)image {
if (nil == mMovie) [self buildQTKitMovie];
if ((nil == mMovie) || (nil == image)) return;
if (nil == startTime) startTime = [[NSDate date] retain];
[mMovie CMSAddImage:image];
framesCaptured++;
if (fabs([startTime timeIntervalSinceNow]) >= autoStopAfterSeconds) {
[self stopCapturingMovie];
}
}
- (void)saveToDesktop {
if (nil == mMovie) return;
NSString * path = [NSLocalizedString(@"UsersDesktop", @"~/Desktop/") stringByExpandingTildeInPath];
NSString * capturePrefix = @"CM Movie";
NSString * newPath = [NSString stringWithFormat:@"%@.mov", [FileUtils incrementalNameForDirectoryPath:path andPrefix:capturePrefix]];
if (nil == newPath) return;
[self writeToPath:newPath
ofType: nil
forSaveOperation: NSSaveOperation
error:nil];
}
// Write to a movie file
// Most of this method is from Apple.
- (BOOL)writeToPath:(NSString *)path ofType:(NSString *)typeName
forSaveOperation:(NSSaveOperationType)saveOperation error:(NSError **)outError {
BOOL success = NO;
switch (saveOperation) {
case NSSaveOperation:
{
success = [mMovie flattenToFilePath:path];
// movie file does not exist, so we'll flatten our in-memory movie to the file
// release our old in-memory movie
[mMovie release];
mMovie = nil;
// re-acquire movie from the new movie file
mMovie = [QTMovie movieWithFile:path error:nil];
[mMovie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];
[mMovie retain];
}
break;
case NSSaveAsOperation:
case NSSaveToOperation:
// flatten movie (make it self-contained)
success = [mMovie flattenToFilePath: path];
break;
}
return success;
}
// Build a QTKit movie from a series of image frames
// Most of this method is from Apple.
- (void)buildQTKitMovie {
/*
NOTES ABOUT CREATING A NEW ("EMPTY") MOVIE AND ADDING IMAGE FRAMES TO IT
In order to compose a new movie from a series of image frames with QTKit
it is of course necessary to first create an "empty" movie to which these
frames can be added. Actually, the real requirements (in QuickTime terminology)
for such an "empty" movie are that it contain a writable data reference. A
movie with a writable data reference can then accept the addition of image
frames via the -addImage method.
A future version of QTKit will provide a simple Obj C method for creating
a QTMovie with a writable data reference.
In the meantime, we can use the native QuickTime API CreateMovieStorage to create
a QuickTime movie with a writable data reference (in our example below we use a
data reference to a file). We then use the QTKit movieWithQuickTimeMovie: method to
instantiate a QTMovie from this QuickTime movie.
Finally, images are added to the movie as movie frames using -addImage.
*/
OSErr err;
// create a QuickTime movie
Movie qtMovie = [self quicktimeMovieFromTempFile:&mDataHandlerRef error:&err];
if (nil == qtMovie) return;
// instantiate a QTMovie from our QuickTime movie
mMovie = [QTMovie movieWithQuickTimeMovie:qtMovie disposeWhenDone:YES error:nil];
if (!mMovie || err) return;
// mark the movie as editable
[mMovie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];
// keep it around until we are done with it...
[mMovie retain];
//new
GWorldPtr gworld = NULL;
Rect bounds = { 0, 0, 100, 100 };
err = QTNewGWorld( &gworld, 0, &bounds, NULL, NULL, 0 );
if( err ) NSLog(@"QTNewGWorld probelm");
SetMovieGWorld( qtMovie, gworld, NULL );
}
// Creates a QuickTime movie file from a temporary file
// Most of this method is from Apple.
-(Movie)quicktimeMovieFromTempFile:(DataHandler *)outDataHandler error:(OSErr *)outErr {
*outErr = -1;
// generate a name for our movie file
NSString *tempName = [NSString stringWithCString:tmpnam(nil)
encoding:[NSString defaultCStringEncoding]];
if (nil == tempName) return nil;
Handle dataRefH = nil;
OSType dataRefType;
// create a file data reference for our movie
*outErr = QTNewDataReferenceFromFullPathCFString((CFStringRef)tempName,
kQTNativeDefaultPathStyle,
0,
&dataRefH,
&dataRefType);
if (*outErr != noErr) return nil;
// create a QuickTime movie from our file data reference
Movie qtMovie = nil;
CreateMovieStorage (dataRefH,
dataRefType,
'TVOD',
smSystemScript,
newMovieActive,
outDataHandler,
&qtMovie);
*outErr = GetMoviesError();
if (*outErr != noErr) {
DisposeHandle(dataRefH);
return nil;
}
return qtMovie;
}
@end
@implementation MovieCapturer
- (id)init {
self = [super init];
if (self) {
mMovie = nil;
mDataHandlerRef = nil;
stillCapturer = nil;
framesPerSecond = 1;
autoStopAfterFrames = 60;
autoStopAfterSeconds = 10;
captureTimer = nil;
startTime = nil;
isRecording = NO;
}
return self;
}
- (void)dealloc {
[mMovie release];
[stillCapturer release];
[self destroyTimer];
[startTime release];
if (mDataHandlerRef) CloseMovieStorage(mDataHandlerRef);
[super dealloc];
}
- (NSTimeInterval)timerInterval {
return 1.0 / (float)framesPerSecond;
}
- (void)createTimer {
[self destroyTimer];
captureTimer = [[NSTimer scheduledTimerWithTimeInterval:[self timerInterval]
target:self
selector:@selector(timerUpdate:)
userInfo:nil
repeats:YES] retain];
}
- (void)timerUpdate:(NSTimer*)theTimer {
[stillCapturer captureScreenForRect:captureRect];
[self addImageToMovie:[stillCapturer capturedImage]];
}
- (void)setAutoStopTime:(int)seconds {
int s = seconds;
if (s < 1) s = 1;
if (s > 60) s = 60;
autoStopAfterSeconds = seconds;
autoStopAfterFrames = s * framesPerSecond;
}
- (void)startCapturingMovieForRect:(NSRect)rect {
[startTime release];
startTime = nil;
isRecording = YES;
framesCaptured = 0;
if (nil == stillCapturer) stillCapturer = [[StillCapturer alloc] init];
[mMovie release]; mMovie = nil;
captureRect = rect;
[self createTimer];
}
- (void)stopCapturingMovie {
if (!isRecording) return;
[self destroyTimer];
double time = fabs([startTime timeIntervalSinceNow]);
NSLog(@"captured %i frames in %f seconds.", framesCaptured, time);
long timeElapsed = (long)time;
NSNumber *scale = [mMovie attributeForKey:QTMovieTimeScaleAttribute];
// set the duration
[mMovie scaleSegment:QTMakeTimeRange(QTZeroTime, [mMovie duration])
newDuration:QTMakeTime(timeElapsed * [scale longValue], [scale longValue])];
[self saveToDesktop];
isRecording = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:@"CMSStoppedRecording" object:@""];
}
- (BOOL)isRecording {
return isRecording;
}
- (int)framesPerSecond {
return framesPerSecond;
}
- (void)setFramesPerSecond:(int)fps {
if (isRecording) return;
framesPerSecond = fps;
if (framesPerSecond < 1) framesPerSecond = 1;
if (framesPerSecond > 30) framesPerSecond = 30;
}
- (void)setMovie:(QTMovie *)movie {
[movie retain];
[mMovie release];
mMovie = movie;
}
-(QTMovie *)movie { return mMovie; }
// Most of this method is from Apple.
- (BOOL)readFromURL:(NSURL *)url ofType:(NSString *)type error:(NSError **)outError {
BOOL success = NO;
// read the movie
if ([QTMovie canInitWithURL:url]) {
[self setMovie:((QTMovie *)[QTMovie movieWithURL:url error:nil])];
success = (mMovie != nil);
}
return success;
}
@end