我正在编写一个 iPad 应用程序,我需要在其中加入不同分辨率的 mp4 文件。为此,我使用 AVAssetReader 的组合来读取 mp4 源文件,并使用 AVAssetWriter 将这些源文件写入单个 mp4 输出文件。
我尝试使用 AVAssetExportSession 但我遇到的问题是不同连接文件之间存在黑框。
我现在面临的问题是一切似乎都很好,但 AVAssetWriter 的完成处理程序从未被调用。
这是我的选择器,将 mp4 文件 URL、单个输出文件 URL 和完成处理程序列表作为输入。
- (void)resizeAndJoinVideosAtURLs:(NSArray *)videoURLs toOutputURL:(NSURL *)outputURL withHandler:(void(^)(NSURL *fileURL))handler
{
/*
First step: create the writer and writer input
*/
NSError *error = nil;
self.videoAssetWriter = [[AVAssetWriter alloc] initWithURL:outputURL fileType:AVFileTypeMPEG4 error:&error];
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecH264, AVVideoCodecKey,[NSNumber numberWithInt:640], AVVideoWidthKey,[NSNumber numberWithInt:480], AVVideoHeightKey,nil];
AVAssetWriterInput* videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
videoWriterInput.expectsMediaDataInRealTime = NO;
if([self.videoAssetWriter canAddInput:videoWriterInput])
{
[self.videoAssetWriter addInput:videoWriterInput];
[self.videoAssetWriter startWriting];
[self.videoAssetWriter startSessionAtSourceTime:kCMTimeZero];
/*
Second step: for each video URL given create a reader and an reader input
*/
for(NSURL *videoURL in videoURLs)
{
NSLog(@"Processing file: %@",videoURL);
AVAsset *videoAsset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
AVAssetReader *videoAssetReader = [[AVAssetReader alloc] initWithAsset:videoAsset error:&error];
AVAssetTrack *videoAssetTrack = [videoAsset tracksWithMediaType:AVMediaTypeVideo].firstObject;
NSDictionary *videoOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
AVAssetReaderTrackOutput *videoAssetTrackOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:videoAssetTrack outputSettings:videoOptions];
videoAssetTrackOutput.alwaysCopiesSampleData = NO;
if([videoAssetReader canAddOutput:videoAssetTrackOutput])
{
[videoAssetReader addOutput:videoAssetTrackOutput];
[videoAssetReader startReading];
/*
Step three: copy the buffers from the reader to the writer
*/
while ([videoAssetReader status] == AVAssetReaderStatusReading)
{
if(![videoWriterInput isReadyForMoreMediaData]) continue;
CMSampleBufferRef buffer = [videoAssetTrackOutput copyNextSampleBuffer];
if(buffer)
{
[videoWriterInput appendSampleBuffer:buffer];
CFRelease(buffer);
}
}
} else NSLog(@"ERROR: %@",error);
}
[videoWriterInput markAsFinished];
} else NSLog(@"ERROR: %@",error);
__weak ClipBuilder *weakself = self;
[self.videoAssetWriter finishWritingWithCompletionHandler:^{
handler(outputURL);
weakself.videoAssetWriter = nil;
}];
}
我的输出文件存在并且 AVAssetWriter 存在,因为它是一个属性,但仍然没有调用完成处理程序。这有什么可以解释的?
谢谢你的帮助。
这有什么可以解释的?