如何修剪视频文件并使用iOS SDK转换为15秒视频?

时间:2013-11-27 13:59:50

标签: iphone ios5 avassetwriter avassetexportsession avasset

我想修剪一个视频文件。我想从图库中选择视频并将其转换为15秒的视频。如果我使用拾取器视图控制器进行常规修剪,它不会指定时间并只显示帧,但我需要修复15秒。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:11)

<强>目标C

-(void)cropVideo:(NSURL*)videoToTrimURL{
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoToTrimURL options:nil];
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *outputURL = paths[0];
    NSFileManager *manager = [NSFileManager defaultManager];
    [manager createDirectoryAtPath:outputURL withIntermediateDirectories:YES attributes:nil error:nil];
    outputURL = [outputURL stringByAppendingPathComponent:@"output.mp4"];
    // Remove Existing File
    [manager removeItemAtPath:outputURL error:nil];


    exportSession.outputURL = [NSURL fileURLWithPath:outputURL];
    exportSession.shouldOptimizeForNetworkUse = YES;
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;
    CMTime start = CMTimeMakeWithSeconds(1.0, 600); // you will modify time range here
    CMTime duration = CMTimeMakeWithSeconds(15.0, 600);
    CMTimeRange range = CMTimeRangeMake(start, duration);
    exportSession.timeRange = range;
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void)
     {
         switch (exportSession.status) {
             case AVAssetExportSessionStatusCompleted:
                 [self writeVideoToPhotoLibrary:[NSURL fileURLWithPath:outputURL]];
                 NSLog(@"Export Complete %d %@", exportSession.status, exportSession.error);
                 break;
             case AVAssetExportSessionStatusFailed:
                 NSLog(@"Failed:%@",exportSession.error);
                 break;
             case AVAssetExportSessionStatusCancelled:
                 NSLog(@"Canceled:%@",exportSession.error);
                 break;
             default:
                 break;
         }

         //[exportSession release];
     }];
}

在Swift 4.0中

    static func cropVideo(atURL url:URL) {
    let asset = AVURLAsset(url: url)
    let exportSession = AVAssetExportSession.init(asset: asset, presetName: AVAssetExportPresetHighestQuality)!
    var outputURL = URL(string:NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last!)

    let fileManager = FileManager.default
    do {
        try fileManager.createDirectory(at: outputURL!, withIntermediateDirectories: true, attributes: nil)
    } catch {

    }

    outputURL?.appendPathComponent("output.mp4")
    // Remove existing file
    do {
        try fileManager.removeItem(at: outputURL!)
    }
    catch {

    }

    exportSession.outputURL = outputURL
    exportSession.shouldOptimizeForNetworkUse = true
    exportSession.outputFileType = AVFileTypeQuickTimeMovie
    let start = CMTimeMakeWithSeconds(1.0, 600) // you will modify time range here
    let duration = CMTimeMakeWithSeconds(15.0, 600)
    let range = CMTimeRangeMake(start, duration)
    exportSession.timeRange = range
    exportSession.exportAsynchronously {
        switch(exportSession.status) {
        case .completed: break
            //
        case .failed: break
            //
        case .cancelled: break
            //
        default: break
        }
    }
}

答案 1 :(得分:0)

如果我们需要设置开始时间和结束时间,我上面的答案对我有所改变。

我改变了这一点:

CMTime start = CMTimeMakeWithSeconds(1.0, 600); // you will modify time range here
CMTime duration = CMTimeMakeWithSeconds(15.0, 600);
CMTimeRange range = CMTimeRangeMake(start, duration);

对此:

CMTime start = CMTimeMakeWithSeconds(self.StartTime, 600); // you will modify time range here
    CMTime duration = CMTimeSubtract(CMTimeMakeWithSeconds(self.EndTime, 600), start);
    CMTimeRange range = CMTimeRangeMake(start, duration);

它对我有用。