iOS 8 相冊視頻導(dǎo)出

1. 資源獲取

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info 在這個方法中info獲取中獲取視頻URL。
不允許編輯的情況:使用UIImagePickerControllerReferenceURL
允許編輯的情況:獲取UIImagePickerControllerMediaURL,這是編輯過的視頻數(shù)據(jù),存放于tmp目錄中,可直接通過[[NSData alloc] initWithContentsOfURL:url];獲取數(shù)據(jù),數(shù)據(jù)量大的情況,不應(yīng)該讀到內(nèi)存中。在某些情況下,編輯失敗UIImagePickerControllerMediaURL為空。依然通過UIImagePickerControllerReferenceURL獲取。

2. 導(dǎo)出視頻原始數(shù)據(jù)

即通過PHAsset方式導(dǎo)出。

PHFetchResult * fetchResult = [PHAsset fetchAssetsWithALAssetURLs: @[url] options: nil];
PHAsset * phasset = fetchResult.firstObject;

[[PHImageManager defaultManager] requestAVAssetForVideo:phasset options:nil resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
        
        if (asset) {
            AVURLAsset *a = (AVURLAsset *)asset;
            NSData *data = [NSData dataWithContentsOfURL:a.URL];
            NSLog(@"%ul",data.length);
        }
        
}];

上述方法為異步導(dǎo)出。如果需要為同步:


dispatch_semaphore_t    semaphore = dispatch_semaphore_create(0);

PHVideoRequestOptions *option = [PHVideoRequestOptions new];
__block AVAsset *resultAsset;

[[PHImageManager defaultManager] requestAVAssetForVideo:videoAsset options:option resultHandler:^(AVAsset * avasset, AVAudioMix * audioMix, NSDictionary * info) {
    resultAsset = avasset;
    dispatch_semaphore_signal(semaphore);
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

3. 自定義導(dǎo)出

- (void)CropVideo:(NSURL *)videoFileUrl withStartTime:(CGFloat)startTime stopTime:(CGFloat)stopTime outPutUrl:(NSURL *)ouputUrl finishBlock:(void(^)(BOOL))block{
    
    AVAsset *anAsset = [[AVURLAsset alloc] initWithURL:videoFileUrl options:nil];
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:anAsset];
    
    NSString *present;
    if ([compatiblePresets containsObject:AVAssetExportPreset640x480]) {
        present = AVAssetExportPreset640x480;
    }else{
        //get medium preset
        present = compatiblePresets[compatiblePresets.count/2];
        
    }
    
    NSLog(@"preset %@",present);
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
                                           initWithAsset:anAsset presetName:present];
    CMTime start = CMTimeMakeWithSeconds(startTime, anAsset.duration.timescale);
    CMTime duration = CMTimeMakeWithSeconds(stopTime-startTime, anAsset.duration.timescale);
    CMTimeRange range = CMTimeRangeMake(start, duration);
    exportSession.timeRange = range;
    exportSession.shouldOptimizeForNetworkUse = YES;
    exportSession.fileLengthLimit = range.duration.value/range.duration.timescale*1024*80;
    exportSession.outputURL = ouputUrl;
    [[NSFileManager defaultManager] removeItemAtURL:ouputUrl error:nil];
    exportSession.outputFileType = AVFileTypeMPEG4;
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        
        
        BOOL flag = NO;
        switch ([exportSession status]) {
            case AVAssetExportSessionStatusFailed:
            case AVAssetExportSessionStatusCancelled:
                
                flag = NO;
                break;
            default:
                flag = YES;
                break;
        }
        
        if (block) {
            block(flag);
        }
        if (flag) {
            NSLog(@"export success to url:%@",ouputUrl);
        }else{
            
            NSLog(@"export failed");
        }
        
    }];
    
    
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容