Last active
October 17, 2017 09:30
-
-
Save DarkKor/6230294 to your computer and use it in GitHub Desktop.
The code for MusicGrabber, which can save music from iPod-library to your Documents or Caches.
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
+ (void)saveMusicFromURL: (NSURL *)sourceURL | |
toFile: (NSURL *)destURL | |
completion: (void(^)(BOOL success))_completion | |
{ | |
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:sourceURL options:nil]; | |
NSError *err = nil; | |
AVAssetReader *reader = [[AVAssetReader assetReaderWithAsset: asset | |
error: &err] retain]; | |
if(err) | |
{ | |
[reader release]; | |
_completion(NO); | |
return; | |
} | |
AVAssetReaderOutput *output = [[AVAssetReaderAudioMixOutput assetReaderAudioMixOutputWithAudioTracks: asset.tracks | |
audioSettings: nil] retain]; | |
if(![reader canAddOutput:output]) | |
{ | |
[reader release]; | |
[output release]; | |
_completion(NO); | |
return; | |
} | |
[reader addOutput:output]; | |
AVAssetWriter *writer = [[AVAssetWriter assetWriterWithURL: destURL | |
fileType: AVFileTypeCoreAudioFormat | |
error: &err] retain]; | |
if(err) | |
{ | |
[reader release]; | |
[output release]; | |
[writer release]; | |
_completion(NO); | |
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 *input = [[AVAssetWriterInput assetWriterInputWithMediaType: AVMediaTypeAudio | |
outputSettings: outputSettings] retain]; | |
if(![writer canAddInput:input]) | |
{ | |
[reader release]; | |
[output release]; | |
[writer release]; | |
[input release]; | |
_completion(NO); | |
return; | |
} | |
[writer addInput:input]; | |
input.expectsMediaDataInRealTime = NO; | |
[writer startWriting]; | |
[reader startReading]; | |
AVAssetTrack *soundtrack = [asset.tracks objectAtIndex:0]; | |
CMTime startTime = CMTimeMake (0, soundtrack.naturalTimeScale); | |
[writer startSessionAtSourceTime: startTime]; | |
__block UInt64 convertedByteCount = 0; | |
dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue", NULL); | |
[input requestMediaDataWhenReadyOnQueue: mediaInputQueue | |
usingBlock: ^ | |
{ | |
while (input.readyForMoreMediaData) | |
{ | |
CMSampleBufferRef nextBuffer = [output copyNextSampleBuffer]; | |
if(nextBuffer) | |
{ | |
[input appendSampleBuffer: nextBuffer]; | |
convertedByteCount += CMSampleBufferGetTotalSampleSize (nextBuffer); | |
CFRelease(nextBuffer); | |
} | |
else | |
{ | |
[input markAsFinished]; | |
[writer finishWriting]; | |
[reader cancelReading]; | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
_completion(YES); | |
}); | |
break; | |
} | |
} | |
}]; | |
[reader release]; | |
[output release]; | |
[writer release]; | |
[input release]; | |
return; | |
} |
SourceURL is maybe next:
NSArray *items = [mediaItemCollection items];
MPMediaItem *_track = items[i];
NSURL *sourceURL = [_track valueForProperty:MPMediaItemPropertyAssetURL];
Dos you have any experience if this "grabbing" is according to the Apple guidelines or will this be rejected during the compliance test?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do not forget to import AVFounddation