capture screen activity to a movie file using AV Foundation on OS X 10.7 Lion and later
来源:互联网 发布:黑马程序员java视频 编辑:程序博客网 时间:2024/06/05 00:35
How to capture screen activity to a movie file using AV Foundation on OS X 10.7 Lion and later
Q: How do I capture screen activity to a QuickTime movie on OS X 10.7 Lion and later?
A: On OS X 10.7 Lion and later, the way to make a movie of screen activity is to use AV Foundation.
Important: AV Foundation grabs the screen repeatedly. If you want to capture a static image instead of a real-time movie, a more efficient way is to call CGDisplayCreateImage
orCGDisplayCreateImageForRect
, for example:
// Create an image from the entire main display
CGImageRef image = CGDisplayCreateImage(kCGDirectMainDisplay);
See How to take an image snapshot of the screen on Mac OS X Lion for more information.
To perform a real-time screen capture and save it to a QuickTime movie file, you need minimally three AV objects:
An
AVCaptureSession
object, which coordinates the flow of data from AV input sources to outputs.An
AVCaptureScreenInput
object, which is the input data source that provides video data from the given display.An
AVCaptureMovieFileOutput
object, which is the output destination for you to write captured media data to a QuickTime movie file.
Note: By default, AVCaptureScreenInput
captures the entire screen. You may set its cropRect
property to limit the capture rectangle to a subsection of the screen.
In the following example, the code creates a capture session, adds a screen input to provide video frames, adds an output destination to save captured frames, starts the flow of data from the input to the output, and stops the flow in 5 seconds. By letting the class conform to the AVCaptureFileOutputRecordingDelegate
protocol and setting the recording delegate properly, you can monitor when the recording is finished via the delegate method.
Note: To capture both a main and secondary display at the same time, use two active capture sessions, one for each display. On OS X, AVCaptureMovieFileOutput
only supports writing to a single video track.
AVCaptureFileOutputRecordingDelegate
protocol
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface Recorder : NSObject <AVCaptureFileOutputRecordingDelegate> {
@private
AVCaptureSession *mSession;
AVCaptureMovieFileOutput *mMovieFileOutput;
NSTimer *mTimer;
}
-(void)screenRecording:(NSURL *)destPath;
@end
Screen recording example
-(void)screenRecording:(NSURL *)destPath
{
// Create a capture session
mSession = [[AVCaptureSession alloc] init];
// Set the session preset as you wish
mSession.sessionPreset = AVCaptureSessionPresetMedium;
// If you're on a multi-display system and you want to capture a secondary display,
// you can call CGGetActiveDisplayList() to get the list of all active displays.
// For this example, we just specify the main display.
// To capture both a main and secondary display at the same time, use two active
// capture sessions, one for each display. On Mac OS X, AVCaptureMovieFileOutput
// only supports writing to a single video track.
CGDirectDisplayID displayId = kCGDirectMainDisplay;
// Create a ScreenInput with the display and add it to the session
AVCaptureScreenInput *input = [[[AVCaptureScreenInput alloc] initWithDisplayID:displayId] autorelease];
if (!input) {
[mSession release];
mSession = nil;
return;
}
if ([mSession canAddInput:input])
[mSession addInput:input];
// Create a MovieFileOutput and add it to the session
mMovieFileOutput = [[[AVCaptureMovieFileOutput alloc] init] autorelease];
if ([mSession canAddOutput:mMovieFileOutput])
[mSession addOutput:mMovieFileOutput];
// Start running the session
[mSession startRunning];
// Delete any existing movie file first
if ([[NSFileManager defaultManager] fileExistsAtPath:[destPath path]])
{
NSError *err;
if (![[NSFileManager defaultManager] removeItemAtPath:[destPath path] error:&err])
{
NSLog(@"Error deleting existing movie %@",[err localizedDescription]);
}
}
// Start recording to the destination movie file
// The destination path is assumed to end with ".mov", for example, @"/users/master/desktop/capture.mov"
// Set the recording delegate to self
[mMovieFileOutput startRecordingToOutputFileURL:destPath recordingDelegate:self];
// Fire a timer in 5 seconds
mTimer = [[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(finishRecord:) userInfo:nil repeats:NO] retain];
}
-(void)finishRecord:(NSTimer *)timer
{
// Stop recording to the destination movie file
[mMovieFileOutput stopRecording];
[mTimer release];
mTimer = nil;
}
// AVCaptureFileOutputRecordingDelegate methods
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
{
NSLog(@"Did finish recording to %@ due to error %@", [outputFileURL description], [error description]);
// Stop running the session
[mSession stopRunning];
// Release the session
[mSession release];
mSession = nil;
}
Document Revision History
Added additional notes about capturing the main and secondary displays at the same time. Other miscellaneous changes.
2011-08-10Corrected a link.
2011-05-13New document that shows how to capture screen activity to a movie file using AV Foundation on OS X 10.7 Lion and later.
- capture screen activity to a movie file using AV Foundation on OS X 10.7 Lion and later
- How to capture video frames from the camera as images using AV Foundation on iOS
- ios学习--How to capture video frames from the camera as images using AV Foundation
- How to capture video frames from the camera as images using AV Foundation
- Install Nginx and PHP on OS X Mountain Lion (10.8)
- How to Virtualize OS X Lion on Windows
- Installing JDeveloper on Mac OS X Lion (10.7)
- Installing RVM on OS X Lion
- Install wget on OS X Lion
- screen capture using CGDisplayStreamCreateWithDispatchQueue
- How To Capture Data Packets On A Network Using Wireshark (a. k. a. Ethereal)
- Multithreading on iOS And Mac OS X Using NSOperations
- Copy file or folder path to the clipboard in Mac OS X Lion
- Using "RDP Encoder Mirror Driver" to Capture Screen
- Windows Dev Intro - Using a Capture Device for Media Foundation
- Hotkey of locking screen on OS X
- Capture a Screen Shot
- Mac OS X v10.5, 10.6: How to capture a packet trace
- Python+OpenCV学习(12)---特征匹配
- 一些摄像机标定的Matlab工具箱(含Kinect和激光)
- UI 第六节 uisegmentedControl uiimage uislider 课后习题
- NSScanner: nil string argument
- Dimensionality Reduction(学习Free Mind知识整理)
- capture screen activity to a movie file using AV Foundation on OS X 10.7 Lion and later
- mybatis调用存储过程
- URLConnection/Android Stutio中HttpClient的添加jar包方法和DoGet、DoPost的用法
- 自定义MITK PlanarFigure及困惑
- 宏定义以及例子
- hdu2824 The Euler function
- [疑似官泄] 某Ri牌示波器固件破解
- Exbox上的相关链接
- hdu5446