我正试图使用AVFoundation框架以正确的方式翻转视频。由于某些原因,下面的代码每次都会生成一个空白视频。
我注意到,当我不应用AVAssetExportSession的videoComposition设置时,它确实通过视频(它在照片库中可见和可播放),但它没有被翻转,因为层指令没有应用。所以我假设问题是在图层指令被传递到合成中,但它是正确设置的。我错过了什么,或者没有看到什么?
代码如下:
NSURL *assetURL = [NSURL fileURLWithPath:path];
AVAsset *movieAsset = [AVAsset assetWithURL:assetURL];
NSLog(@"Asset: %0.1f",CMTimeGetSeconds(movieAsset.duration));
NSLog(@"Asset Preferred Transform %@", NSStringFromCGAffineTransform(movieAsset.preferredTransform));
//Output Composition
AVMutableComposition *outputComposition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *videoTrack = [outputComposition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, movieAsset.duration)
ofTrack:[[movieAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:kCMTimeZero
error:nil];
[videoTrack setPreferredTransform:CGAffineTransformMakeScale(1, -1)];
AVMutableVideoCompositionLayerInstruction *videoLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
[videoLayerInstruction setTransform:CGAffineTransformMakeScale(1, -1) atTime:kCMTimeZero];
AVMutableVideoCompositionInstruction *videoInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
videoInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, movieAsset.duration);
videoInstruction.layerInstructions = [NSArray arrayWithObjects:videoLayerInstruction, nil];
AVMutableVideoComposition *outputVideoComposition = [AVMutableVideoComposition videoComposition];
outputVideoComposition.instructions = [NSArray arrayWithObjects:videoInstruction, nil];
outputVideoComposition.frameDuration = CMTimeMake(1, 30);
outputVideoComposition.renderSize = CGSizeMake(480, 640);
//Export
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:outputComposition presetName:AVAssetExportPresetHighestQuality] ;
exporter.videoComposition = outputVideoComposition;
NSString *newPath = [documentsDirectory stringByAppendingPathComponent:[self.filePath stringByAppendingString:@"-fb.mov"]];
[[NSFileManager defaultManager] removeItemAtPath:newPath error:nil];
exporter.outputURL = [NSURL fileURLWithPath:newPath];
exporter.outputFileType = AVFileTypeQuickTimeMovie;
NSLog(@"Starting export");
[exporter exportAsynchronouslyWithCompletionHandler:^(void){
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Video exported");
NSLog(@"%@", newPath);
NSURL *newAssetURL = [NSURL fileURLWithPath:newPath];
AVAsset *newMovieAsset = [AVAsset assetWithURL:newAssetURL];
NSLog(@"New Asset %@",newMovieAsset);
NSLog(@"New Asset Duration: %0.1f",CMTimeGetSeconds(newMovieAsset.duration));
NSLog(@"New Asset Preferred Transform %@", NSStringFromCGAffineTransform(newMovieAsset.preferredTransform));
UISaveVideoAtPathToSavedPhotosAlbum(newPath, nil, nil, nil);}}];
你给outputVideoComposition的渲染大小是不正确的。应该是根据iPhone屏幕
我有同样的问题,它是旋转的视频导出帧。旋转锚点位于左上角的东西。
要修复,应用另一个转换,像这样:CGAffineTransformMakeTranslation (exportingVideoWidth exportingVideoHeight);
希望这对你有帮助,如果你有任何问题请告诉我。