3

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

DrustZ
  • 342
  • 1
  • 5
  • 19
  • Can you loop through all the tracks in video2 to see if there's another track (maybe audio) with a longer duration for some reason? Also, which of the two durations (4.73 or 3.09) do you think is correct? – Clay Garrett Dec 18 '15 at 15:52
  • I also checked the audio's duration, which is equal to the asset's, which is the correct one(the longer one). – DrustZ Dec 18 '15 at 16:00
  • But I have no idea why it will be shorter... the process are nearly the same except for the second time I'll go to merge them... – DrustZ Dec 18 '15 at 16:01
  • I'm trying to implement a function like instagram to merge videos, how do they implement the function... using the same method(I mean , by ``AVMutableComposition``)? – DrustZ Dec 18 '15 at 16:32

1 Answers1

0

Ok After a tiring investigation, I finally got the point.
It finally prove to be related with GPUImageVideoCamera. And anyone who is also using the library may pay attention: if you only write [GPUImageMovieWriter finishRecording], it is not enough(which means, the file is not completed).
You have to write [videoCamera stopCameraCapture] to close the camera capture and then the file is closed and completed.

WEIRD...

DrustZ
  • 342
  • 1
  • 5
  • 19