trimming video,then I send the video trimmed to android device and play,I find audio/video out of sync, the audio is several seconds behind the video. but the video can play normal on iOS device. 1.I trim video with codes like this:
- (IBAction)showTrimmedVideo:(UIButton *)sender
{
[self deleteTmpFile];
NSURL *videoFileUrl = [NSURL fileURLWithPath:self.originalVideoPath];
AVAsset *anAsset = [[AVURLAsset alloc] initWithURL:videoFileUrl options:nil];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:anAsset];
if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality]) {
self.exportSession = [[AVAssetExportSession alloc]
initWithAsset:anAsset presetName:AVAssetExportPresetHighestQuality];
// Implementation continues.
NSURL *furl = [NSURL fileURLWithPath:self.tmpVideoPath];
self.exportSession.outputURL = furl;
self.exportSession.outputFileType = AVFileTypeMPEG4;
CMTime start = CMTimeMakeWithSeconds(self.startTime, anAsset.duration.timescale);
CMTime duration = CMTimeMakeWithSeconds(self.stopTime-self.startTime, anAsset.duration.timescale);
CMTimeRange range = CMTimeRangeMake(start, duration);
self.exportSession.timeRange = range;
self.trimBtn.hidden = YES;
self.myActivityIndicator.hidden = NO;
[self.myActivityIndicator startAnimating];
[self.exportSession exportAsynchronouslyWithCompletionHandler:^{
switch ([self.exportSession status]) {
case AVAssetExportSessionStatusFailed:
NSLog(@"Export failed: %@", [[self.exportSession error] localizedDescription]);
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");
break;
default:
NSLog(@"NONE");
dispatch_async(dispatch_get_main_queue(), ^{
[self.myActivityIndicator stopAnimating];
self.myActivityIndicator.hidden = YES;
self.trimBtn.hidden = NO;
[self playMovie:self.tmpVideoPath];
});
break;
}
}];
}
}
2.I send the video trimmed to server,then android device get video from server,but they find audio/video out of sync,at first I consider of server do something wrong,so I just send video to android device with USB,the error still exist.
3.so I analyze the trimmed video by ffmpeg tools:
ffmpeg -i trimVideo.mp4
then I find trimVideo.mp4 start is a negative number.
here is what ffmpeg print:
Metadata:
major_brand : qt
minor_version : 0
compatible_brands: qt
creation_time : 2015-08-29 12:22:13
encoder : Lavf56.15.102
Duration: 00:02:21.77, start: -4.692568, bitrate: 359 kb/s
Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 24000 Hz, stereo, fltp, 69 kb/s (default)
Metadata:
creation_time : 2015-08-29 12:22:13
handler_name : Core Media Data Handler
Stream #0:1(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 512x288 [SAR 1:1 DAR 16:9], 277 kb/s, 15.16 fps, 15.17 tbr, 12136 tbn, 30.34 tbc (default)
Metadata:
creation_time : 2015-08-29 12:22:13
handler_name : Core Media Data Handler
encoder : 'avc1'
I have been puzzled by this bug for several days, I am sorry of my bad english and I really need your help,thanks.
ok,finally I find a way to solve the problem. I use SDAvassetExportSession to trim video,although the result video lengths are longer a little,but A/V can be sync in non quicktime player.