更改视频格式的好方法是什么?
我正在做这样的事情来合并多个视频并使用 AVAssetExportSession 导出它们:
AVMutableComposition *mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
NSError * error = nil;
AVURLAsset *assetClip;
AVAssetTrack *clipVideoTrack;
AVAssetTrack *clipAudioTrack;
for (int i = [clipPaths count]-1; i >= 0; i--) {
assetClip = [AVURLAsset URLAssetWithURL:[clipPaths objectAtIndex:i] options:nil];
clipVideoTrack = [[assetClip tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
clipAudioTrack = [[assetClip tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[compositionVideoTrack insertTimeRange:clipVideoTrack.timeRange ofTrack:clipVideoTrack atTime:kCMTimeZero error:&error];
[compositionAudioTrack insertTimeRange:clipAudioTrack.timeRange ofTrack:clipAudioTrack atTime:kCMTimeZero error:&error];
}
NSString *cachesFolder = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingString:@"/"];
NSString *finalPath = [cachesFolder stringByAppendingPathComponent:[self.currentFileName stringByAppendingString:@".mov"]];
// Remove video file if it exists
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:finalPath]) {
[fileManager removeItemAtPath:finalPath error:&error];
}
NSURL *lastURL = [NSURL fileURLWithPath:finalPath];
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.outputURL = lastURL;
[exporter exportAsynchronouslyWithCompletionHandler:^(void){
switch(exporter.status) {
case AVAssetExportSessionStatusFailed:
NSLog(@"exporting failed");
break;
case AVAssetExportSessionStatusCompleted:
NSLog(@"exporting completed");
[[NSNotificationCenter defaultCenter] postNotificationName:@"didCompleteMovieMerging" object:nil];
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"export cancelled");
break;
}
}];
我看到你可以使用
AVMutableVideoComposition* videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.renderSize = CGSizeMake(320, 320);
videoComposition.frameDuration = CMTimeMake(1, 30);
而不是 AVAssetExportSession,但我找不到让这两者一起工作的方法。