2

I have a problem in Compressed video data (video data is gotten from ALASSET). I want to compress video data before uploading to server. I found the below function for converting to low quality but output is NSURL not NSDATA. How can I compress NSData of video before uploading.

This is my upload function:

ALAsset *alasset = [allVideos objectAtIndex:i];
ALAssetRepresentation *rep = [alasset defaultRepresentation];
NSString * videoName = [rep filename];
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

And this is convert function:

    - (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:AVAssetExportPresetLowQuality];
    exportSession.outputURL = outputURL;
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void)
     {
         handler(exportSession);
         [exportSession release];
     }];
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
user3214941
  • 387
  • 1
  • 9
  • 17

1 Answers1

-3

Below code is working perfectly for me:

asset = [[AssetItem alloc] initWithURL:movieURL];
            NSLog(@"Start");
            asset.exportSession.shouldOptimizeForNetworkUse = TRUE;
            asset.preset = AVAssetExportPresetMediumQuality;
            asset.exportSession.outputFileType = @"public.mpeg-4";
            [asset exportAssetWithCompletionHandler:^(NSError *error){
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (error != nil)
                    {
                        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[error localizedDescription]
                                                                            message:[error localizedFailureReason] delegate:self
                                                                  cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
                        [alertView show];
                    }
                });
            }];
halfer
  • 19,824
  • 17
  • 99
  • 186
Yashesh
  • 1,799
  • 1
  • 11
  • 29
  • What kind of class is `AssetItem`? I only know `AVAsset`. I tried the exactly same code than the thread starter, but got always a "could not complete" error. Unfortunately the error is very unspecific. – dannyyy Jul 16 '14 at 12:05
  • https://developer.apple.com/library/ios/samplecode/AVMovieExporter/Listings/AVMovieExporter_AssetItem_h.html – Yashesh Jul 16 '14 at 13:02
  • 1
    -1 -- This is incomplete information,if download the Apple sample code from the link provided in the comment,it is noticed that -- "AssetItem" actually is subclass of "NSObject". – Shailesh Aug 14 '14 at 15:25