Recently did a project, I put the core functions of the one to share with you, the focus is to comb it yourself.
Here about the video transcoding store I've sorted out two methods, both of which are processed for the video in the album.
1, the method does not compress the video, just take the video intact from the album and put it into the sandbox path, the purpose is to get the video nsdata to upload
Here I pass a URL, this URL is a bit special, is the album file URL, so I said only for the album video Processing
Convert the URL of the original video to NSData data, write to Sandbox + (void) Videowithurl: (nsstring *) URL withfilename: (NSString *) FileName { Alassetslibrary *assetlibrary = [[Alassetslibrary alloc] init]; Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{if (URL) { [Assetlibrary Assetforurl:[nsurl Urlwithstring:url] resultblock:^ (Alasset *asset) {Alassetrepresentation *rep = [Asset defaultrepresentation]; NSString *pathdocuments = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) OBJECTATINDEX:0]; NSString *imagepath = [NSString stringwithformat:@ "%@/image", pathdocuments]; NSString *dbfilepath = [ImagePath stringbyappendingpathcomponent:filename]; char const *CVIDEOPATH = [Dbfilepath utf8string]; FILE *file = fopen (Cvideopath, "A +"); if (file) { const int buffersize = 11024 * 1024; Initializes a 1M buffer Byte *buffer = (byte*) malloc (buffersize); Nsuinteger Read = 0, offset = 0, written = 0; nserror* err = nil; if (rep.size! = 0) {do { Read = [Rep getbytes:buffer fromoffset:offset Length:buffersize error:&err]; Written = fwrite (buffer, sizeof (char), read, file); Offset + = read; } while (read! = 0 &&!err);//no end, no error, OK continue}//release buffer, close file Free (buffer); buffer = NULL; fclose (file); File = NULL; }} Failureblock:nil]; } });}
2, it is recommended to use this method, the method of compression processing of video, the degree of compression can be adjusted
Here I pass the model of the past, will my URL with the past, and then compression completed with the model to bring out the NSData, the data everyone according to their own needs free play
+ (void) Convertvideowithmodel: (Rzprojectfilemodel *) model {model.filename = [nsstring stringwithformat:@ "%ld.mp 4 ", Randomnum]; Save to Sandbox path nsstring *pathdocuments = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) OBJECTATINDEX:0]; NSString *videopath = [NSString stringwithformat:@ "%@/image", pathdocuments]; Model.sandboxfilepath = [Videopath stringByAppendingPathComponent:model.filename]; transcoding configuration Avurlasset *asset = [Avurlasset URLAssetWithURL:model.assetFilePath options:nil]; Avassetexportsession *exportsession= [[Avassetexportsession alloc] Initwithasset:asset presetname: Avassetexportpresetmediumquality]; Exportsession.shouldoptimizefornetworkuse = YES; Exportsession.outputurl = [Nsurl FileURLWithPath:model.sandBoxFilePath]; Exportsession.outputfiletype = AVFileTypeMPEG4; [exportsession exportasynchronouslywithcompletionhandler:^{int exportstatus = Exportsession.status; Rzlog (@ "%d", exportstatus); Switch (exportstatus) {case avassetexportsessionstatusfailed: { Log error to text view Nserror *exporterror = exportsession . Error; NSLog (@ "avassetexportsessionstatusfailed:%@", exporterror); Break } Case avassetexportsessionstatuscompleted: { Rzlog (@ "video transcoding success"); NSData *data = [NSData DataWithContentsOfFile:model.sandBoxFilePath]; Model.filedata = data; } } }]; }
Here you can modify the compression ratio, Apple has been encapsulated in the official, according to demand adjustment
Avassetexportsession *exportsession= [[Avassetexportsession alloc] Initwithasset:asset presetname: Avassetexportpresetmediumquality];
To modify the output type here, it is not a problem to choose MP4 under normal circumstances.
Exportsession.outputfiletype = AVFileTypeMPEG4;
Mark is compressed with this, image is the picture, 0.4 is the scale, the size is adjustable
Model.filedata = uiimagejpegrepresentation (image, 0.4);
So you're happy to get the nsdata after the transcoding, and then play it for a try
mpmovieplayerviewcontroller* Playerview = [[Mpmovieplayerviewcontroller alloc] Initwithcontenturl:[nsurl Fileurlwithpath:sandboxfilepath]]; [SUPERVC Presentviewcontroller:playerview Animated:yes Completion:nil];
Remark
You can see that I use sandbox storage here, and in the next section I'll tidy up the application sandbox with code management.
Update
Recently found a lot of people contacted me, asked me to demo, recently I also organized a bit, currently hanging on GitHub, hope the great God to correct. Https://github.com/Snoopy008/SelectVideoAndConvert
Wen/snoopy008 (author of Jane's book)
Original link: http://www.jianshu.com/p/1eeaec2ae0fa
Transferred from: http://blog.5ibc.net/p/89566.html
iOS video compression stored locally and uploaded to the server