Created
February 27, 2019 00:37
-
-
Save mixflame/af75a3b57d9642354e9e4859400ccad3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// convertToWav.m | |
// MixDJ | |
// | |
// Created by Jonathan Silverman on 2/26/19. | |
// Copyright © 2019 Jonathan Silverman. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <AVFoundation/AVFoundation.h> | |
void convertToWav(NSURL *assetURL, NSString *exportPath) { | |
// set up an AVAssetReader to read from a URL | |
AVURLAsset *songAsset = | |
[AVURLAsset URLAssetWithURL:assetURL options:nil]; | |
NSError *assetError = nil; | |
AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:songAsset | |
error:&assetError]; | |
if (assetError) { | |
NSLog (@"error: %@", assetError); | |
return; | |
} | |
AVAssetReaderOutput *assetReaderOutput = | |
[AVAssetReaderAudioMixOutput | |
assetReaderAudioMixOutputWithAudioTracks:songAsset.tracks | |
audioSettings: nil]; | |
if (! [assetReader canAddOutput: assetReaderOutput]) { | |
NSLog (@"can't add reader output... die!"); | |
return; | |
} | |
[assetReader addOutput: assetReaderOutput]; | |
if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) { | |
[[NSFileManager defaultManager] removeItemAtPath:exportPath | |
error:nil]; | |
} | |
NSURL *exportURL = [NSURL fileURLWithPath:exportPath]; | |
AVAssetWriter *assetWriter = | |
[AVAssetWriter assetWriterWithURL:exportURL | |
fileType:AVFileTypeWAVE | |
error:&assetError]; | |
if (assetError) { | |
NSLog (@"error: %@", assetError); | |
return; | |
} | |
AudioChannelLayout channelLayout; | |
memset(&channelLayout, 0, sizeof(AudioChannelLayout)); | |
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo; | |
NSDictionary *outputSettings = | |
[NSDictionary dictionaryWithObjectsAndKeys: | |
[NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey, | |
[NSNumber numberWithFloat:44100.0], AVSampleRateKey, | |
[NSNumber numberWithInt:2], AVNumberOfChannelsKey, | |
[NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)], | |
AVChannelLayoutKey, | |
[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 ([assetWriter canAddInput:assetWriterInput]) { | |
[assetWriter addInput:assetWriterInput]; | |
} else { | |
NSLog (@"can't add asset writer input... die!"); | |
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]; | |
// update ui | |
convertedByteCount += | |
CMSampleBufferGetTotalSampleSize (nextBuffer); | |
NSNumber *convertedByteCountNumber = | |
[NSNumber numberWithLong:convertedByteCount]; | |
NSLog(convertedByteCountNumber.stringValue); | |
// [self performSelectorOnMainThread:@selector(updateSizeLabel:) | |
// withObject:convertedByteCountNumber | |
// waitUntilDone:NO]; | |
} else { | |
// done! | |
[assetWriterInput markAsFinished]; | |
[assetWriter finishWriting]; | |
[assetReader cancelReading]; | |
NSDictionary *outputFileAttributes = | |
[[NSFileManager defaultManager] | |
attributesOfItemAtPath:exportPath | |
error:nil]; | |
NSLog (@"done. file size is %ld", | |
[outputFileAttributes fileSize]); | |
NSNumber *doneFileSize = [NSNumber numberWithLong: | |
[outputFileAttributes fileSize]]; | |
// [self performSelectorOnMainThread:@selector(updateCompletedSizeLabel:) | |
// withObject:doneFileSize | |
// waitUntilDone:NO]; | |
// release a lot of stuff | |
break; | |
} | |
} | |
}]; | |
NSLog (@"bottom of convertTapped:"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment