在 iPhone 上剪切视频 [英] Cutting video on iPhone

查看:27
本文介绍了在 iPhone 上剪切视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自设备摄像头的视频文件 -- 例如,存储为/private/var/mobile/Media/DCIM/100APPLE/IMG_0203.MOV,我需要剪切该视频的前 10 秒.我可以使用哪些 API 或库?

I have a video file from the device camera -- stored as /private/var/mobile/Media/DCIM/100APPLE/IMG_0203.MOV, for example, and I need to cut the first 10 seconds of this video. What API or libraries I can use?

推荐答案

我找到了标准 API 的解决方案:AVAssetExportSession

I found solution with standard API: AVAssetExportSession

- (void)getTrimmedVideoForFile:(NSString *)filePath withInfo:(NSArray *)info
{
//[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:filePath] options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality];

// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES);
// NSString *outputURL = paths[0];
NSFileManager *manager = [NSFileManager defaultManager];
// [manager createDirectoryAtPath:outputURL withIntermediateDirectories:YES attributes:nil error:nil];
// outputURL = [outputURL stringByAppendingPathComponent:@"output.mp4"];
NSString *outputURL = [NSString stringWithFormat:@"/tmp/%@.mp4", [info objectAtIndex:2]];
NSLog(@"OUTPUT: %@", outputURL);
// Remove Existing File
// [manager removeItemAtPath:outputURL error:nil];

if (![manager fileExistsAtPath:outputURL]) {
    exportSession.outputURL = [NSURL fileURLWithPath:outputURL];
    exportSession.shouldOptimizeForNetworkUse = YES;
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;
    CMTime start = kCMTimeZero;
    CMTime duration = kCMTimeIndefinite;
    if ([[NSString stringWithFormat:@"%@", [info objectAtIndex:3]] floatValue] > 20.0) {
        start = CMTimeMakeWithSeconds(1.0, 600);
        duration = CMTimeMakeWithSeconds(10.0, 600);
    }

    CMTimeRange range = CMTimeRangeMake(start, duration);
    exportSession.timeRange = range;
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
        switch (exportSession.status) {
            case AVAssetExportSessionStatusCompleted:
                NSLog(@"Export Complete %d %@", exportSession.status, exportSession.error);
                [self sendVideoPreview:info];
                break;
            case AVAssetExportSessionStatusFailed:
                NSLog(@"Failed:%@",exportSession.error);
                // [self addToDelayed:info withAction:@"add"];
                break;
            case AVAssetExportSessionStatusCancelled:
                NSLog(@"Canceled:%@",exportSession.error);
                // [self addToDelayed:info withAction:@"add"];
                break;
            default:
                break;
        }
    }];
} else {
    [self sendVideoPreview:info];
}

这篇关于在 iPhone 上剪切视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆