我試圖用以下代碼將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是支持的輸出類型。
沒有任何人有任何想法,爲什麼這是給我的錯誤「不支持此媒體的操作」
好,謝謝我曾嘗試但不幸的是,我不認爲這是正確的,因爲AVAssetExportSession對象無法建造時間範圍(是等於空)當我改變這個代碼。這是發生了什麼 - > NSLog(@「Exporter:%@ \ n」,exporter); 出口商:(null) – Jamesla
你的意思是不正確的?你正在使用一個錯誤的常量值。您可以檢查蘋果文檔以獲取該方法中支持的值。我想你應該檢查Apple的AVSimpleEditor項目。 https://developer.apple.com/library/ios/samplecode/AVSimpleEditoriOS/Introduction/Intro.html#//apple_ref/doc/uid/DTS40012797 –
對不起,我搞砸了(改變導出到AVMediaTypeVideo,而不是軌道)你是死對頭的。我會獎給你獎品:)謝謝你的幫助Gaurav。 – Jamesla