音频格式转化 m4a 转 wav
程序员文章站
2022-05-28 15:18:16
...
还是因为音频合成的原因,合成的音频是m4a 格式,上传到七牛后,形成的链接,浏览器可以播放。但是手机不行,m4a 是偏视频格式,用原有的avplayer 方法不能播放,所以需要进行转化后上传播放,
⚠️:更新 之前删除临时m4a转化文件的位置不对导致音频过段,还有采样率一定要对上。
下面是一些采样率的基础值介绍:
- 8,000 Hz - 电话所用采样率,对于人的说话已经足够
- 11,025 Hz
- 22,050 Hz - 无线电广播所用采样率
- 32,000 Hz - miniDV数字视频camcorder、DAT(LP mode)所用采样率
- 44,100 Hz - 音频CD,也常用于MPEG-1音频(VCD, SVCD, MP3)所用采样率
- 47,250 Hz - Nippon Columbia(Denon)开发的世界上第一个商用PCM录音机所用采样率
- 48,000 Hz - miniDV、数字电视、DVD、DAT、电影和专业音频所用的数字声音所用采样率
- 50,000 Hz - 二十世纪七十年代后期出现的3M和Soundstream开发的第一款商用数字录音机所用采样率
- 50,400 Hz - 三菱X-80数字录音机所用所用采样率
- 96,000或者192,000 Hz - DVD-Audio、一些LPCM DVD音轨、Blu-ray Disc(蓝光盘)音轨、和HD-DVD(高清晰度DVD)音轨所用所用采样率
- 2.8224 MHz - SACD、索尼和飞利浦联合开发的称为Direct Stream Digital的1位sigma-delta modulation过程所用采样率。
一定要采用合适的采样率,要不音频是播不出来的。
代码如下:
-(void)convetM4aToWav:(NSURL *)originalUrl
destUrl:(NSString *)destUrlStr
completed:(void (^)(NSError *error)) completed {
NSLog(@"\n\n\nM4A-WAV\n\n\n");
if ([[NSFileManager defaultManager] fileExistsAtPath:destUrlStr]) {
[[NSFileManager defaultManager] removeItemAtPath:destUrlStr error:nil];
}
NSURL *destUrl = [NSURL fileURLWithPath:destUrlStr];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:originalUrl options:nil];
//读取原始文件信息
NSError *error = nil;
AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:songAsset error:&error];
if (error) {
NSLog (@"error: %@", error);
completed(error);
return;
}
AVAssetReaderOutput *assetReaderOutput = [AVAssetReaderAudioMixOutput
assetReaderAudioMixOutputWithAudioTracks:songAsset.tracks
audioSettings: nil];
if (![assetReader canAddOutput:assetReaderOutput]) {
NSLog (@"can't add reader output... die!");
completed(error);
return;
}
[assetReader addOutput:assetReaderOutput];
AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:destUrl
fileType:AVFileTypeCoreAudioFormat
error:&error];
if (error) {
NSLog (@"error: %@", error);
completed(error);
return;
}
AudioChannelLayout channelLayout;
memset(&channelLayout, 0, sizeof(AudioChannelLayout));
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
[NSNumber numberWithFloat:8000], AVSampleRateKey,
[NSNumber numberWithInt:2], AVNumberOfChannelsKey,
[NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
[NSData dataWithBytes :&channelLayout length:sizeof(AudioChannelLayout)],AVChannelLayoutKey,
[NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
[NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
nil];
AVAssetWriterInput *assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
outputSettings:outputSettings];
if ([assetWriter canAddInput:assetWriterInput]) {
[assetWriter addInput:assetWriterInput];
} else {
NSLog (@"can't add asset writer input... die!");
completed(error);
return;
}
assetWriterInput.expectsMediaDataInRealTime = NO;
[assetWriter startWriting];
[assetReader startReading];
AVAssetTrack *soundTrack = [songAsset.tracks objectAtIndex:0];
CMTime startTime = CMTimeMake (0, soundTrack.naturalTimeScale);
[assetWriter startSessionAtSourceTime:startTime];
__block UInt64 convertedByteCount = 0;
dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue", NULL);
[assetWriterInput requestMediaDataWhenReadyOnQueue:mediaInputQueue
usingBlock: ^
{
while (assetWriterInput.readyForMoreMediaData) {
CMSampleBufferRef nextBuffer = [assetReaderOutput copyNextSampleBuffer];
if (nextBuffer) {
// append buffer
[assetWriterInput appendSampleBuffer: nextBuffer];
// NSLog (@"appended a buffer (%zu bytes)",
// CMSampleBufferGetTotalSampleSize (nextBuffer));
convertedByteCount += CMSampleBufferGetTotalSampleSize (nextBuffer);
} else {
[assetWriterInput markAsFinished];
[assetWriter finishWritingWithCompletionHandler:^{
}];
[assetReader cancelReading];
NSDictionary *outputFileAttributes = [[NSFileManager defaultManager]
attributesOfItemAtPath:[destUrl path]
error:nil];
NSLog (@"FlyElephant %lld",[outputFileAttributes fileSize]);
NSLog(@"转换结束");
// 删除临时temprecordAudio.m4a文件
NSError *removeError = nil;
if ([[NSFileManager defaultManager] fileExistsAtPath:[originalUrl path]]) {
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:[originalUrl path] error:&removeError];
if (!success) {
NSLog(@"删除临时temprecordAudio.m4a文件失败:%@",removeError);
completed(removeError);
}else{
NSLog(@"删除临时temprecordAudio.m4a文件:%@成功",originalUrl);
completed(removeError);
}
}else {
NSLog(@"文件不存在");
}
break;
}
}
}];
}
文档目录相关代码如下:
- (NSString *)documentPath
{
NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
return documentPath;
}
- (NSString *)audioCacheFolder
{
NSString *audioFolder = [[self documentPath] stringByAppendingPathComponent:AUDIOCACHE];
if (![FILEMANAGER fileExistsAtPath:audioFolder]) {
NSError *error = nil;
[FILEMANAGER createDirectoryAtPath:audioFolder withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
NSLog(@"音频文件夹创建失败----%@", error);
}
}
return audioFolder;
}
//用url作为文件名
- (NSString *)audioFilePath:(NSString *)audioURL
{
NSString *fileName = [audioURL stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
return [[self audioCacheFolder] stringByAppendingPathComponent:fileName];
}
转载于:https://www.jianshu.com/p/e78447cb24ee