[英]Exporting audio from 3gp video files with AVAssetExportSession
我試圖從3gpp視頻文件中導出音頻而它無法工作......有誰知道我可能做錯了什么? 這是我正在使用的代碼:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"newFile.m4a"];
NSString *tempFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"oldFile.3gp"];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:tempFilePath] options:nil];
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^ {
//HERE IS THE PROBLEM. THE ARRAY OF TRACKS IS EMPTY FOR SOME REASON.
AVAssetTrack* audioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
AVMutableComposition* audioComposition = [AVMutableComposition composition];
AVMutableCompositionTrack* audioCompositionTrack = [audioComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[audioCompositionTrack insertTimeRange:[audioTrack timeRange] ofTrack:audioTrack atTime:CMTimeMake(0, 1) error:nil];
AVAssetExportSession *exprortSession = [AVAssetExportSession exportSessionWithAsset:audioComposition presetName:AVAssetExportPresetAppleM4A];
NSURL *toFileURL = [NSURL URLWithString:filePath];
exprortSession.outputURL = toFileURL;
exprortSession.outputFileType = @"com.apple.m4a-audio";
NSLog(@"exportAsynchronouslyWithCompletionHandler will start");
[exprortSession exportAsynchronouslyWithCompletionHandler: ^(void) {
if (exprortSession.status == AVAssetExportSessionStatusCompleted) {
NSLog(@"Export success");
}
else {
NSLog(@"Export failed");
}
}];
}];
嘗試加載資產
[AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:tempFilePath] options:nil];
代替
[AVURLAsset URLAssetWithURL:[NSURL URLWithString:tempFilePath] options:nil];
由於您的任務將直接操作音頻樣本緩沖區,您應該使用AVFoundation將為您提供的第二個變體:配對的AVAssetReader和AVAssetWriter設置。 您可以在Apple開發人員源代碼的AVReaderWriterOSX中找到正確的示例代碼。 除了您有不同的I / O格式設置外,這也適用於iOS。 應該給出將音頻解壓縮為PCM並寫回未壓縮的.wav或音頻文件的可用性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.