2010-11-23 12 views
9

Je ferais de la vidéo de taille réduite, peut-être 50 pixels à travers et 75 pixels pour la longueur. Ce sont la dimension physique.Comment faire une vidéo de taille réduite en utilisant AVAssetWriter?

Comment réglez-vous cela? dans les vidéosettings? Je pense que AVVideoWidthKey et AVVideoHeightKey sont plus pour la résolution pas pour la dimension physique ce dont j'avais besoin.

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
           AVVideoCodecH264, AVVideoCodecKey, 
           [NSNumber numberWithInt: 320], AVVideoWidthKey,  
           [NSNumber numberWithInt:480], AVVideoHeightKey, 
           nil]; 
AVAssetWriterInput* writerInput = [[AVAssetWriterInput 
            assetWriterInputWithMediaType:AVMediaTypeVideo 
            outputSettings:videoSettings] retain 

Répondre

19

Vous devez définir les paramètres de codec vidéo:

NSDictionary *videoCleanApertureSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
              [NSNumber numberWithInt:320], AVVideoCleanApertureWidthKey, 
              [NSNumber numberWithInt:480], AVVideoCleanApertureHeightKey, 
              [NSNumber numberWithInt:10], AVVideoCleanApertureHorizontalOffsetKey, 
              [NSNumber numberWithInt:10], AVVideoCleanApertureVerticalOffsetKey, 
              nil]; 


NSDictionary *videoAspectRatioSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
              [NSNumber numberWithInt:3], AVVideoPixelAspectRatioHorizontalSpacingKey, 
              [NSNumber numberWithInt:3],AVVideoPixelAspectRatioVerticalSpacingKey, 
                nil]; 



NSDictionary *codecSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithInt:960000], AVVideoAverageBitRateKey, 
           [NSNumber numberWithInt:1],AVVideoMaxKeyFrameIntervalKey, 
           videoCleanApertureSettings, AVVideoCleanApertureKey, 
           //videoAspectRatioSettings, AVVideoPixelAspectRatioKey, 
           //AVVideoProfileLevelH264Main30, AVVideoProfileLevelKey, 
           nil]; 





NSString *targetDevice = [[UIDevice currentDevice] model]; 

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
           AVVideoCodecH264, AVVideoCodecKey, 
           codecSettings,AVVideoCompressionPropertiesKey, 
           [NSNumber numberWithInt:320], AVVideoWidthKey, 
           [NSNumber numberWithInt:480], AVVideoHeightKey, 
           nil]; 
+0

Pourquoi [NSNumber numberWithInt: 1], AVVideoMaxKeyFrameIntervalKey? – Meekohi

+1

@Meekohi: ['AVVideoMaxKeyFrameIntervalKey'] (https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVFoundation_Constants/Reference/reference.html#//apple_ref/doc/c_ref/AVVideoMaxKeyFrameIntervalKey) spécifie un touche pour accéder à l'intervalle maximum entre les images clés. La valeur correspondante est une instance de 'NSNumber'. «1» signifie clé ** cadres seulement **. – Regexident

+3

Hey désolé je comprends techniquement ce qu'il fait, ma question est pourquoi vous le feriez intentionnellement dans cette situation? Cela ne réduit-il pas la capacité de compresser la vidéo finale? – Meekohi

7

Vous avez besoin d'un doctorat pour travailler avec AVAssetWriter - il est non trivial: https://developer.apple.com/library/mac/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/05_Export.html#//apple_ref/doc/uid/TP40010188-CH9-SW1

Il y a une bibliothèque incroyable pour faire exactement ce que vous voulez qui est juste un remplacement drop-in AVAssetExportSession avec des fonctionnalités plus cruciales comme le changement du débit: https://github.com/rs/SDAVAssetExportSession

Voici comment l'utiliser:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 

    SDAVAssetExportSession *encoder = [SDAVAssetExportSession.alloc initWithAsset:[AVAsset assetWithURL:[info objectForKey:UIImagePickerControllerMediaURL]]]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    self.myPathDocs = [documentsDirectory stringByAppendingPathComponent: 
         [NSString stringWithFormat:@"lowerBitRate-%d.mov",arc4random() % 1000]]; 
    NSURL *url = [NSURL fileURLWithPath:self.myPathDocs]; 
    encoder.outputURL=url; 
    encoder.outputFileType = AVFileTypeMPEG4; 
    encoder.shouldOptimizeForNetworkUse = YES; 

    encoder.videoSettings = @ 
    { 
    AVVideoCodecKey: AVVideoCodecH264, 
    AVVideoCompressionPropertiesKey: @ 
    { 
    AVVideoAverageBitRateKey: @2300000, 
    AVVideoProfileLevelKey: AVVideoProfileLevelH264High40, 
    }, 
    }; 
    encoder.audioSettings = @ 
    { 
    AVFormatIDKey: @(kAudioFormatMPEG4AAC), 
    AVNumberOfChannelsKey: @2, 
    AVSampleRateKey: @44100, 
    AVEncoderBitRateKey: @128000, 
    }; 

    [encoder exportAsynchronouslyWithCompletionHandler:^ 
    { 
    int status = encoder.status; 

    if (status == AVAssetExportSessionStatusCompleted) 
    { 
     AVAssetTrack *videoTrack = nil; 
     AVURLAsset *asset = [AVAsset assetWithURL:encoder.outputURL]; 
     NSArray *videoTracks = [asset tracksWithMediaType:AVMediaTypeVideo]; 
     videoTrack = [videoTracks objectAtIndex:0]; 
     float frameRate = [videoTrack nominalFrameRate]; 
     float bps = [videoTrack estimatedDataRate]; 
     NSLog(@"Frame rate == %f",frameRate); 
     NSLog(@"bps rate == %f",bps/(1024.0 * 1024.0)); 
     NSLog(@"Video export succeeded"); 
     // encoder.outputURL <- this is what you want!! 
    } 
    else if (status == AVAssetExportSessionStatusCancelled) 
    { 
     NSLog(@"Video export cancelled"); 
    } 
    else 
    { 
     NSLog(@"Video export failed with error: %@ (%d)", encoder.error.localizedDescription, encoder.error.code); 
    } 
    }]; 
} 
+0

Pourriez-vous s'il vous plaît me dire quels paramètres je dois utiliser pour utiliser AVVideoProfileLevelH264Baseline30 avec cette bibliothèque? J'ai essayé le vôtre en remplaçant AVVideoProfileLevelH264High40 mais il signale une erreur, donc je suppose que les paramètres ne sont pas adaptés à la version 3.0 de base. Merci d'avance @etayluz – Gannicus