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