5

I'm working on App in which I'm composing video and export to directory. It works fine in iOS 7, but fail in iOS 8.

Below is my code:

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;
         }

     });


 }];

It gives "The media data could not be decoded. It may be damaged." error while running on iOS 8.

TJ-
  • 14,085
  • 12
  • 59
  • 90
AltafBangash
  • 129
  • 3
  • 10
  • I've not read your code but our app does do composition and it works fine under iOS8. I know that doesn't help you much, but at least you know it isn't 8's fault. – Paul Cezanne Sep 17 '14 at 13:27
  • @PaulCezanne: can you post code of composition – AltafBangash Sep 18 '14 at 04:46
  • Probably not. It is a huge amount of code and also proprietary. If I can extract a safe snippet I will. But right now I doubt it. – Paul Cezanne Sep 18 '14 at 11:26
  • @AltafBangash Are you running this code in the simulator? I was getting this same error exporting a video composition in the iOS 8 simulator, but it seems to work fine on a device. – Tim Sep 19 '14 at 08:16
  • @Tim Yes I'm running on simulator. Ok I'll try on device but first I have to install iOS 8 on device. Thanks a lot dear – AltafBangash Sep 19 '14 at 10:16
  • @Tim can you post a link to the video at assetPath. Also is this video generated using AVAssetWriter? I am seeing the same issue and believe there is something in the software simulator decoder that might be asserting for something not required by the hardware decoder. – jfeldman Sep 19 '14 at 19:49
  • Sorry @AltafBangash my previous comment was meant for you – jfeldman Sep 22 '14 at 17:48
  • While I do not see that error message, I do experience a hang when using a video composition with certain movies on the simulator for Xcode 6/iOS 8. I wrote a blog post about this and posted a bug to Apple. http://finalize.com/2014/10/18/using-a-video-composition-to-process-certain-videos-causes-an-xcode-simulator-hang/ This article links to a project on GitHub demonstrating the bug - https://github.com/scottcarter/VideoCompositionSimulatorBug – Scott Carter Oct 18 '14 at 23:06
  • @AltafBangash..If you got the solution then please tell me.or upload the new code. I am having same issue with this code.please help....Thanks. – Milan patel Apr 28 '15 at 10:54
  • Have you got the solution for this issue? – Nithin Michael Oct 12 '15 at 12:02

1 Answers1

-1

Try this :

AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition
                                                                      presetName:AVAssetExportPresetPassthrough];
Bhumika
  • 876
  • 7
  • 20
Karen
  • 1