Some devices (typically Android devices) can't read videos I record using an iphone with ImagePicker API in iOS. So I searched for some way to convert the baseline to level 3.0 profile after taking the video. I found a library(https://github.com/rs/SDAVAssetExportSession) that provides easy way to use AVAssetWriterInput and I use this code:
- (IBAction)takeVideo:(id)sender
{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
NSArray *availableTypes = [UIImagePickerController
availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
ipc.mediaTypes = availableTypes;
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
[ipc setVideoMaximumDuration:10];
ipc.delegate = self;
ipc.videoQuality = UIImagePickerControllerQualityTypeMedium;
if ([availableTypes containsObject:(__bridge NSString *)kUTTypeMovie]) {
[ipc setMediaTypes:@[(__bridge NSString *)kUTTypeMovie]];
}
[self presentViewController:ipc animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *mediaURL = info[UIImagePickerControllerMediaURL];
if (mediaURL) {
SDAVAssetExportSession *encoder = [SDAVAssetExportSession.alloc initWithAsset:[AVAsset assetWithURL:mediaURL]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
self.videoPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"myvideo.mov"]];
self.url = [NSURL fileURLWithPath:self.videoPath];
encoder.outputURL=self.url;
encoder.outputFileType = AVFileTypeMPEG4;
encoder.shouldOptimizeForNetworkUse = YES;
encoder.videoSettings = @
{
AVVideoCodecKey: AVVideoCodecH264,
AVVideoWidthKey: @1920,
AVVideoHeightKey: @1080,
AVVideoCompressionPropertiesKey: @
{
AVVideoAverageBitRateKey: @6000000, // Lower bit rate here
AVVideoProfileLevelKey: AVVideoProfileLevelH264Baseline30,//This is what I want
},
};
encoder.audioSettings = @
{
AVFormatIDKey: @(kAudioFormatMPEG4AAC),
AVNumberOfChannelsKey: @2,
AVSampleRateKey: @44100,
AVEncoderBitRateKey: @128000,
};
[encoder exportAsynchronouslyWithCompletionHandler:^
{
int status = encoder.status;
if (status == AVAssetExportSessionStatusCompleted)
{
AVAssetTrack *videoTrack = nil;
AVURLAsset *asset = [AVAsset assetWithURL:encoder.outputURL];
NSArray *videoTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
videoTrack = [videoTracks objectAtIndex:0];
float frameRate = [videoTrack nominalFrameRate];
float bps = [videoTrack estimatedDataRate];
NSLog(@"Frame rate == %f",frameRate);
NSLog(@"bps rate == %f",bps/(1024.0 * 1024.0));
NSLog(@"Video export succeeded");
[self dismissViewControllerAnimated:YES completion:^{
//some instructions...
}];
}
else if (status == AVAssetExportSessionStatusCancelled)
{
NSLog(@"Video export cancelled");
}
else
{
NSLog(@"Video export failed with error: %@ (%ld)", encoder.error.localizedDescription, (long)encoder.error.code);
}
}];
}
}
But I do get the error "Video export failed with error: The operation could not be completed (-11800)". I am pretty sure this is because my videoSettings are wrong/incompatible with baseline 3.0.
How to get the baseline 3.0 working?