YPImagePicker icon indicating copy to clipboard operation
YPImagePicker copied to clipboard

Adding option to reduce the bit rate of bigger video files

Open riazhazan opened this issue 5 years ago • 3 comments

Is your feature request related to a problem? Please describe. I can set the compression of video, but I am not able to set the bit rate of a video which drastically reduces the file size

Describe the solution you'd like just like compression option, it would be better if we could add a set bitrate option

riazhazan avatar Sep 24 '20 14:09 riazhazan

@riazhazan write here if ios SDK give such an API.

NikKovIos avatar Sep 25 '20 13:09 NikKovIos

@NikKovIos

I am adding Objective c code. The below code will help to do the compression. In below code you can compress video by specifying resolution and bitrate

`- (void)compressVideoWithInputVideoUrl:(NSURL ) inputVideoUrl { / Create Output File Url */

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *finalVideoURLString = [documentsDirectory stringByAppendingPathComponent:@"compressedVideo.mp4"];
NSURL *outputVideoUrl = ([[NSURL URLWithString:finalVideoURLString] isFileURL] == 1)?([NSURL URLWithString:finalVideoURLString]):([NSURL fileURLWithPath:finalVideoURLString]); // Url Should be a file Url, so here we check and convert it into a file Url


SDAVAssetExportSession *compressionEncoder = [SDAVAssetExportSession.alloc initWithAsset:[AVAsset assetWithURL:inputVideoUrl]]; // provide inputVideo Url Here
compressionEncoder.outputFileType = AVFileTypeMPEG4;
compressionEncoder.outputURL = outputVideoUrl; //Provide output video Url here
compressionEncoder.videoSettings = @
{
AVVideoCodecKey: AVVideoCodecH264,
AVVideoWidthKey: @800,   //Set your resolution width here
AVVideoHeightKey: @600,  //set your resolution height here
AVVideoCompressionPropertiesKey: @
    {
    AVVideoAverageBitRateKey: @45000, // Give your bitrate here for lower size give low values
    AVVideoProfileLevelKey: AVVideoProfileLevelH264High40,
    },
};
compressionEncoder.audioSettings = @
{
AVFormatIDKey: @(kAudioFormatMPEG4AAC),
AVNumberOfChannelsKey: @2,
AVSampleRateKey: @44100,
AVEncoderBitRateKey: @128000,
};

[compressionEncoder exportAsynchronouslyWithCompletionHandler:^
 {
     if (compressionEncoder.status == AVAssetExportSessionStatusCompleted)
     {
        NSLog(@"Compression Export Completed Successfully");
     }
     else if (compressionEncoder.status == AVAssetExportSessionStatusCancelled)
     {
         NSLog(@"Compression Export Canceled");
     }
     else
     {
          NSLog(@"Compression Failed");

     }
 }];

}`

riazhazan avatar Oct 01 '20 10:10 riazhazan

I found an implementation to copy from, but not have time to do it now. https://github.com/VideoFlint/VIExportSession/tree/master/VIExportSession

NikKovIos avatar Oct 08 '20 10:10 NikKovIos