IOS Video Compression

Source: Internet
Author: User

Recently, I encountered a problem when uploading a video, that is, when uploading the recorded video, the size is too large, especially when recording a p video, however, when you select a video from the iPad library or album, the system will compress the video.
. Later I found that there are several APIs in IOS that can be used to compress videos (the reencode should be accurate ). These APIs are included in avfoundation. I hope to help my friends who encounter the same problem. Skip this step if you know it.




Wrote a convenient method


-(Void) lowquailtywithinputurl :( nsurl *) inputurl

Outputurl :( nsurl *) outputurl

Blockhandler :( void (^) (avassetexportsession *) Handler

{

Avurlasset * asset = [avurlasset urlassetwithurl: inputurl opti *****: Nil];

Avassetexportsession * session = [[avassetexportsession alloc] initwithasset: asset presetname: avassetexportpresetmediumquality];

Session. outputurl = outputurl;

Session. outputfiletype = avfiletypequicktimemovie;

[Session exportasynchronouslywithcompletionhandler: ^ (void)

{

Handler (session );

}];

}


The specific usage is


[Self lowquailtywithinputurl: Video outputurl: Output blockhandler: ^ (avassetexportsession * session)

{

If (session. Status = avassetexportsessi ***** tatuscompleted)

{



}

Else

{





}

}];


Check the block successfully, fail, or cancel, and then release the session.


During this period, you can continuously view the progress attribute of the session to obtain the conversion progress.


You can set the compression quality.


Avf_export nsstring * C ***** t avassetexportpresetlowquality ns_available_ios (4_0 );

Avf_export nsstring * C ***** t avassetexportpresetmediumquality ns_available_ios (4_0 );

Avf_export nsstring * C ***** t avassetexportpresethighestquality ns_available_ios (4_0 );


After my observations, we found that the system's compression and avassetexportpresetmediumquality are equivalent to 480*640 of the 66 m video, and then 15 m after compression.


I have been compressing frames by frame until I found this method, that is, writing video frames to the file while compressing them. This causes the video to be very unclear.

After that, the video quality will not suffer much, and the file size and definition of the video can be balanced.


If the shouldoptimizefornetworkuse attribute of the session is set, the effect will be better.


The following is an official example,

Https://developer.apple.com/library/ios/#samplecode/RosyWriter/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011110


This example shows how to manually set the bit rate and FPS when writing a video file.

At the same time, this example is very similar to the real-time filter method on the iPhone.

Turn: http://www.cocoachina.com/bbs/read.php? Tid = 106768

Related Article

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.