根据时间进行视频的裁剪

一个视频自由设置时间,并裁剪成更短时间的视频。

示例代码如下:

+ (void)videoClippingWithFilePath:(NSString *)filePath start:(CGFloat)startTime end:(CGFloat)endTime complete:(void (^)(BOOL isSuccess, NSString *videoPath, Float64 videoDuration))complete
{
    if (filePath == nil || filePath.length <= 0) {
        if (complete) {
            complete(NO, nil, 0.0);
        }
    } else {
        NSURL *url = [NSURL fileURLWithPath:filePath];
        AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
        
        // 裁剪保存路径
        NSString *fileName = filePath.lastPathComponent;
        NSString *fileType = fileName.pathExtension;
        NSString *outputFileName = [NSString stringWithFormat:@"output.%@", fileType];
        NSString *outputFilePath = [[filePath stringByReplacingOccurrencesOfString:fileName withString:@""] stringByAppendingPathComponent:outputFileName];
        NSLog(@"fileName = %@, fileType = %@, outputFilePath = %@", fileName, fileType, outputFilePath);
        NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
    
        //
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough];
        exportSession.outputURL = outputFileUrl;
        exportSession.outputFileType = AVFileTypeMPEG4;
        exportSession.shouldOptimizeForNetworkUse = YES;
        //
        CMTime start = CMTimeMakeWithSeconds(startTime, asset.duration.timescale);
        CMTime duration = CMTimeMakeWithSeconds((endTime - startTime), asset.duration.timescale);
        CMTimeRange range = CMTimeRangeMake(start, duration);
        exportSession.timeRange = range;
        //
        [exportSession exportAsynchronouslyWithCompletionHandler:^{
            switch ([exportSession status]) {
                case AVAssetExportSessionStatusFailed: {
                    NSLog(@"裁剪失败:%@", [[exportSession error] description]);
                    if (complete) {
                        complete(NO, outputFilePath, endTime);
                    }
                } break;
                case AVAssetExportSessionStatusCancelled: {
                    if (complete) {
                        complete(NO, outputFilePath, endTime);
                    }
                } break;
                case AVAssetExportSessionStatusCompleted: {
                    if (complete) {
                        complete(YES, outputFilePath, endTime);
                    }
                } break;
                default: {
                    if (complete) {
                        complete(NO, outputFilePath, endTime);
                    }
                } break;
            }
        }];
    }
}

需要导入头文件 ‘#import <AVFoundation/AVFoundation.h>’

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值