6

从一个 CAF 录音开始,我需要创建一个视频文件,其中视频轨道只是空帧,而音轨就是这个录音文件。我知道这是一个奇怪的用例,这可能就是我找不到有关如何操作的信息的原因。我正在尝试使用AVMutableComposition,但我无法弄清楚如何让它工作。

这是我现在所拥有的:

AVMutableComposition *composition = [AVMutableComposition composition];    

/* soundFilePath is the path to the CAF audio file */
AVURLAsset *audioAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:soundFilePath] options:nil];
CMTimeRange fullTime = CMTimeRangeMake(kCMTimeZero, [audioAsset duration]);
AVAssetTrack *sourceAudioTrack = [[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

/* Add audio track */
AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack insertTimeRange:fullTime ofTrack:sourceAudioTrack atTime:kCMTimeZero error:nil];

/* docsDir is defined previously as the path to where I want to save the file */
NSString *videoFilePath = [docsDir stringByAppendingPathComponent:@"video.mov"];
NSURL *videoFileURL = [NSURL fileURLWithPath:videoFilePath];

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];

exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.outputURL = videoFileURL;

[exporter exportAsynchronouslyWithCompletionHandler:nil];

这让我得到的是一个 .mov 文件,其中包含音频,但没有视频。我需要该文件包含某种视频内容 - 只是空的视频帧。我对所有这些 iOS 库都很陌生,所以上面的所有代码基本上都是从各种示例中拼凑而成的,我当然不完全了解这一切应该如何工作。

我还尝试添加一个AVMutableVideoComposition以防AVMutableComposition万一这是正确的方法。为了做到这一点,我以与添加音轨AVMutableComposition相同的方式添加了一个空白视频轨道:

/* Add video track */
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionVideoTrack insertEmptyTimeRange:fullTime];

然后我创建并配置了一个AVMutableVideoComposition这样的:

AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.frameDuration = [audioAsset duration];
videoComposition.renderScale = 1.0;
videoComposition.renderSize = CGSizeMake(320, 240);
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack];
instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
instruction.timeRange = compositionVideoTrack.timeRange;

videoComposition.instructions = [NSArray arrayWithObject:instruction];

最后,添加AVMutableVideoCompositionAVMutableComposition

exporter.videoComposition = videoComposition;

但是,使用这种方法,我什么也得不到。不创建视频文件。我在导出的完成处理程序中放置了一条日志语句,它确实完成了,但没有创建视频文件。

对此的任何帮助将不胜感激。谢谢!

4

0 回答 0