iOS video compression processing

Source: Internet
Author: User

Recently busy with project development, yesterday to complete the development of the project, today, take the time to share my development in the technical issues involved!

Because the recent development involves video, so video compression, upload, play and a series of functions are to be involved, so here, I will share with you video compression!

IOS video compression problem, I also found information on the Internet, but not much, and not enough detailed comprehensive, I wrote a small compression demo, using the system is a class library

#import <AVFoundation/AVFoundation.h> Avassetexportsession This class, the official API is explained in this way, avassetexportsession is the Avasset object content transcoding, and output to the established path;

-(Nullable Instancetype) Initwithasset: (Avasset *) Asset Presetname: (NSString *) Presetname Ns_designated_initializer; Initialize method

Asset, the parameter is the asset object to Transcode, Presetname, this parameter is the way to transcode the name, the string type, the system has a given type value,

Avassetexportpresetlowquality

Avassetexportpresetmediumquality

Vassetexportpresethighestquality

avassetexportpreset640x480

avassetexportpreset1280x720

Outputurl, for the URL of the output content (Specify a file path, and then initialize a URL based on the path, assign to, Outputurl)

Outputfiletype, the format type of the video content for output compression

Below directly on the code, if there are errors, or there is a bad place, welcome to correct me, I humbly learn to ask, and everyone to encourage!

//Created by Ios-developer on 16/2/19.//copyright©2016 year Ios-jessonliu. All rights reserved.//#import "JFCompressionVideo.h"#import<AVFoundation/AVFoundation.h>#defineCompressionvideopaht [Nshomedirectory () stringbyappendingformat:@ "/documents/compressionvideofield"]@interfaceJfcompressionvideo ()@end@implementationJfcompressionvideo+ (void) Compressedvideoothermethodwithurl: (nsurl *) URL Compressiontype: (NSString *) Compressiontype Compressionresultpath: (compressionsuccessblock) resultpathblock {nsstring*Resultpath; NSData*data =[NSData Datawithcontentsofurl:url]; CGFloat totalsize= (float) Data.length/1024x768/1024x768; Avurlasset*avasset =[Avurlasset Urlassetwithurl:url Options:nil]; Nsarray*compatiblepresets =[Avassetexportsession Exportpresetscompatiblewithasset:avasset]; //Whether the compression format is selected in the supported compression formats    if([compatiblepresets Containsobject:compressiontype]) {avassetexportsession*exportsession =[[Avassetexportsession alloc] Initwithasset:avasset Presetname:compressiontype]; NSDateFormatter*formater = [[NSDateFormatter alloc] init];//Use time to rename files, prevent video storage from overwriting,[formater Setdateformat:@"Yyyy-mm-dd-hh:mm:ss"]; Nsfilemanager*manager =[Nsfilemanager Defaultmanager]; BOOL isexists=[manager Fileexistsatpath:compressionvideopaht]; if(!isexists) {[Manager createdirectoryatpath:compressionvideopaht Withintermediatedirectories:yes Attributes:ni        L Error:nil]; } Resultpath= [Compressionvideopaht stringbyappendingpathcomponent:[nsstring stringWithFormat:@"Outputjfvideo-%@.mov", [formater stringfromdate:[nsdate Date]]]; Jflog (@"Resultpath =%@", Resultpath); Exportsession.outputurl=[Nsurl Fileurlwithpath:resultpath]; Exportsession.outputfiletype=AVFileTypeMPEG4; Exportsession.shouldoptimizefornetworkuse=YES; [Exportsession Exportasynchronouslywithcompletionhandler:^(void)                  {             if(Exportsession.status = =avassetexportsessionstatuscompleted) {NSData*data =[NSData Datawithcontentsoffile:resultpath]; floatMemorysize = (float) Data.length/1024x768/1024x768; Jflog (@"video Compression size%f", memorysize);                              Resultpathblock (Resultpath, memorysize); } Else{Jflog (@"Compression failed");            }                      }]; } Else{Jflog (@"compression in%@ format is not supported", Compressiontype); }}


/**

* Clear the Sandbox file, compressed video all, after using the compressed file, do not use again, you can call the method, delete

*/

+ (void) removecompressedvideofromdocuments {    *manager = [Nsfilemanager Defaultmanager];     if  ([manager Fileexistsatpath:compressionvideopaht]) {        [[Nsfilemanager Defaultmanager] Removeitematpath:compressionvideopaht Error:nil];}    } @end

iOS video compression processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.