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];
そして最後に、を追加しAVMutableVideoComposition
ましたAVMutableComposition
:
exporter.videoComposition = videoComposition;
ただし、このアプローチでは何も得られません。ビデオファイルは作成されません。エクスポートの完了ハンドラーにログステートメントを入れて完了しましたが、ビデオファイルが作成されません。
これに関する助けをいただければ幸いです。ありがとうございました!