ビデオを作成してディレクトリにエクスポートするアプリに取り組んでいます。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 での実行中にエラーが発生しました。