/* Convert resize and quanlity movie */
+ (void) resizeVideo:(NSString*)inputVideo output:(NSString*) outputVideo size:(CGSize) size bitsPerPixel:(float) bitsPerPixel {
NSAutoreleasePool *pools = [[NSAutoreleasePool alloc] init];
NSURL *outPath = [NSURL fileURLWithPath:outputVideo];
NSURL *inputPath = [NSURL fileURLWithPath:inputVideo];
NSError *error = nil;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:outPath fileType:AVFileTypeQuickTimeMovie error:&error];
NSParameterAssert(videoWriter);
AVAsset *avAsset = [[[AVURLAsset alloc] initWithURL:inputPath options:nil] autorelease];
int numPixels = size.width * size.height;
NSLog(@"bitsPerPixel:%f",bitsPerPixel);
int bitsPerSecond = numPixels * bitsPerPixel;
NSDictionary* videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInteger:size.width], AVVideoWidthKey,
[NSNumber numberWithInteger:size.height], AVVideoHeightKey,
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInteger:bitsPerSecond], AVVideoAverageBitRateKey,
[NSNumber numberWithInteger:30], AVVideoMaxKeyFrameIntervalKey,
nil], AVVideoCompressionPropertiesKey,
nil];
AVAssetWriterInput* videoWriterInput = [[AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings] retain];
NSParameterAssert(videoWriterInput);
NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
videoWriterInput.expectsMediaDataInRealTime = YES;
[videoWriter addInput:videoWriterInput];
NSError *aerror = nil;
AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:avAsset error:&aerror];
AVAssetTrack *videoTrack = [[avAsset tracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0];
videoWriterInput.transform = CGAffineTransformRotate(videoTrack.preferredTransform, DEGREES_TO_RADIANS(180));//videoTrack.preferredTransform;
NSDictionary *videoOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
AVAssetReaderTrackOutput *asset_reader_output = [[AVAssetReaderTrackOutput alloc] initWithTrack:videoTrack outputSettings:videoOptions];
[reader addOutput:asset_reader_output];
/* Begin convert */
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];
[reader startReading];
dispatch_queue_t _processingQueue = dispatch_queue_create("assetAudioWriterQueue", NULL);
[videoWriterInput requestMediaDataWhenReadyOnQueue:_processingQueue usingBlock:
^{
while ([videoWriterInput isReadyForMoreMediaData]) {
CMSampleBufferRef sampleBuffer;
if ([reader status] == AVAssetReaderStatusReading &&
(sampleBuffer = [asset_reader_output copyNextSampleBuffer])) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
BOOL result = [videoWriterInput appendSampleBuffer:sampleBuffer];
CMSampleBufferInvalidate(sampleBuffer);
CFRelease(sampleBuffer);
sampleBuffer = nil;
if (!result) {
[reader cancelReading];
break;
}
[pool release];
} else {
[videoWriterInput markAsFinished];
switch ([reader status]) {
case AVAssetReaderStatusReading:
// the reader has more for other tracks, even if this one is done
break;
case AVAssetReaderStatusCompleted:
[videoWriter finishWriting];
[videoWriter release];
[videoWriterInput release];
[asset_reader_output release];
break;
case AVAssetReaderStatusFailed:
[videoWriter cancelWriting];
[videoWriter release];
[videoWriterInput release];
[asset_reader_output release];
break;
}
break;
}
}
}
];
[pools release];
}
+ (void) resizeVideo:(NSString*)inputVideo output:(NSString*) outputVideo size:(CGSize) size bitsPerPixel:(float) bitsPerPixel {
NSAutoreleasePool *pools = [[NSAutoreleasePool alloc] init];
NSURL *outPath = [NSURL fileURLWithPath:outputVideo];
NSURL *inputPath = [NSURL fileURLWithPath:inputVideo];
NSError *error = nil;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:outPath fileType:AVFileTypeQuickTimeMovie error:&error];
NSParameterAssert(videoWriter);
AVAsset *avAsset = [[[AVURLAsset alloc] initWithURL:inputPath options:nil] autorelease];
int numPixels = size.width * size.height;
NSLog(@"bitsPerPixel:%f",bitsPerPixel);
int bitsPerSecond = numPixels * bitsPerPixel;
NSDictionary* videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInteger:size.width], AVVideoWidthKey,
[NSNumber numberWithInteger:size.height], AVVideoHeightKey,
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInteger:bitsPerSecond], AVVideoAverageBitRateKey,
[NSNumber numberWithInteger:30], AVVideoMaxKeyFrameIntervalKey,
nil], AVVideoCompressionPropertiesKey,
nil];
AVAssetWriterInput* videoWriterInput = [[AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings] retain];
NSParameterAssert(videoWriterInput);
NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
videoWriterInput.expectsMediaDataInRealTime = YES;
[videoWriter addInput:videoWriterInput];
NSError *aerror = nil;
AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:avAsset error:&aerror];
AVAssetTrack *videoTrack = [[avAsset tracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0];
videoWriterInput.transform = CGAffineTransformRotate(videoTrack.preferredTransform, DEGREES_TO_RADIANS(180));//videoTrack.preferredTransform;
NSDictionary *videoOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
AVAssetReaderTrackOutput *asset_reader_output = [[AVAssetReaderTrackOutput alloc] initWithTrack:videoTrack outputSettings:videoOptions];
[reader addOutput:asset_reader_output];
/* Begin convert */
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];
[reader startReading];
dispatch_queue_t _processingQueue = dispatch_queue_create("assetAudioWriterQueue", NULL);
[videoWriterInput requestMediaDataWhenReadyOnQueue:_processingQueue usingBlock:
^{
while ([videoWriterInput isReadyForMoreMediaData]) {
CMSampleBufferRef sampleBuffer;
if ([reader status] == AVAssetReaderStatusReading &&
(sampleBuffer = [asset_reader_output copyNextSampleBuffer])) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
BOOL result = [videoWriterInput appendSampleBuffer:sampleBuffer];
CMSampleBufferInvalidate(sampleBuffer);
CFRelease(sampleBuffer);
sampleBuffer = nil;
if (!result) {
[reader cancelReading];
break;
}
[pool release];
} else {
[videoWriterInput markAsFinished];
switch ([reader status]) {
case AVAssetReaderStatusReading:
// the reader has more for other tracks, even if this one is done
break;
case AVAssetReaderStatusCompleted:
[videoWriter finishWriting];
[videoWriter release];
[videoWriterInput release];
[asset_reader_output release];
break;
case AVAssetReaderStatusFailed:
[videoWriter cancelWriting];
[videoWriter release];
[videoWriterInput release];
[asset_reader_output release];
break;
}
break;
}
}
}
];
[pools release];
}
Comments
Post a Comment