/**
把.m4a转为.caf格式
@paramoriginalUrlStr .m4a文件路径
@paramdestUrlStr .caf文件路径
@paramcompleted 转化完成的block
*/
+ (void)convetM4aToWav:(NSString*)originalUrlStr
destUrl:(NSString*)destUrlStr
completed:(void(^)(NSError*error)) completed {
NSURL*originalUrl = [NSURLfileURLWithPath:originalUrlStr];
NSURL*destUrl = [NSURLfileURLWithPath:destUrlStr];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:originalUrl options:nil]; NSError*error =nil;
AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:songAsset error:&error];
if(error) {
NSLog (@"AVAssetWriter error: %@", error.description);
completed(error);
return;
}
AVAssetReaderOutput *assetReaderOutput = [AVAssetReaderAudioMixOutput
assetReaderAudioMixOutputWithAudioTracks:songAsset.tracks
audioSettings:nil];
if(![assetReadercanAddOutput:assetReaderOutput]) {
NSLog (@"assetWriter canAddInput error %@",error.description);
completed(error);
return;
}
[assetReaderaddOutput:assetReaderOutput];
AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:destUrl
fileType:AVFileTypeCoreAudioFormat
error:&error];
if(error) {
NSLog(@"error: %@", error);
completed(error);
return;
}
AudioChannelLayoutchannelLayout;
memset(&channelLayout,0,sizeof(AudioChannelLayout));
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
[NSNumbernumberWithFloat:44100],AVSampleRateKey,
[NSNumbernumberWithInt:2],AVNumberOfChannelsKey,
[NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
[NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
nil];
AVAssetWriterInput *assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
outputSettings:outputSettings];
if([assetWritercanAddInput:assetWriterInput]) {
[assetWriteraddInput:assetWriterInput];
}else{
NSLog (@"can't add asset writer input... die!");
completed(error);
return;
}
assetWriterInput.expectsMediaDataInRealTime = NO;
[assetWriterstartWriting];
[assetReaderstartReading];
AVAssetTrack*soundTrack = [songAsset.tracksobjectAtIndex:0];
CMTimestartTime =CMTimeMake(0, soundTrack.naturalTimeScale);
[assetWriterstartSessionAtSourceTime:startTime];
__blockUInt64convertedByteCount =0;
dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue", NULL);
[assetWriterInputrequestMediaDataWhenReadyOnQueue:mediaInputQueue
usingBlock: ^
{
while(assetWriterInput.readyForMoreMediaData) {
CMSampleBufferRefnextBuffer = [assetReaderOutputcopyNextSampleBuffer];
if(nextBuffer) {
// append buffer
[assetWriterInputappendSampleBuffer: nextBuffer];
NSLog (@"appended a buffer (%zu bytes)",
CMSampleBufferGetTotalSampleSize(nextBuffer));
convertedByteCount +=CMSampleBufferGetTotalSampleSize(nextBuffer);
}else{
[assetWriterInputmarkAsFinished];
[assetWriterfinishWritingWithCompletionHandler:^{
}];
[assetReadercancelReading];
NSDictionary*outputFileAttributes = [[NSFileManagerdefaultManager]
attributesOfItemAtPath:[destUrlpath]
error:nil];
NSLog(@"转换结束,输出文件参数\n%@",outputFileAttributes);
completed(nil);
break;
}
}
}];
}