|
轉(zhuǎn)自:http://flhs-wdw.blog.sohu.com/207300574.html iphone提供了AVFoundation庫來方便的操作多媒體設(shè)備,AVAssetWriter這個類可以方便的將圖像和音頻寫成一個完整的視頻文件。甚至將整個應(yīng)用的操作錄制下來,也不是什么困難的事情。 這里先說一下如何將錄像的視頻寫到指定文件中去: 首先先準(zhǔn)備好AVCaptureSession,當(dāng)錄制開始后,可以控制調(diào)用相關(guān)回調(diào)來取音視頻的每一貞數(shù)據(jù)。
- 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];
回調(diào)函數(shù): 剩下的工作就是初始化AVAssetWriter,包括音頻與視頻輸入輸出: - -(void) initVideoAudioWriter
-
- {
-
- CGSize size = CGSizeMake(480, 320);
-
-
-
-
-
- NSString *betaCompressionDirectory = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents/Movie.mp4"];
-
-
-
- NSError *error = nil;
-
-
-
- unlink([betaCompressionDirectory UTF8String]);
-
-
-
-
-
- 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");
-
-
-
-
-
- AudioChannelLayout acl;
-
- bzero( &acl, sizeof(acl));
-
- acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
-
-
-
- NSDictionary* audioOutputSettings = 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;
-
-
-
- [videoWriter addInput:audioWriterInput];
-
- [videoWriter addInput:videoWriterInput];
-
- }
這里音頻的參數(shù)一定要注意,如果添錯了,可能會沒聲音,這個問題折磨了我不少時間,這樣,當(dāng)回調(diào)函數(shù)開始執(zhí)行后,就會調(diào)用寫視頻和音頻的函數(shù),文件就被寫到指定位置去了。如果想加些濾鏡效果,只要有好的圖像處理算法,針對圖像每一幀進(jìn)行處理就可以了
|