-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathScreenRecorder.m
151 lines (127 loc) · 4.87 KB
/
ScreenRecorder.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
//
// ScreenRecorder.m
// mac-screen-recorder
//
// Created by Daniel Dixon on 3/19/10.
//
#import "ScreenRecorder.h"
@implementation ScreenRecorder
-(id)init
{
return [self init:YES];
}
-(id)init:(BOOL)captureAudio
{
if (self = [super init]) {
// If an error occurs here, send a [self release] message and return nil.
BOOL success = YES;
NSError *error = nil;
// create a capture session
mCaptureSession = [[QTCaptureSession alloc] init];
// attach the screen input to the capture session
mCaptureScreenInput = [[NSClassFromString(@"QTCaptureScreenInput") alloc] init];
success = [mCaptureSession addInput:mCaptureScreenInput error:&error];
if (!success) {
NSLog(@"Could not attach the screen input to the capture session!");
[self release];
return nil;
}
if(error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
if (captureAudio) {
// grab the default audio device
QTCaptureDevice *audioDevice = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeSound];
success = [audioDevice open:&error];
if(!success) {
NSLog(@"Could not start the audio device!");
[self release];
return nil;
}
// attach the audio input to the capture session
mCaptureAudioDeviceInput = [[QTCaptureDeviceInput alloc] initWithDevice:audioDevice];
success = [mCaptureSession addInput:mCaptureAudioDeviceInput error:&error];
if (!success) {
NSLog(@"Could not attach the audio device to the capture session!");
[self release];
return nil;
}
}
if(error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
// specify the output file
mCaptureMovieFileOutput = [[QTCaptureMovieFileOutput alloc] init];
success = [mCaptureSession addOutput:mCaptureMovieFileOutput error:&error];
if(!success) {
NSLog(@"Could not obtain a file output the capture session!");
[self release];
return nil;
}
if(error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
// set this class as a delegate for the output file
[mCaptureMovieFileOutput setDelegate:self];
// specify compression by grabbing all the "connections" to the output file, looking at their type, and setting options
NSEnumerator *connectionEnumator = [[mCaptureMovieFileOutput connections] objectEnumerator];
QTCaptureConnection *connection;
while ((connection = [connectionEnumator nextObject])) {
NSString *mediaType = [connection mediaType];
QTCompressionOptions *compressionOptions = nil;
if([mediaType isEqualToString:QTMediaTypeVideo]) {
//NSLog(@"Setting video compression.");
// QTCompressionOptionsLosslessAnimationVideo produces HUGE files
// according to this page, custom options are not available: http://developer.apple.com/mac/library/qa/qa2008/qa1586.html (see bottom)
// supposedly a way around it: http://osdir.com/ml/quicktime-api/2009-06/msg00122.html (older than the prior page)
compressionOptions = [QTCompressionOptions compressionOptionsWithIdentifier:@"QTCompressionOptionsSD480SizeH264Video"]; //QTCompressionOptionsLosslessAnimationVideo"];
} else if ([mediaType isEqualToString:QTMediaTypeSound]) {
//NSLog(@"Setting audio compression.");
compressionOptions = [QTCompressionOptions compressionOptionsWithIdentifier:@"QTCompressionOptionsVoiceQualityAACAudio"]; //QTCompressionOptionsHighQualityAACAudio"];
} else {
NSLog(@"Found an odd media connection type.");
}
[mCaptureMovieFileOutput setCompressionOptions:compressionOptions forConnection:connection];
}
// unfortunately this increasing the file size dramatically for no reason:
//[mCaptureMovieFileOutput setMinimumVideoFrameInterval:(1.0/15.0)]; // consider denominator the framerate
return self;
} else {
[self release];
return nil;
}
}
-(void)startRecording:(NSString*)file
{
// tell capture session to start reading inputs and sending to outputs
[mCaptureSession startRunning];
// tell file output where to save to
[mCaptureMovieFileOutput recordToOutputFileURL:[NSURL fileURLWithPath:file]];
}
-(void)stopRecording
{
// tell file output to stop recording
[mCaptureMovieFileOutput recordToOutputFileURL:nil];
// tell capture session to stop reading input and sending to outputs
[mCaptureSession stopRunning];
}
-(void)quit {
[self stopRecording];
if([[mCaptureAudioDeviceInput device] isOpen])
[[mCaptureAudioDeviceInput device] close];
}
-(void)captureOutput:(QTCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL forConnections:(NSArray *)connections dueToError:(NSError *)error
{
NSLog(@"QTCaptureFileOutput has stopped recording!");
//NSLog(@"%@", outputFileURL);
// [[NSWorkspace sharedWorkspace] openURL:outputFileURL]; // opens file in Quicktime Player
}
-(void)dealloc
{
[mCaptureSession release];
[mCaptureAudioDeviceInput release];
[mCaptureMovieFileOutput release];
[mCaptureScreenInput release];
[super dealloc];
}
@end