这两天由于在做关于视频方面的东西,因为要上传到服务器,所以就涉及到视频的压缩和从视频中截取第几帧图片,同时根据展示的需要来把获取到的图片进行定高或者定宽,以及同比例的缩放等。所以就整理了一下这方面的东西,供大家参考:
视频压缩:用的是以下这个方法,
-(void)convertVideoWithUrl:(NSURL*)inputURL outUrl:(NSURL*)outputUrl
这里的inputURL是你自己采集到的视频的url,这里的outputUrl是你压缩之后需要保存的视频的url。大家用的时候,这两个URL要分清楚,因为我看了许多网上的都没怎么说清楚这两个区别,导致视频压缩会出现一直失败的情况。这里outputUrl我用的是以下这个保存压缩之后的视频。这个可根据自己的项目的需要进行修改。
#pragma mark 保存压缩 - (NSURL *)compressedURL{ NSDateFormatter *formater = [[NSDateFormatter alloc] init]; [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"]; return [NSURL fileURLWithPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@outPut.mp4",[formater stringFromDate:[NSDate date]]]]]; }
//此方法为获取视频的大小
- (CGFloat)fileSize:(NSURL *)path{ return [[NSData dataWithContentsOfURL:path] length]/1024.00 /1024.00; }
压缩完之后,你自己可以看到压缩之后的视频大小相对于没压缩的,会小很多。
从视频中获取某一帧图片,来进行封面展示:
方法一:
-(UIImage *)getImage:(NSString *)videoURL{ AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoURL] options:nil]; AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset]; gen.appliesPreferredTrackTransform = YES; CMTime time = CMTimeMakeWithSeconds(0.0, 600); NSError *error = nil; CMTime actualTime; CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error]; UIImage *thumb = [[UIImage alloc] initWithCGImage:image]; CGImageRelease(image); return thumb; }
方法二:
- (UIImage *)firstFrameWithVideoURL:(NSURL *)url size:(CGSize)size { // 获取视频第一帧 NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumbernumberWithBool:NO]forKey:AVURLAssetPreferPreciseDurationAndTimingKey]; AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:url options:opts]; AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset]; generator.appliesPreferredTrackTransform = YES; generator.maximumSize = CGSizeMake(size.width, size.height); NSError *error = nil; CGImageRef img = [generator copyCGImageAtTime:CMTimeMake(0, 10) actualTime:NULL error:&error]; { return [UIImage imageWithCGImage:img]; } return nil;
}
图片定高压缩:传入图片的UIImage,以及你需要压缩后变为多高的值
-(UIImage *) imageCompressForWidthScale:(UIImage *)sourceImage targetHeight:(CGFloat)defineHeight{ UIImage *newImage = nil; CGSize imageSize = sourceImage.size; CGFloat width = imageSize.width; CGFloat height = imageSize.height; CGFloat targetHeight = defineHeight; CGFloat targetWidth = height / (width / targetHeight); CGSize size = CGSizeMake(targetWidth, targetHeight); CGFloat scaleFactor = 0.0; CGFloat scaledWidth = targetWidth; CGFloat scaledHeight = targetHeight; CGPoint thumbnailPoint = CGPointMake(0.0, 0.0); if(CGSizeEqualToSize(imageSize, size) == NO){ CGFloat widthFactor = targetWidth / width; CGFloat heightFactor = targetHeight / height; if(widthFactor > heightFactor){ scaleFactor = widthFactor; } else{ scaleFactor = heightFactor; } scaledWidth = width * scaleFactor; scaledHeight = height * scaleFactor; if(widthFactor > heightFactor){ thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; }else if(widthFactor < heightFactor){ thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; } } UIGraphicsBeginImageContext(size); CGRect thumbnailRect = CGRectZero; thumbnailRect.origin = thumbnailPoint; thumbnailRect.size.width = scaledWidth; thumbnailRect.size.height = scaledHeight; [sourceImage drawInRect:thumbnailRect]; newImage = UIGraphicsGetImageFromCurrentImageContext(); if(newImage == nil){ NSLog(@"scale image fail"); } UIGraphicsEndImageContext(); return newImage; }
图片定宽压缩:传入图片的UIImage,以及你需要压缩后变为多宽的值
-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{ UIImage *newImage = nil; CGSize imageSize = sourceImage.size; CGFloat width = imageSize.width; CGFloat height = imageSize.height; CGFloat targetWidth = defineWidth; CGFloat targetHeight = height / (width / targetWidth); CGSize size = CGSizeMake(targetWidth, targetHeight); CGFloat scaleFactor = 0.0; CGFloat scaledWidth = targetWidth; CGFloat scaledHeight = targetHeight; CGPoint thumbnailPoint = CGPointMake(0.0, 0.0); if(CGSizeEqualToSize(imageSize, size) == NO){ CGFloat widthFactor = targetWidth / width; CGFloat heightFactor = targetHeight / height; if(widthFactor > heightFactor){ scaleFactor = widthFactor; } else{ scaleFactor = heightFactor; } scaledWidth = width * scaleFactor; scaledHeight = height * scaleFactor; if(widthFactor > heightFactor){ thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; }else if(widthFactor < heightFactor){ thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; } } UIGraphicsBeginImageContext(size); CGRect thumbnailRect = CGRectZero; thumbnailRect.origin = thumbnailPoint; thumbnailRect.size.width = scaledWidth; thumbnailRect.size.height = scaledHeight; [sourceImage drawInRect:thumbnailRect]; newImage = UIGraphicsGetImageFromCurrentImageContext(); if(newImage == nil){ NSLog(@"scale image fail"); } UIGraphicsEndImageContext(); return newImage; }
图片等比例压缩:传入图片的UIImage,以及你需要按照什么大小来压缩的值
//图片等比例压缩 有损压缩 -(UIImage *) imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{ UIImage *newImage = nil; CGSize imageSize = sourceImage.size; CGFloat width = imageSize.width; CGFloat height = imageSize.height; CGFloat targetWidth = size.width; CGFloat targetHeight = size.height; CGFloat scaleFactor = 0.0; CGFloat scaledWidth = targetWidth; CGFloat scaledHeight = targetHeight; CGPoint thumbnailPoint = CGPointMake(0.0, 0.0); if(CGSizeEqualToSize(imageSize, size) == NO){ CGFloat widthFactor = targetWidth / width; CGFloat heightFactor = targetHeight / height; if(widthFactor > heightFactor){ scaleFactor = widthFactor; } else{ scaleFactor = heightFactor; } scaledWidth = width * scaleFactor; scaledHeight = height * scaleFactor; if(widthFactor > heightFactor){ thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; }else if(widthFactor < heightFactor){ thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; } } UIGraphicsBeginImageContext(size); CGRect thumbnailRect = CGRectZero; thumbnailRect.origin = thumbnailPoint; thumbnailRect.size.width = scaledWidth; thumbnailRect.size.height = scaledHeight; [sourceImage drawInRect:thumbnailRect]; newImage = UIGraphicsGetImageFromCurrentImageContext(); if(newImage == nil){ NSLog(@"scale image fail"); } UIGraphicsEndImageContext(); return newImage; }
网友评论