【问题标题】:How to trim the video file and convert to 15 seconds video with iOS SDK?如何使用 iOS SDK 修剪视频文件并转换为 15 秒视频?
【发布时间】:2013-11-27 13:59:50
【问题描述】:

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

【问题讨论】:

    标签: iphone ios5 avassetwriter avassetexportsession avasset


    【解决方案1】:

    Objective-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
            }
        }
    }
    

    【讨论】:

    • 如果我们改变时间开始和持续时间,那么有时它不会正确修剪视频。请您检查一下。
    • @ram 我的视频时长是 60 秒,我想从 15 秒到 30 秒删除
    • CMTime 开始 = CMTimeMakeWithSeconds(15, 600); // 您将修改 CMTime 持续时间 = CMTimeMakeWithSeconds(30, 600); @sohil 它会为你工作
    • @ram 我再次对你说我不想像你说的那样从头开始剪切视频,但实际上我想将视频从 15 秒剪切到 30 秒(注意:不是从头开始剪切) .
    • @ram,我正在使用此代码,但修剪后视频音频消失了可以给我任何建议如何修剪视频而不丢失音频。
    【解决方案2】:

    上面的答案对我有用,如果我们需要设置开始时间和结束时间来修剪。

    我改变了这个:

    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);
    

    它对我有用。

    【讨论】:

    • 我的视频时长是 60 秒,我想从 15 秒到 30 秒删除
    • CMTime 开始 = CMTimeMakeWithSeconds(15, 600); // 您将修改 CMTime 持续时间 = CMTimeMakeWithSeconds(30, 600); @sohil 它会为你工作
    猜你喜欢
    • 2015-09-13
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 2016-05-01
    相关资源
    最近更新 更多