在图像处理之后,我将它们保存为新视频.
我正在运行我的代码而没有任何错误,但相册中没有新的视频.
这是我的代码.
// VIDeo writer init- (BOol)setupAssetWriterForURL:(CMFormatDescriptionRef)formatDescription{ float bitsPerPixel; CMVIDeoDimensions dimensions = CMVIDeoFormatDescriptionGetDimensions(formatDescription); int numPixels = dimensions.wIDth * dimensions.height; int bitsPerSecond; if ( numPixels < (640 * 480) ) bitsPerPixel = 4.05; else bitsPerPixel = 11.4; bitsPerSecond = numPixels * bitsPerPixel; NSDictionary *vIDeoCompressionSettings = [NSDictionary dictionaryWithObjectsAndKeys: AVVIDeoCodecH264,AVVIDeoCodecKey,[NSNumber numberWithInteger:dimensions.wIDth],AVVIDeoWIDthKey,[NSNumber numberWithInteger:dimensions.height],AVVIDeoHeightKey,[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInteger:bitsPerSecond],AVVIDeoAverageBitRateKey,[NSNumber numberWithInteger:30],AVVIDeoMaxKeyFrameIntervalKey,nil],AVVIDeoCompressionPropertIEsKey,nil]; if ([assetWriter canApplyOutputSettings:vIDeoCompressionSettings forMediaType:AVMediaTypeVIDeo]) { assetWriterVIDeoIn = [[AVAssetWriterinput alloc] initWithMediaType:AVMediaTypeVIDeo outputSettings:vIDeoCompressionSettings]; assetWriterVIDeoIn.expectsMediaDataInRealTime = YES; assetWriterVIDeoIn.transform = [self transformFromCurrentVIDeoOrIEntationToOrIEntation:self.referenceOrIEntation]; if ([assetWriter canAddinput:assetWriterVIDeoIn]) [assetWriter addinput:assetWriterVIDeoIn]; else { NSLog(@"Couldn't add asset writer vIDeo input."); return NO; } } else { NSLog(@"Couldn't apply vIDeo output settings."); return NO; } return YES;}
阅读视频
- (voID)readMovIE:(NSURL *)url{ AVURLAsset * asset = [AVURLAsset URLAssetWithURL:url options:nil]; [asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler: ^{ dispatch_async(dispatch_get_main_queue(),^{ AVAssetTrack * vIDeoTrack = nil; NSArray * tracks = [asset tracksWithMediaType:AVMediaTypeVIDeo]; if ([tracks count] == 1) { vIDeoTrack = [tracks objectAtIndex:0]; NSError * error = nil; // _movIEReader is a member variable AVAssetReader *movIEReader = [[AVAssetReader alloc] initWithAsset:asset error:&error]; if (error) NSLog(@"_movIEReader fail!\n"); Nsstring* key = (Nsstring*)kCVPixelBufferPixelFormatTypeKey; NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; NSDictionary* vIDeoSettings = [NSDictionary dictionaryWithObject:value forKey:key]; [movIEReader addOutput:[AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:vIDeoTrack outputSettings:vIDeoSettings]]; [movIEReader startReading]; while ([movIEReader status] == AVAssetReaderStatusReading) { AVAssetReaderTrackOutput * output = [movIEReader.outputs objectAtIndex:0]; CMSampleBufferRef sampleBuffer = [output copyNextSampleBuffer]; if (sampleBuffer) { if ( !assetWriter ) { outputURL = [NSURL fileURLWithPath:[Nsstring stringWithFormat:@"%@/%llu.mov",NstemporaryDirectory(),mach_absolute_time()]]; NSError *error = nil; assetWriter = [[AVAssetWriter alloc] initWithURL:outputURL fileType:(Nsstring *)kUTTypeQuickTimeMovIE error:&error]; if (error) [self showError:error]; if (assetWriter) { CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer); [self setupAssetWriterForURL:formatDescription]; } } CVImageBufferRef imageBuffer = CMSampleBufferGetimageBuffer(sampleBuffer); CVPixelBufferLockBaseAddress(imageBuffer,0); int bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); int bufferWIDth = CVPixelBufferGetWIDth(imageBuffer); int bufferHeight = CVPixelBufferGetHeight(imageBuffer); unsigned char *pixel = (unsigned char *)CVPixelBufferGetBaseAddress(imageBuffer); for( int row = 0; row < bufferHeight; row++ ) { for( int column = 0; column < bufferWIDth; column++ ) { pixel[0] = (pixel[0]+pixel[1]+pixel[2])/3; pixel[1] = (pixel[0]+pixel[1]+pixel[2])/3; pixel[2] = (pixel[0]+pixel[1]+pixel[2])/3; pixel += 4; } } CVPixelBufferUnlockBaseAddress(imageBuffer,0); if ( assetWriter ) { [self writeSampleBuffer:sampleBuffer ofType:AVMediaTypeVIDeo]; } CFRelease(sampleBuffer); } } if (assetWriter) { [assetWriterVIDeoIn markAsFinished]; assetWriter = nil; [assetWriter finishWriting]; assetWriterVIDeoIn = nil; assetWriter = nil; [self saveMovietoCameraRoll]; } else { [self showError:[assetWriter error]]; } } }); }];}- (voID) writeSampleBuffer:(CMSampleBufferRef)sampleBuffer ofType:(Nsstring *)mediaType{ if ( assetWriter.status == AVAssetWriterStatusUnkNown ) { if ([assetWriter startWriting]) { [assetWriter startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer)]; } else { [self showError:[assetWriter error]]; } } if ( assetWriter.status == AVAssetWriterStatusWriting ) { if (mediaType == AVMediaTypeVIDeo) { if (assetWriterVIDeoIn.readyForMoreMediaData) { if (![assetWriterVIDeoIn appendSampleBuffer:sampleBuffer]) { [self showError:[assetWriter error]]; } } } }}- (voID)saveMovietoCameraRoll{ ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; [library writeVIDeoAtPathToSavedPhotosAlbum:outputURL completionBlock:^(NSURL *assetURL,NSError *error) { if (error){ [self showError:error]; NSLog(@"save fail"); } else { [self removefile:outputURL]; NSLog(@"!!!"); } }); }];}- (voID)removefile:(NSURL *)fileURL{ NSfileManager *fileManager = [NSfileManager defaultManager]; Nsstring *filePath = [fileURL path]; if ([fileManager fileExistsAtPath:filePath]) { NSError *error; BOol success = [fileManager removeItemAtPath:filePath error:&error]; if (!success) [self showError:error]; }}
有什么建议?
解决方法 Here是从图像创建电影的答案.希望你能从那里得到一些帮助.
总结以上是内存溢出为你收集整理的iOS – 逐帧读取视频文件,进行图像处理,然后另存为新的视频文件全部内容,希望文章能够帮你解决iOS – 逐帧读取视频文件,进行图像处理,然后另存为新的视频文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)