OStack程序员社区-中国程序员成长平台

标题: ios - 带有指令的 AVAssetExportSession 使黑屏 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:07
标题: ios - 带有指令的 AVAssetExportSession 使黑屏

我正在尝试将 2 个音频文件和 1 个视频文件合并为 1 个 .mov 文件。我用下一个代码实现了它:

-(void)combineData{
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];

AVAsset *audioAsset = [AVAsset assetWithURL:_songURL];
AVAsset* audioAsset2 = [AVAsset assetWithURL:[NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[@"speechRecord" stringByAppendingPathExtension"caf"]]]];
AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[@"movie" stringByAppendingPathExtension"mov"]]] options:nil];

AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];

AVMutableCompositionTrack *secondTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[secondTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[audioAsset2 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];

AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];

AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
/*CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(90));
CGAffineTransform rotateTranslate = CGAffineTransformTranslate(rotationTransform,320,0);*/
[layerInstruction setTransform:videoTrack.preferredTransform atTime:kCMTimeZero];

AVMutableVideoCompositionInstruction * mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration);
[mainInstruction setLayerInstructions:[NSArray arrayWithObject:layerInstruction]];

mixComposition.naturalSize = videoTrack.naturalSize;

AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];
mainCompositionInst.renderScale = 1.0;
mainCompositionInst.renderSize = videoTrack.naturalSize;
mainCompositionInst.frameDuration = CMTimeMake(1, 30);
mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction];
//mainCompositionInst.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent"mergeVideo.mov"];

if ([[NSFileManager defaultManager] fileExistsAtPath:myPathDocs]) {
    [[NSFileManager defaultManager] removeItemAtPath:myPathDocs error:nil];
    NSLog(@"Removing old mergeVideo");
}

NSURL *url = [NSURL fileURLWithPath:myPathDocs];

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL=url;
exporter.outputFileType = AVFileTypeQuickTimeMovie; 
exporter.videoComposition = mainCompositionInst;
exporter.shouldOptimizeForNetworkUse = YES; 

[exporter exportAsynchronouslyWithCompletionHandler:^ 
 {
     dispatch_async(dispatch_get_main_queue(), ^{
         [self exportDidFinish:exporter];
     });
 }];

我的第一个任务是处理我的新视频 90 次旋转。当我将 mainCompositionInst 包含到我的代码中时,我的新视频有所有声音,但它有一个黑屏并且仍然没有旋转。如果我不使用任何说明,它可以正常工作。也许我的代码有错误,也可能没有。你有什么建议?



Best Answer-推荐答案


我会尝试在 preferredTransform 顶部应用旋转,因为 preferredTransform 可能会翻译您的视频,使其正确显示在屏幕上。

我会尝试以下方法:

CGAffineTransform transform = videoTrack.preferredTransform;
transform = CGAffineTransformRotate(transform, DEGREES_TO_RADIANS(90));
//transform = CGAffineTransformTranslate(transform, 320, 0);
[layerInstruction setTransform:transform atTime:kCMTimeZero];

关于ios - 带有指令的 AVAssetExportSession 使黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21235467/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4