AVFoundation AVAssetExportSession在尝试使用图层指令翻转视频时产生空白视频
问题描述:
我试图用AVFoundation框架正确地翻转视频。出于某种原因,下面的代码每次都会生成一个空白视频。AVFoundation AVAssetExportSession在尝试使用图层指令翻转视频时产生空白视频
我注意到,当我不应用AVAssetExportSession的videoComposition设置时,它确实通过视频(它在Photo LIbrary中可见和可播放),但由于未应用层指令,因此不会翻转。所以我认为这个问题出现在层指令被传递到构图中,但它被正确设置。我错过了什么,或没有看到?
下面是代码:
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);
希望这可以帮助,让我知道如果您有任何问题。