2013-12-13 23 views
1

我试图用以下代码将2个预先存在的mpeg4视频加在一个ipad2上。AVAssetExportSession - 在IOS中加入2个mp4文件

-(void)mergeTestVideos 
{ 

    //setup asset 
    NSString *firstassetpath = [NSString stringWithFormat:@"%@mpeg4-1.mp4", NSTemporaryDirectory()]; 
    NSString *secondassetpath = [NSString stringWithFormat:@"%@mpeg4-2.mp4", NSTemporaryDirectory()]; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    AVAsset *firstAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:firstassetpath]]; 
    AVAsset *secondAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:secondassetpath]]; 

    NSLog(@"FirstAsset Is Readable = %d", firstAsset.isReadable); 
    NSLog(@"FirstAsset Is playable = %d", firstAsset.isPlayable); 
    NSLog(@"FirstAsset Is exportable = %d", firstAsset.exportable); 
    NSLog(@"SecondAsset Is Readable = %d", secondAsset.isReadable); 
    NSLog(@"SecondAsset Is playable = %d", secondAsset.isPlayable); 
    NSLog(@"SecondAsset Is exportable = %d", secondAsset.exportable); 

    //setup composition and track 
    AVMutableComposition *composition = [[AVMutableComposition alloc]init]; 
    AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVAssetExportPresetPassthrough preferredTrackID:kCMPersistentTrackID_Invalid]; 

    //add assets to track 
    [track insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration) ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0] atTime:kCMTimeZero error:nil]; 

    [track insertTimeRange:CMTimeRangeMake(kCMTimeZero, secondAsset.duration) ofTrack:[[secondAsset tracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0] atTime:firstAsset.duration error:nil]; 

    // 5 - Create exporter 
    AVAssetExportSession *exporter = [[AVAssetExportSession alloc]initWithAsset:composition presetName:AVAssetExportPresetPassthrough]; 

    NSString *outputURL = [NSString stringWithFormat:@"%@mergedvid.mp4", NSTemporaryDirectory()]; 

    NSLog(@"%@", exporter.supportedFileTypes); 
    exporter.outputURL=[NSURL fileURLWithPath:outputURL]; 

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

} 

-(void)exportDidFinish:(AVAssetExportSession*)session { 

    NSLog(@"export method"); 
    NSLog(@"%i", session.status); 
    NSLog(@"%@", session.error); 
} 

和输出如下:

- FirstAsset Is Readable = 1 
- FirstAsset Is playable = 1 
- FirstAsset Is exportable = 1 
- SecondAsset Is Readable = 1 
- SecondAsset Is playable = 1 
- SecondAsset Is exportable = 1 
- (
"com.apple.quicktime-movie", 
"com.apple.m4a-audio", 
"public.mpeg-4", 
"com.apple.m4v-video", 
"public.3gpp", 
"org.3gpp.adaptive-multi-rate-audio", 
"com.microsoft.waveform-audio", 
"public.aiff-audio", 
"public.aifc-audio", 
"com.apple.coreaudio-format" 
) 
-export method 
- 4 
- Error Domain=AVFoundationErrorDomain Code=-11838 "Operation Stopped" UserInfo=0x155f76f0 {NSLocalizedDescription=Operation Stopped, NSLocalizedFailureReason=The operation is not supported for this media.} 

确定,所以根据输出我的文件是确定,导出和MP4是支持的输出类型。

没有任何人有任何想法,为什么这是给我的错误“不支持此媒体的操作”

回答

2

我觉得这种说法是您的罪魁祸首

AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVAssetExportPresetPassthrough preferredTrackID:kCMPersistentTrackID_Invalid]; 

这里你逝去的AVAssetExportPresetPassthrough哪里你应该使用AVMediaTypeVideoAVMediaTypeAudio

+0

好,谢谢我曾尝试但不幸的是,我不认为这是正确的,因为AVAssetExportSession对象无法建造时间范围(是等于空)当我改变这个代码。这是发生了什么 - > NSLog(@“Exporter:%@ \ n”,exporter); 出口商:(null) – Jamesla

+0

你的意思是不正确的?你正在使用一个错误的常量值。您可以检查苹果文档以获取该方法中支持的值。我想你应该检查Apple的AVSimpleEditor项目。 https://developer.apple.com/library/ios/samplecode/AVSimpleEditoriOS/Introduction/Intro.html#//apple_ref/doc/uid/DTS40012797 –

+0

对不起,我搞砸了(改变导出到AVMediaTypeVideo,而不是轨道)你是死对头的。我会奖给你奖品:)谢谢你的帮助Gaurav。 – Jamesla

1

如果你正在加入两个视频,那么你为什么在插入时间范围时在两个地方使用kCMTimeZero。我觉得应该是

[track insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration) ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0] atTime:kCMTimeZero error:nil]; 

[track insertTimeRange:CMTimeRangeMake(kCMTimeZero, secondAsset.duration) ofTrack:[[secondAsset tracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0] atTime:firstAsset.duration error:nil]; 

见变化atTime同时插入了secondAsset

+0

好点子!感谢那。不幸的是,在改变这个之后,我仍然坚持原来的问题。 – Jamesla

+0

您是否尝试将AVAssetExportPresetPassthrough更改为AVMediaTypeVideo? –