7
Những là sự khởi đầu o một phương pháp tôi đang sử dụng để hợp nhất video cùngAVAsset "tracksWithMediaType" trả về một mảng trống
-(void) mergeVideosAndAudio:(AVAsset *)audioAsset{
//Load Video Assets
NSError *error;
NSArray *dirFiles;
if ((dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self documentsDirectory] error:&error]) == nil) {
// handle the error
};
// find all the temp files
NSArray *movFiles = [dirFiles filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self BEGINSWITH 'temp'"]];
NSLog(@"The are %i temp files",movFiles.count);
//Create assets array
NSMutableArray *assets = [[NSMutableArray alloc]init];
for (int i = 0; i < movFiles.count; i++) {
NSString *videoURL = [[self documentsDirectory] stringByAppendingPathComponent:
[NSString stringWithFormat:@"temp%i.mov", i]];
NSURL *url = [NSURL fileURLWithPath:videoURL];
AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:url options:nil];
[assets addObject:videoAsset];
}
NSLog(@"assets:%i ", assets.count);
// a second way
for (id obj in assets)
NSLog(@"obj: %@", obj);
//Create the composition
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
// 1 - Video track
AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime videoTrackDuration;
for (int j = 0; j < assets.count; j++) {
AVURLAsset *currentAsset = assets[j];
videoTrackDuration = CMTimeAdd(videoTrackDuration, currentAsset.duration);
CMTime time;
if (j == 0) {
time = kCMTimeZero;
}else{
AVURLAsset *previousAsset = assets[j-1];
time = previousAsset.duration;
}
AVAssetTrack *assetTrack = [[currentAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, currentAsset.duration) ofTrack:assetTrack atTime:time error:nil];
}
Vấn đề tôi đang gặp là tracksWithMediaType
tài sản của currentAsset là một mảng trống. Đây là giao diện điều khiển
Bất kỳ trợ giúp sẽ được đánh giá cao. Cảm ơn
Bạn đã xác minh rằng currentAsset thực trỏ đến một tập tin thực tế trên hệ thống tập tin? – jlw
@jlw Khi tôi hiểu nó currentAsset lấy tập tin được lưu trữ trong mảng tài sản mà có thể được nhìn thấy trong console.Right? – Kaitis
Có, nhưng câu hỏi của tôi là nếu tất cả các currentAssets có thể chơi được thông qua AVPlayer hoặc MPMoviePlayer. Có thể tạo một AVAsset với một NSURL trỏ đến một vị trí không thực sự chứa một tệp. – jlw