2

我需要按帧修剪录制的视频/预先录制的视频。我有东西AVCatureSession要拿。但我无法确定如何使用第一个时间帧和第二个时间帧从选择中获取视频。这样我就可以在第一个和第二个时间帧之间获得修剪后的视频。请指导我如何实现它。运行代码将不胜感激。

4

1 回答 1

4

您可以使用 AVMutableComposition 来执行此任务与 Swift 5 兼容的代码

extension AVAsset {      
    func assetByTrimming(startTime: CMTime, endTime: CMTime) throws -> AVAsset {
        let duration = CMTimeSubtract(endTime, startTime)
        let timeRange = CMTimeRange(start: startTime, duration: duration)

        let composition = AVMutableComposition()

        do {
            for track in tracks {
                let compositionTrack = composition.addMutableTrack(withMediaType: track.mediaType, preferredTrackID: track.trackID)
                compositionTrack?.preferredTransform = track.preferredTransform
                try compositionTrack?.insertTimeRange(timeRange, of: track, at: CMTime.zero)
            }
        } catch let error {
            throw TrimError("error during composition", underlyingError: error)
        }

        return composition
    }

}

于 2021-06-25T11:18:46.680 回答