从文档目录中获取已保存的视频以在iOS中查看

时间:2015-03-16 07:29:04

标签: ios uiview

我生成视频并保存到CameraRoll,在这里我需要将视频提取到我生成并保存在相机胶卷中的下一个视图。希望有人帮忙。我没有得到正确的视频关注。实际上,我使用此代码创建和保存视频

NSError *error = nil;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
NSString *videoOutputPath = [documentsDirectory        stringByAppendingPathComponent:@"test_output.mp4"];
if ([fileMgr removeItemAtPath:videoOutputPath error:&error] != YES)
NSLog(@"Unable to delete file: %@", [error localizedDescription]);
CGSize imageSize = CGSizeMake(400, 200);
NSUInteger fps = 30;
NSArray* imagePaths = [[NSBundle mainBundle]   pathsForResourcesOfType:@"jpg" inDirectory:nil];
self.chosenImages = [[NSMutableArray alloc]   initWithCapacity:imagePaths.count];
NSLog(@"-->imageArray.count= %i", self.chosenImages.count);
for (NSString* path in imagePaths)
{
[self.chosenImages addObject:[UIImage imageWithContentsOfFile:path]];

}

NSLog(@"Start building video from defined frames.");

AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
[NSURL fileURLWithPath:videoOutputPath]  fileType:AVFileTypeQuickTimeMovie
                                                      error:&error];
NSParameterAssert(videoWriter);

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                           AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:imageSize.width],   AVVideoWidthKey,
[NSNumber numberWithInt:imageSize.height], AVVideoHeightKey,
                           nil];

AVAssetWriterInput* videoWriterInput = [AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
                                    outputSettings:videoSettings];
AVAssetWriterInputPixelBufferAdaptor *adaptor =     [AVAssetWriterInputPixelBufferAdapto assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput sourcePixelBufferAttributes:nil];
NSParameterAssert(videoWriterInput);
NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
videoWriterInput.expectsMediaDataInRealTime = YES;
[videoWriter addInput:videoWriterInput];
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];

CVPixelBufferRef buffer = NULL;
int frameCount = 0;
double numberOfSecondsPerFrame = 6;
double frameDuration = fps * numberOfSecondsPerFrame;
for(UIImage * img in self.chosenImages)
{
//UIImage * img = frm._imageFrame;
 buffer = [self pixelBufferFromCGImage:[img CGImage]];
 BOOL append_ok = NO;
 int j = 0;
 while (!append_ok && j < 30) {
    if (adaptor.assetWriterInput.readyForMoreMediaData)  {
        //print out status:
        NSLog(@"Processing video frame (%d,%lu)",frameCount,(unsigned long)   [self.chosenImages count]);

        CMTime frameTime = CMTimeMake(frameCount*frameDuration,(int32_t) fps);
        append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
        if(!append_ok){
            NSError *error = videoWriter.error;
            if(error!=nil) {
                NSLog(@"Unresolved error %@,%@.", error, [error userInfo]);
            }
        }
    }
    else {
        printf("adaptor not ready %d, %d\n", frameCount, j);
        [NSThread sleepForTimeInterval:0.1];
    }
    j++;
}
if (!append_ok) {
    printf("error appending image %d times %d\n, with error.", frameCount, j);
}
frameCount++;
}
 NSLog(@"**************************************************");

  //Finish the session:
 [videoWriterInput markAsFinished];
 [videoWriter finishWriting];
 NSLog(@"Write Ended");

AVMutableComposition* mixComposition = [AVMutableComposition composition];

NSString *bundleDirectory = [[NSBundle mainBundle] bundlePath];
// audio input file...
NSString *audio_inputFilePath = [bundleDirectory   stringByAppendingPathComponent:@"30secs.mp3"];
NSURL    *audio_inputFileUrl = [NSURL fileURLWithPath:audio_inputFilePath];
NSURL    *video_inputFileUrl = [NSURL fileURLWithPath:videoOutputPath];
NSString *outputFilePath = [documentsDirectory stringByAppendingPathComponent:@"final_video.mp4"];
NSURL    *outputFileUrl = [NSURL fileURLWithPath:outputFilePath];

if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath])
[[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];

CMTime nextClipStartTime = kCMTimeZero;

AVURLAsset* videoAsset = [[AVURLAsset  alloc]initWithURL:video_inputFileUrl options:nil];
CMTimeRange video_timeRange =    CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition   addMutableTrackWithMediaType:AVMediaTypeVideo     preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:      [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]   atTime:nextClipStartTime error:nil];



AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl  options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil];
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
//_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputFileType = @"public.mpeg-4";
//NSLog(@"support file types= %@", [_assetExport supportedFileTypes]);
_assetExport.outputURL = outputFileUrl;

[_assetExport exportAsynchronouslyWithCompletionHandler:
 ^(void ) {
   ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
     [assetLibrary writeVideoAtPathToSavedPhotosAlbum:outputFileUrl completionBlock:^(NSURL *assetURL, NSError *error){
         if(error == nil){
             NSLog(@"Saved Successfully");
         }

 }
 ];

  NSLog(@"DONE.....outputFilePath--->%@", outputFilePath); 

0 个答案:

没有答案