iOS 把视频文件转换为caf文件

需要注意一点,代码过于老旧,现在不需要retain和release
-(IBAction) convertTapped : (id) sender {
    /* set up an AVAssetReader to read from the iPod Library */
    NSURL       *assetURL   = [song valueForProperty : MPMediaItemPropertyAssetURL];
    AVURLAsset  *songAsset  =
    [AVURLAsset URLAssetWithURL:assetURL options:nil];

    NSError     *assetError = nil;
    AVAssetReader   *assetReader    =
    [[AVAssetReader assetReaderWithAsset:songAsset
      error:&assetError]
     retain];
    if ( assetError )
    {
        NSLog( @ "error: %@", assetError );
        return;
    }
    AVAssetReaderOutput *assetReaderOutput =
    [[AVAssetReaderAudioMixOutput
      assetReaderAudioMixOutputWithAudioTracks:songAsset.tracks
      audioSettings: nil]
     retain];
    if ( ! [assetReader canAddOutput : assetReaderOutput] )
    {
        NSLog( @ "can't add reader output... die!" );
        return;
    }
    [assetReader addOutput : assetReaderOutput];
    NSArray *dirs = NSSearchPathForDirectoriesInDomains
                ( NSDocumentDirectory, NSUserDomainMask, YES );
    NSString    *documentsDirectoryPath = [dirs objectAtIndex:0];
    NSString    *exportPath     = [[documentsDirectoryPath
                            stringByAppendingPathComponent:EXPORT_NAME]
                           retain];
    if ([[NSFileManager defaultManager] fileExistsAtPath : exportPath] )
    {
        [[NSFileManager defaultManager] removeItemAtPath : exportPath
         error : nil];
    }
    NSURL *exportURL = [NSURL fileURLWithPath:exportPath];

    AVAssetWriter *assetWriter =
    [[AVAssetWriter assetWriterWithURL:exportURL
      fileType:AVFileTypeCoreAudioFormat
      error:&assetError]
     retain];
    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]
     retain];
    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];
                 [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 */
                 [assetReader release];
                 [assetReaderOutput release];
                 [assetWriter release];
                 [assetWriterInput release];
                 [exportPath release];
                 break;
             }
         }
     }];
    NSLog( @ "bottom of convertTapped:" );
}

参考:
http://subfurther.com/blog/2010/12/13/from-ipod-library-to-pcm-samples-in-far-fewer-steps-than-were-previously-necessary/

你可能感兴趣的:(iOS 把视频文件转换为caf文件)