gpt4 book ai didi

ios - AVVideoComposition 在 iOS 8 中不起作用

转载 作者:可可西里 更新时间:2023-11-01 05:00:29 26 4
gpt4 key购买 nike

我正在开发应用程序,我正在其中制作视频并导出到目录。它在 iOS 7 中运行良好,但在 iOS 8 中失败。

下面是我的代码:

AVAsset *pVideoTrack = [AVAsset assetWithURL:[NSURL fileURLWithPath:assetPath]];
AVVideoComposition *origionalComposition = [AVVideoComposition videoCompositionWithPropertiesOfAsset:pVideoTrack];

AVAssetTrack *clipVideoTrack = [[pVideoTrack tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, pVideoTrack.duration)
ofTrack:clipVideoTrack
atTime:kCMTimeZero error:nil];
[compositionVideoTrack setPreferredTransform:[[[pVideoTrack tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]];

BOOL bIsPotrait = [self checkVideoOrientationProperty:[[pVideoTrack tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]];

CGSize videoSize = CGSizeMake(0, 0);
if(bIsPotrait)
videoSize = CGSizeMake([[[pVideoTrack tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize].height, [[[pVideoTrack tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize].width);

else
videoSize = [[[pVideoTrack tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize];

AVMutableVideoComposition* videoComp = [AVMutableVideoComposition videoComposition];
videoComp.renderSize = videoSize;

AVAssetTrack *videoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];

[layerInstruction setTransform:clipVideoTrack.preferredTransform atTime:kCMTimeZero];

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [mixComposition duration]);

videoComp.instructions = [NSArray arrayWithObject: instruction];

videoComp.frameDuration = origionalComposition.frameDuration;
videoComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:animatedWaterMarkLayer];

_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
_assetExport.videoComposition = videoComp;

NSString *CasheDirectory = [UICommonMethods GetLibraryCacheDirectoryPath];
NSString *outputPath = [CasheDirectory stringByAppendingPathComponent:kWatermarkVideoName];
NSURL *exportUrl = [NSURL fileURLWithPath:outputPath];

if ([[NSFileManager defaultManager] fileExistsAtPath:outputPath])
[[NSFileManager defaultManager] removeItemAtPath:outputPath error:nil];

_assetExport.outputFileType = AVFileTypeQuickTimeMovie;
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = NO;

[_assetExport exportAsynchronouslyWithCompletionHandler:^
{
dispatch_async(dispatch_get_main_queue(), ^{

switch ([_assetExport status]) {
case AVAssetExportSessionStatusExporting:
NSLog(@"Export in progress ");
case AVAssetExportSessionStatusFailed:
NSLog(@"Export failed (%d): %@", [[_assetExport error] code], [[_assetExport error] localizedFailureReason]);
[_assetExport cancelExport];
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");
break;
case AVAssetExportSessionStatusCompleted:
NSLog(@"Export done");
NSLog (@"finished writing %f", [dtStartDate timeIntervalSinceNow]);
break;
case AVAssetExportSessionStatusWaiting:
NSLog (@"AVAssetExportSessionStatusWaiting");
break;
case AVAssetExportSessionStatusUnknown:
NSLog (@"AVAssetExportSessionStatusUnknown");
break;
}

});


}];

它给出“无法解码媒体数据。它可能已损坏。”在 iOS 8 上运行时出错。

最佳答案

试试这个:

AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition
presetName:AVAssetExportPresetPassthrough];

关于ios - AVVideoComposition 在 iOS 8 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25891037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com