如何使用AVAssetWriter制作缩小尺寸的video?

我会制作缩小尺寸的video,可能是50像素和75像素的长度。 这些是物理层面。

你如何设定? 在video设置? 我认为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 

您需要设置video编解码器参数:

 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]; 

你需要一个博士学位与AVAssetWriter – 这是不平凡的: https : //developer.apple.com/library/mac/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/05_Export.html#//apple_ref/doc/uid/ TP40010188-CH9-SW1

有一个惊人的库正是你想要的只是一个AVAssetExportSession插件replace更重要的function,如更改比特率: 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); } }]; }