如何优雅的把视频转换成LivePhoto格式

2017-10-17  本文已影响0人  曾柏超

今天因为某些原因,up打算把一个视频当做手机livePhoito锁屏壁纸
然后先感谢tako这位日本小伙伴的帮助

http://gadget.takoweb.com

up也在苹果商店上看到了很多这种软件
然鹅...
想要解锁一些高级功能是要收费的!
然后up就想既然身为高贵的iOS开发者(捂脸笑)
就自己研究了一下
其实这个livePhoto锁屏壁纸 主要是依赖一个iOS11 中一个<photos.framework>中的PHPhotoLibrary 和 PHLivePhoto 以及 <AVFoundation.framework> AVAssetExportSession 这个类

--------------------------分割线----------------------

格式转换


#pragma mark - Segue
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    
    // We don't need to check. But to make sure...
    if ([identifier isEqualToString:@"next"]) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"Generating..." preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertController animated:YES completion:nil];
        
        BOOL __block ret = NO;
        
        // Generate UUID
        self.genUUID = [[NSUUID UUID] UUIDString];
        NSLog(@"genUUID: %@", self.genUUID);
        
        do {
            // Generate image.
            {
                ret = [self generateStillImage:CMTimeGetSeconds([self.videoPlayer currentTime]) url:(AVURLAsset *)self.playerItem.asset];
                if (ret == NO) break;
            }
            
            // Generate movie.
            {
                // Remove temporary files
                [self removeFile:PATH_TEMP_FILE];
                [self removeFile:PATH_MOVIE_FILE];
                
                // First, save movie to app storage.
                AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:self.playerItem.asset presetName:AVAssetExportPresetPassthrough];
                exportSession.outputFileType = [[exportSession supportedFileTypes] objectAtIndex:0];
                
                // NSLog(@"%@", [[exportSession supportedFileTypes] description]);
                
                exportSession.outputURL = [NSURL fileURLWithPath:PATH_TEMP_FILE];
                
                dispatch_semaphore_t __block semaphore = dispatch_semaphore_create(0);
                [exportSession exportAsynchronouslyWithCompletionHandler:^{
                    if (exportSession.status == AVAssetExportSessionStatusCompleted) {
                        NSLog(@"export session completed");
                    } else {
                        NSLog(@"export session error: %@", exportSession.error);
                        ret = NO;
                    }
                    dispatch_semaphore_signal(semaphore);
                }];
                dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
                if (ret == NO) break;
                
                // Second. add metadata to movie.
                QuickTimeMov *qtmov = [[QuickTimeMov alloc] initWithPath:PATH_TEMP_FILE];
                [qtmov write:PATH_MOVIE_FILE assetIdentifier:self.genUUID];
                
                ret = [[NSFileManager defaultManager] fileExistsAtPath:PATH_MOVIE_FILE];
                if (ret == NO) break;
            }
            ret = YES;
            
        } while (NO);
        
        [alertController dismissViewControllerAnimated:YES completion:nil];
        
        if (ret)
        {
            // Go to next screen.
            return YES;
        }
        else
        {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"error" preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
            [self presentViewController:alertController animated:YES completion:nil];
        }
    }
    
    return NO;
}


只有成为终身学习才能走的更远~

https://github.com/tako0910/LivePhotoMaker

上一篇下一篇

猜你喜欢

热点阅读