Home » IOS » IOS video capture and compression

IOS video capture and compression


A recent study using iOS built-in API for video compression, video capture and so on from the beginning, because once wanted to direct the video compression, but always failed, the study found that can not be directly invoked in PC video file compression, so AVAssetExportSessionStatusFailed. can only be used directly or testing and call in iPhone video. ADO, code:



Use UIImagePickerController to complete video capture, and stored in

custom directory


The

method is as follows


- (IBAction) start: (ID) sender

{

UIImagePickerController*ipc=[[UIImagePickerControlleralloc]init];

Ipc.sourceType=UIImagePickerControllerSourceTypeCamera; //sourcetype three are camera, photoLibrary and photoAlbum

NSArray*availableMedia=[UIImagePickerControlleravailableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]; what are supported by the //Camera Media format, a total of two are @ "public.image", "public.movie" @

Ipc.mediaTypes=[NSArrayarrayWithObject:availableMedia[1]]; / / set the media type for public.movie

[selfpresentViewController:ipcanimated:YEScompletion:nil];

Ipc.videoMaximumDuration=30.0f; //30
.
Ipc.delegate=self; / / set by

[ipcrelease];

}



The three value of the above mentioned ipc.sourceType, camera refers to call the camera to shoot, and photoLibrary refers to all the pictures in the mobile phone, photoAlbum is simply refers to the pictures in the album. The rest do not do much to explain.



Then some

finished in the following principal method


- (void) imagePickerController: (UIImagePickerController*) pickerdidFinishPickingMediaWithInfo: (NSDictionary*) info

{

SourceURL=[[infoobjectForKey:UIImagePickerControllerMediaURL]retain];



FileLenLabel.text=[NSStringstringWithFormat:@ "%fs" [selfgetVideoLength:sourceURL]]; / / the URL why can do, because there must be such a

FileSizeLabel.text=[NSStringstringWithFormat:@ "%fkb" [selfgetFileSize:[[sourceURLabsoluteString]substringFromIndex:16]]]; / / and no storage refers to where the sourceURL file, because it added so will this string is removed, the Label is used in engineering test show that the file size of the label
shooting
NSLog ([[sourceURLabsoluteString]substringFromIndex:16]);

[selfdismissViewControllerAnimated:YEScompletion:nil];

}



Well, here have taken good video storage in the sourceURL. The following compression of



- (IBAction) convert: (ID) sender

File not already exist {// conversion, otherwise an error

AVURLAsset*avAsset=[AVURLAssetURLAssetWithURL:sourceURLoptions:nil];

NSArray*compatiblePresets=[AVAssetExportSessionexportPresetsCompatibleWithAsset:avAsset];

If ([compatiblePresetscontainsObject:resultQuality]) {

AVAssetExportSession*exportSession=[[AVAssetExportSessionalloc]initWithAsset:avAssetpresetName:resultQuality];

NSDateFormatter*formater=[[NSDateFormatteralloc]init]; / / with the time for the full name of the file, to avoid duplication, when the test can determine whether a file exists if it exists, delete, re generated files

[formatersetDateFormat:@ "yyyy-MM-dd-HH:mm:ss"];

ResultPath=[[NSHomeDirectory (stringByAppendingFormat:@) "/Documents/output-%@.mp4" [formaterstringFromDate:[NSDatedate]]]retain];

NSLog (resultPath);

[formaterrelease];

ExportSession.outputURL=[NSURLfileURLWithPath:resultPath];

ExportSession.outputFileType=AVFileTypeMPEG4;

[exportSessionexportAsynchronouslyWithCompletionHandler:^ (void)

{

Switch (exportSession.status) {

CaseAVAssetExportSessionStatusUnknown:

NSLog (@ AVAssetExportSessionStatusUnknown);

Break;

CaseAVAssetExportSessionStatusWaiting:

NSLog (@ AVAssetExportSessionStatusWaiting);

Break;

CaseAVAssetExportSessionStatusExporting:

NSLog (@ AVAssetExportSessionStatusExporting);

Break;

CaseAVAssetExportSessionStatusCompleted:

NSLog (@ AVAssetExportSessionStatusCompleted);



Break;

CaseAVAssetExportSessionStatusFailed:

NSLog (@ AVAssetExportSessionStatusFailed);

Break;

}

[exportSessionrelease];

}];

}

}



Do some shows, when the video shoot is a parameter to set the shooting quality, three kinds of values of UIImagePickerControllerQualityTypeHigh, Medium, Low, but after the test found that the three parameters of the shooting effect has no influence, there is also a three parameter value when compressed (for iPhone only three. There are other devices for different resolution such as 640*480, but they are not suitable for iPhone, and some of the PC) of these three values are AVAssetExportPresetMediumQuality, Highest, Low, Highest and Medium in self feeling and much difference, clarity, compressed file size is almost the same, but the Low is small if, in a video with Medium (or Highest) size is more than 5M, if Low is about 600KB, but the Low is relatively fuzzy many. Selecting Medium.



Here again on how to get the file size and the length of the video to do a little



- (CGFloat) getFileSize: (NSString*) path

{

NSFileManager*fileManager=[[[NSFileManageralloc]init]autorelease];

Floatfilesize=-1.0;

If ([fileManagerfileExistsAtPath:path]) {

NSDictionary*fileDic=[fileManagerattributesOfItemAtPath:patherror:nil]; / / get file attribute

Unsignedlonglongsize=[[fileDicobjectForKey:NSFileSize]longLongValue];

Filesize=1.0*size/1024;

}

Returnfilesize;

This method can obtain} returns the file size, the unit is KB.


- (CGFloat) getVideoLength: (NSURL*) URL

{

NSDictionary*opts=[NSDictionarydictionaryWithObject:[NSNumbernumberWithBool:NO]

ForKey:AVURLAssetPreferPreciseDurationAndTimingKey];

AVURLAsset*urlAsset=[AVURLAssetURLAssetWithURL:URLoptions:opts];

Floatsecond=0;

Second=urlAsset.duration.value/urlAsset.duration.timescale;

Returnsecond;

This method can get the video file} the length of time.

Latest