vendredi 8 mai 2015

Compatible Video recording for both android and iPhone

I am trying to create a video in iPhone using following settings

NSInteger audioBitRate =  64000;
unsigned int channels = 1;
double sampleRate = 44100;


NSDictionary *audioSettings = @{ AVFormatIDKey : @(kAudioFormatMPEG4AAC),
                                 AVNumberOfChannelsKey : @(channels),
                                 AVSampleRateKey :  @(sampleRate),
                                 AVEncoderBitRateKey : @(audioBitRate),
                                 };





NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [cachePaths firstObject];
NSString *filePath = [cacheDirectory stringByAppendingPathComponent:videoFileName];
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:filePath];

 movieFilePath = filePath;


NSError *errors;
assetWriter = [[AVAssetWriter alloc] initWithURL:outputURL fileType:AVFileTypeMPEG4 error:&errors];

assetWriter.shouldOptimizeForNetworkUse = YES;


NSDictionary *videoCleanApertureSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [NSNumber numberWithInt:videoWidth], AVVideoCleanApertureWidthKey,
                                            [NSNumber numberWithInt:videoHeight], AVVideoCleanApertureHeightKey,
                                            [NSNumber numberWithInt:10], AVVideoCleanApertureHorizontalOffsetKey,
                                            [NSNumber numberWithInt:10], AVVideoCleanApertureVerticalOffsetKey,
                                            nil];


CGFloat videoBitRate = 960*1000;
NSInteger videoFrameRate = 30;
NSDictionary *compressionSettings = @{ AVVideoAverageBitRateKey : @(videoBitRate),
                                       AVVideoMaxKeyFrameIntervalKey : @(videoFrameRate),
                                       AVVideoCleanApertureKey : videoCleanApertureSettings };



NSDictionary *videoSettings = @{ AVVideoCodecKey : AVVideoCodecH264,
                                 AVVideoScalingModeKey : AVVideoScalingModeResizeAspectFill, //AVVideoScalingModeResizeAspectFill //AVVideoScalingModeResizeAspect
                                 AVVideoWidthKey : @(videoWidth), //videoDimensions.width //480 //720 //486 //videoWidth
                                 AVVideoHeightKey : @(videoHeight), //videoDimensions.height //640 //1280 //videoHeight
                                 AVVideoCompressionPropertiesKey : compressionSettings };




videoWriteInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
audioWriteInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeAudio outputSettings:audioSettings];

audioWriteInput.expectsMediaDataInRealTime = YES;
videoWriteInput.expectsMediaDataInRealTime = YES;

assetWriter.movieTimeScale = 30;
videoWriteInput.mediaTimeScale = 30;

This video is in mp4 but i am not sure why Android MediaPlayer is unable to run this video. Doesn't even play in VideoView too.

What should be appropriate settings?

Aucun commentaire:

Enregistrer un commentaire