How to Compress Video File Through coding in iOS
Refer the code below to compress the video file and save the compressed file in document directory.
For such more Blogs you can visit to http://findnerd.com/NerdDigest
- - (void)compressMyVideo:(NSURL *)videoPath completionBlock:(void(^)(id data, BOOL result))block{
- NSLog(@"compressing ====%@", videoPath);
- completionBlock = block;
- NSURL *videoURL = videoPath;//[NSURL fileURLWithPath:videoPath];
- NSLog(@"video url is== %@", videoURL);
- NSString *outputPath = [self outputFilePath];
- NSURL *outputURL = [NSURL fileURLWithPath:outputPath];
- [self convertVideoToLowQuailtyWithInputURL:videoURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession)
- {
- if (exportSession.status == AVAssetExportSessionStatusCompleted)
- {
- printf("completed\n");
- completionBlock(outputURL, YES);
- }
- else
- {
- printf("error\n");
- completionBlock(nil, NO);
- }
- }];
- }
- // Getting the path of outPut file
- - (NSString *)outputFilePath{
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSString *path = [documentsDirectory stringByAppendingPathComponent:@"compressed.MOV"]; NSFileManager *fileManager = [NSFileManager defaultManager];
- // return path;
- if ([fileManager fileExistsAtPath: path])
- {
- [fileManager removeItemAtPath:path error:nil];
- }
- path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"compressed.MOV"] ];
- NSLog(@"path is== %@", path);
- return path;
- }
- // Actual compression is here.
- - (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL
- outputURL:(NSURL*)outputURL
- handler:(void (^)(AVAssetExportSession*))handler
- {
- // [self startCompressingTheVideo:outputURL];
- //
- //
- // return;
- [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
- // AVAssetWriter
- AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
- AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
- // exportSession.fileLengthLimit = 30*1024;
- exportSession.outputURL = outputURL;
- exportSession.outputFileType = AVFileTypeQuickTimeMovie;
- [exportSession exportAsynchronouslyWithCompletionHandler:^(void)
- {
- handler(exportSession);
- // [exportSession release];
- }];
- }
For such more Blogs you can visit to http://findnerd.com/NerdDigest