轉自:http://flhs-wdw.blog.sohu.com/207300574.html html
iphone提供了AVFoundation庫來方便的操做多媒體設備,AVAssetWriter這個類能夠方便的將圖像和音頻寫成一個完整的視頻文件。甚至將整個應用的操做錄製下來,也不是什麼困難的事情。 算法
這裏先說一下如何將錄像的視頻寫到指定文件中去:
首先先準備好AVCaptureSession,當錄製開始後,能夠控制調用相關回調來取音視頻的每一貞數據。
- NSError * error;
-
- session = [[AVCaptureSession alloc] init];
-
- [session beginConfiguration];
-
- [session setSessionPreset:AVCaptureSessionPreset640x480];
-
- [self initVideoAudioWriter];
-
- AVCaptureDevice * videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
-
- AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
-
-
-
- AVCaptureDevice * audioDevice1 = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
-
- AVCaptureDeviceInput *audioInput1 = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice1 error:&error];
-
- videoOutput = [[AVCaptureVideoDataOutput alloc] init];
-
- [videoOutput setAlwaysDiscardsLateVideoFrames:YES];
-
- [videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
-
- [videoOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
-
-
-
- audioOutput = [[AVCaptureAudioDataOutput alloc] init];
-
- numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
-
- [audioOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
-
- [session addInput:videoInput];
-
- [session addInput:audioInput1];
-
- [session addOutput:videoOutput];
-
- [session addOutput:audioOutput];
-
-
-
- [session commitConfiguration];
-
- [session startRunning];
回調函數: session
剩下的工做就是初始化AVAssetWriter,包括音頻與視頻輸入輸出: app
- -(void) initVideoAudioWriter
-
- {
-
- CGSize size = CGSizeMake(480, 320);
-
-
-
-
-
- NSString *betaCompressionDirectory = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents/Movie.mp4"];
-
-
-
- NSError *error = nil;
-
-
-
- unlink([betaCompressionDirectory UTF8String]);
-
-
-
- //----initialize compression engine
-
- self.videoWriter = [[AVAssetWriter alloc] initWithURL:[NSURLfileURLWithPath:betaCompressionDirectory]
-
- fileType:AVFileTypeQuickTimeMovie
-
- error:&error];
-
- NSParameterAssert(videoWriter);
-
- if(error)
-
- NSLog(@"error = %@", [error localizedDescription]);
-
- NSDictionary *videoCompressionProps = [NSDictionary dictionaryWithObjectsAndKeys:
-
- [NSNumber numberWithDouble:128.0*1024.0],AVVideoAverageBitRateKey,
-
- nil ];
-
-
-
- NSDictionary *videoSettings = [NSDictionarydictionaryWithObjectsAndKeys:AVVideoCodecH264, AVVideoCodecKey,
-
- [NSNumber numberWithInt:size.width], AVVideoWidthKey,
-
- [NSNumber numberWithInt:size.height],AVVideoHeightKey,videoCompressionProps, AVVideoCompressionPropertiesKey, nil];
-
- self.videoWriterInput = [AVAssetWriterInputassetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
-
-
-
- NSParameterAssert(videoWriterInput);
-
-
-
- videoWriterInput.expectsMediaDataInRealTime = YES;
-
-
-
- NSDictionary *sourcePixelBufferAttributesDictionary = [NSDictionarydictionaryWithObjectsAndKeys:
-
- [NSNumbernumberWithInt:kCVPixelFormatType_32ARGB], kCVPixelBufferPixelFormatTypeKey, nil];
-
-
-
- self.adaptor = [AVAssetWriterInputPixelBufferAdaptorassetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
-
- sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];
-
- NSParameterAssert(videoWriterInput);
-
- NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
-
-
-
- if ([videoWriter canAddInput:videoWriterInput])
-
- NSLog(@"I can add this input");
-
- else
-
- NSLog(@"i can't add this input");
-
-
-
- // Add the audio input
-
- AudioChannelLayout acl;
-
- bzero( &acl, sizeof(acl));
-
- acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
-
-
-
- NSDictionary* audioOutputSettings = nil;
-
- // audioOutputSettings = [ NSDictionary dictionaryWithObjectsAndKeys:
-
- // [ NSNumber numberWithInt: kAudioFormatAppleLossless ], AVFormatIDKey,
-
- // [ NSNumber numberWithInt: 16 ], AVEncoderBitDepthHintKey,
-
- // [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
-
- // [ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey,
-
- // [ NSData dataWithBytes: &acl length: sizeof( acl ) ], AVChannelLayoutKey,
-
- // nil ];
-
- audioOutputSettings = [ NSDictionary dictionaryWithObjectsAndKeys:
-
- [ NSNumber numberWithInt: kAudioFormatMPEG4AAC ], AVFormatIDKey,
-
- [ NSNumber numberWithInt:64000], AVEncoderBitRateKey,
-
- [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
-
- [ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey,
-
- [ NSData dataWithBytes: &acl length: sizeof( acl ) ], AVChannelLayoutKey,
-
- nil ];
-
-
-
- audioWriterInput = [[AVAssetWriterInput
-
- assetWriterInputWithMediaType: AVMediaTypeAudio
-
- outputSettings: audioOutputSettings ] retain];
-
-
-
- audioWriterInput.expectsMediaDataInRealTime = YES;
-
- // add input
-
- [videoWriter addInput:audioWriterInput];
-
- [videoWriter addInput:videoWriterInput];
-
- }