i_Intiution
i_Intiution

Reputation: 238

How can we get the trimmed video by time frames in iOS?

I have a requirement to trim a recorded video/prerecorded video by frames. I got stuff for AVCatureSession to get it. But I am not able to identify how take first time frame and second time frame to get the video from the selection. So that I can get the trimmed video between first and second time frame. Please guide me how to achieve it. Running code would be highly appreciated.

Upvotes: 2

Views: 210

Answers (1)

Mada Doux
Mada Doux

Reputation: 81

You can use AVMutableComposition to do this task Code compatible with 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
    }

}

Upvotes: 4

Related Questions