Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

よくある動画アプリのあれを実装したい。

 よくある動画アプリのあれを実装したい。

Instagram, Vine, SlideStory のような動画アプリで動画を保存する仕込みが気になったので実装しました。

nakajijapan

May 28, 2014
Tweet

More Decks by nakajijapan

Other Decks in Technology

Transcript

  1. #pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureAudioDataOutputSampleBufferDelegate ! - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer

    fromConnection:(AVCaptureConnection *)connection { ! if (!CMSampleBufferDataIsReady(sampleBuffer)) { NSLog(@"sampleBuffer data is not ready"); } ! CMTime currentTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer); ! if (self.touching) { ! // Video if ([captureOutput isKindOfClass:[AVCaptureVideoDataOutput class]]) { [self addSamplebuffer:sampleBuffer withWriterInput:(AVAssetWriterInput *)self.assetWriterInputVideo]; } // Audio else if ([captureOutput isKindOfClass:[AVCaptureAudioDataOutput class]]) { [self addSamplebuffer:sampleBuffer withWriterInput:(AVAssetWriterInput *)self.assetWriterInputAudio]; } ! } ! self.recordStartTime = currentTime; } AVCaptureVideoDataOutputSampleBufferDelegate AVCaptureAudioDataOutputSampleBufferDelegate
  2. 4UBSU8SJUJOH // AVAssetWriter self.assetWriter = [AVAssetWriter assetWriterWithURL:self.outputURL fileType:AVFileTypeQuickTimeMovie error:&error]; !

    ! // movie NSDictionary *videoSetting = self.videoSettings; self.assetWriterInputVideo = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSetting]; [self.assetWriterInputVideo setExpectsMediaDataInRealTime:YES]; ! ! // audio NSDictionary *audioSetting = self.audioSettings; self.assetWriterInputAudio = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioSetting]; [self.assetWriterInputAudio setExpectsMediaDataInRealTime:YES]; ! ! [self.assetWriter addInput:self.assetWriterInputVideo]; [self.assetWriter addInput:self.assetWriterInputAudio]; ! // queue self.movieWritingQueue = dispatch_queue_create("Movie Writing Queue", DISPATCH_QUEUE_SERIAL); ! // Record [self.assetWriter startWriting]; [self.assetWriter startSessionAtSourceTime:self.recordStartTime]; 
  3. 4UBSU8SJUJOH // AVAssetWriter self.assetWriter = [AVAssetWriter assetWriterWithURL:self.outputURL fileType:AVFileTypeQuickTimeMovie error:&error]; !

    ! // movie NSDictionary *videoSetting = self.videoSettings; self.assetWriterInputVideo = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSetting]; [self.assetWriterInputVideo setExpectsMediaDataInRealTime:YES]; ! ! // audio NSDictionary *audioSetting = self.audioSettings; self.assetWriterInputAudio = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioSetting]; [self.assetWriterInputAudio setExpectsMediaDataInRealTime:YES]; ! ! [self.assetWriter addInput:self.assetWriterInputVideo]; [self.assetWriter addInput:self.assetWriterInputAudio]; ! // queue self.movieWritingQueue = dispatch_queue_create("Movie Writing Queue", DISPATCH_QUEUE_SERIAL); ! // Record [self.assetWriter startWriting]; [self.assetWriter startSessionAtSourceTime:self.recordStartTime]; 
  4. 4UBSU8SJUJOH // AVAssetWriter self.assetWriter = [AVAssetWriter assetWriterWithURL:self.outputURL fileType:AVFileTypeQuickTimeMovie error:&error]; !

    ! // movie NSDictionary *videoSetting = self.videoSettings; self.assetWriterInputVideo = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSetting]; [self.assetWriterInputVideo setExpectsMediaDataInRealTime:YES]; ! ! // audio NSDictionary *audioSetting = self.audioSettings; self.assetWriterInputAudio = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioSetting]; [self.assetWriterInputAudio setExpectsMediaDataInRealTime:YES]; ! ! [self.assetWriter addInput:self.assetWriterInputVideo]; [self.assetWriter addInput:self.assetWriterInputAudio]; ! // queue self.movieWritingQueue = dispatch_queue_create("Movie Writing Queue", DISPATCH_QUEUE_SERIAL); ! // Record [self.assetWriter startWriting]; [self.assetWriter startSessionAtSourceTime:self.recordStartTime]; 
  5. 4UBSU8SJUJOH // AVAssetWriter self.assetWriter = [AVAssetWriter assetWriterWithURL:self.outputURL fileType:AVFileTypeQuickTimeMovie error:&error]; !

    ! // movie NSDictionary *videoSetting = self.videoSettings; self.assetWriterInputVideo = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSetting]; [self.assetWriterInputVideo setExpectsMediaDataInRealTime:YES]; ! ! // audio NSDictionary *audioSetting = self.audioSettings; self.assetWriterInputAudio = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioSetting]; [self.assetWriterInputAudio setExpectsMediaDataInRealTime:YES]; ! ! [self.assetWriter addInput:self.assetWriterInputVideo]; [self.assetWriter addInput:self.assetWriterInputAudio]; ! // queue self.movieWritingQueue = dispatch_queue_create("Movie Writing Queue", DISPATCH_QUEUE_SERIAL); ! // Record [self.assetWriter startWriting]; [self.assetWriter startSessionAtSourceTime:self.recordStartTime]; 
  6. 4UBSU8SJUJOH // AVAssetWriter self.assetWriter = [AVAssetWriter assetWriterWithURL:self.outputURL fileType:AVFileTypeQuickTimeMovie error:&error]; !

    ! // movie NSDictionary *videoSetting = self.videoSettings; self.assetWriterInputVideo = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSetting]; [self.assetWriterInputVideo setExpectsMediaDataInRealTime:YES]; ! ! // audio NSDictionary *audioSetting = self.audioSettings; self.assetWriterInputAudio = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioSetting]; [self.assetWriterInputAudio setExpectsMediaDataInRealTime:YES]; ! ! [self.assetWriter addInput:self.assetWriterInputVideo]; [self.assetWriter addInput:self.assetWriterInputAudio]; ! // queue self.movieWritingQueue = dispatch_queue_create("Movie Writing Queue", DISPATCH_QUEUE_SERIAL); ! // Record [self.assetWriter startWriting]; [self.assetWriter startSessionAtSourceTime:self.recordStartTime]; 
  7. 4UBSU8SJUJOH - (void)addSamplebuffer:(CMSampleBufferRef)sampleBuffer withWriterInput:(AVAssetWriterInput *)assetWriterInput { CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer);

    ! CFRetain(sampleBuffer); CFRetain(formatDescription); dispatch_async(self.movieWritingQueue, ^{ ! if (_assetWriter.status == AVAssetWriterStatusWriting) { ! if (assetWriterInput.readyForMoreMediaData) { ! [assetWriterInput appendSampleBuffer:sampleBuffer]; ! } } ! CFRelease(sampleBuffer); CFRelease(formatDescription); }); }