This is weird, after hours' testing I still can't figure it out.
Ok, this is what I'm going to do: merge two videos, that is to append one after another.
I shoot two videos, then have two urls. Then I created two AVURLAsset using :
AVURLAsset* video1 = [[AVURLAsset alloc]initWithURL:url1 options:options];
AVURLAsset* video2 = [[AVURLAsset alloc]initWithURL:url2 options:options];
Then I get the corresponding tracks:
AVAssetTrack *videoAsset1Track = [[video1 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetTrack *videoAsset2Track = [[video2 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
Then, I have several CMTime to compare:
CMTime endtime1 = videoAsset1Track.timeRange.duration;
CMTime endtime2 = videoAsset2Track.timeRange.duration;
CMTime starttime1 = videoAsset1Track.timeRange.start;
CMTime starttime2 = videoAsset2Track.timeRange.start;
Strange things then happen. When I Log :
NSLog(@"video 1 duration : %f , start : %f, end : %f ", CMTimeGetSeconds(video1.duration), CMTimeGetSeconds(starttime1), CMTimeGetSeconds(endtime1));
NSLog(@"video 2 duration : %f , start : %f, end : %f ", CMTimeGetSeconds(video2.duration), CMTimeGetSeconds(starttime2), CMTimeGetSeconds(endtime2));
It always comes with :
video 1 duration : 4.738333 , start : 0.000000, end : 4.738333
video 2 duration : 4.736871 , start : 0.000000, end : 3.090011
Video 1 is always ok, while video 2: track's duration < asset's duration.
By the way, I'm using GPUImageVideoCamera
and GPUImageMovieWriter
(But I don't think it matters, for video 1 is always ok) to shooting videos and merge.
Edit
God I have another super weird observation:
Whenever I shoot the video, the duration of the track is always shorter than the asset's.
However if I check it for the second time, all will be ok... That's...
What do I do during the two check times? I just use an AVPlayer
to play the url...
Edit Again
Guys this is super super weird!!! I just play the url, and world becomes better. I don't know why, but at least find a way out...(No, this way sometimes still doesn't work...)