First of all I have to say that I love this forum, it helped me so many time. I have a problem and I couldn't find an answer to it anywhere so this is my first question here.
My problem is this:
I have a video represented by AVPlayerItem, the user can edit the video start time using the cutBefore button that cuts the video to the left of the slider
The method responsible for cutting the video is the following:
- (void)CutBeforeAction {
AVMutableComposition *composition = [AVMutableComposition composition];
// Get the audio and video tracks of the video
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
// Calculate the new duration
CMTime currStartTime = _player.currentItem.currentTime;
CMTime endTime = _player.currentItem.duration;
CMTimeRange range = CMTimeRangeFromTimeToTime(currStartTime, endTime);
// Insert the new duration to the tracks
NSError *error = nil;
[compositionVideoTrack insertTimeRange:range
ofTrack:[[_player.currentItem.asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:kCMTimeZero
error:&error];
[compositionAudioTrack insertTimeRange:range
ofTrack:[[_player.currentItem.asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
atTime:kCMTimeZero
error:&error];
// Create a new AVPlayerItem with the new composition
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:composition];
[self setPlayerItem:item];
[_player replaceCurrentItemWithPlayerItem:item];
// change the player location to the beginning of the video
[_player seekToTime:CMTimeMakeWithSeconds(0, 1)];
[self syncTimeLabel];
[self syncScrubber];
}
When running the - (void)cutBefore
method for the firs time it works fine, when I run it for the second time (the video has been already edited once) the
[compositionVideoTrack insertTimeRange:range
ofTrack:[[_player.currentItem.asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:kCMTimeZero
error:&error];
and
[compositionAudioTrack insertTimeRange:range
ofTrack:[[_player.currentItem.asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
atTime:kCMTimeZero
error:&error];
methods returns with the following error:
The operation couldn’t be completed. (OSStatus error -12780.)
I tried to look for what that error code means but found practically nothing.
Thanks for the help
I faced with this issue and problem was with my calculation for timeRange
and startTime
parameters of this method. timeRange.start.value
and startTime.value
must be positive. Maybe my late answer will help someone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With