3

I am trying to select a video from UIPickerViewController, compress the video into about 1.5MB, and post it to a server.

I am already able to select an image and post successfully. I am also able to select a video and post it successfully. The problem is that since the video file is about 27MB, it is too big to playback once sent. So when trying to play back the video from the server, the time shows "--". If I select a smaller video, it will post and play back perfectly fine.

I know that the problem is size because I used another app to compress the video and then used my app to send it to the server. It worked perfectly.

I have spent over 5 hours on this problem and can't seem to figure it out myself. (Just saying that I have tried to solve it and find the answers myself)

I have tried implementing these techniques:

https://gist.github.com/daltheman/4716ec10d6d0f71aba56

https://developer.apple.com/library/tvos/documentation/Performance/Reference/Compression/index.html#//apple_ref/c/func/compression_encode_buffer

https://snackcoffee.me/2015/11/29/zlib-swift-2/

IOS Video Compression Swift iOS 8 corrupt video file

and a few others that are no longer in my tabs. Some of which were in Objective-C but I couldn't seem to translate them well.

TL;DR Is there a function like UIImageJPEGRepresentation for videos in AVFoundation? Can you please post a basic code example of picking an image from UIPicker and changing its file size to 1.5MB?

Community
  • 1
  • 1
Kilo Loco
  • 529
  • 5
  • 10

1 Answers1

0

I had a similiar in task in one of the projects and used AVAssetExportSession to compress video:

    - (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL
                               outputURL:(NSURL*)outputURL
                                 handler:(void (^)(AVAssetExportSession*))handler
{
[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
exportSession.outputURL = outputURL;
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
 {
     handler(exportSession);
 }];}

You can choose a few Quality types:

AVF_EXPORT NSString *const AVAssetExportPresetLowQuality        NS_AVAILABLE(10_11, 4_0);
AVF_EXPORT NSString *const AVAssetExportPresetMediumQuality     NS_AVAILABLE(10_11, 4_0);
AVF_EXPORT NSString *const AVAssetExportPresetHighestQuality    NS_AVAILABLE(10_11, 4_0);

And then check the output size with function like this:

- (NSString *)sizeOfLocalURL:(NSURL *)url {
NSData *fileData = [[NSData alloc] initWithContentsOfURL:url];
return [NSString stringWithFormat:@"%.2f mb", fileData.length/1000000.00];
}

Hope it helps :)

Alex Kosyakov
  • 2,095
  • 1
  • 17
  • 25
  • Yea theres code exactly like this in another question and I've tried to implement it, but since i don't know objective c that well (i code in swift), something must be getting lost in translation. – Kilo Loco Apr 18 '16 at 19:47