2010-11-23 11 views
9

私は、サイズが50ピクセル、長さが75ピクセルのビデオを縮小します。それらは物理的な次元です。AVAssetWriterを使用して縮小ビデオを作成するにはどうすればよいですか?

どのように設定しますか?ビデオ設定で?私は、AVVideoWidthKeyとAVVideoHeightKeyは、私が必要とする物理的な次元ではなく、解像度のためのものだと思います。

あなたがビデオコーデックパラメータを設定する必要が
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
           AVVideoCodecH264, AVVideoCodecKey, 
           [NSNumber numberWithInt: 320], AVVideoWidthKey,  
           [NSNumber numberWithInt:480], AVVideoHeightKey, 
           nil]; 
AVAssetWriterInput* writerInput = [[AVAssetWriterInput 
            assetWriterInputWithMediaType:AVMediaTypeVideo 
            outputSettings:videoSettings] retain 

答えて

19

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

なぜ[の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)は、キーフレーム間の最大間隔にアクセスするためのキー。対応する値は 'NSNumber'のインスタンスです。 '1'はキー**フレームのみ**を意味します。 – Regexident

+3

こんにちは、私はそれが何をしているのかを技術的に理解しています。私の質問は、あなたがこのような状況で意図的にそれを行うのはなぜですか?それは最終ビデオを圧縮する能力を低下させませんか? – Meekohi

7

あなたはAVAssetWriterで動作するように博士号を必要とする - それは非自明です: https://developer.apple.com/library/mac/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/05_Export.html#//apple_ref/doc/uid/TP40010188-CH9-SW1

正確に何を行うための素晴らしいライブラリがありますビットレートを変更するなどの重要な機能を備えたAVAssetExportSessionドロップインの置き換えだけです。https://github.com/rs/SDAVAssetExportSession

ここ

はそれを使用する方法は次のとおりです。

-(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

このライブラリでAVVideoProfileLevelH264Baseline30を使用するために必要な設定を教えてください。 AVVideoProfileLevelH264High40を置き換えてみましたが、エラーが報告されていますので、設定がベースライン3.0に適合していないと推測しています。ありがとうございます@etayluz – Gannicus

関連する問題