pcm数据流存储到.pcm音频文件
//数据源枚举
@interface AudioSample : NSObject
@property (nonatomic, assign) long dataPtr;
@property (nonatomic, assign) int numOfSamples;
@property (nonatomic, assign) int bytesPerSample;
@property (nonatomic, assign) int numOfChannels;
@property (nonatomic, assign) int samplesPerSec;
@end
//文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/pcm/"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) { //如果没有路径则创建
[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString * dataPath = [path stringByAppendingPathComponent:@"pub_audio.pcm"];
int bufferSize = audioSample.numOfSamples * audioSample.bytesPerSample * audioSample.numOfChannels;
void *audioSampleBufferPtr = NULL;
if(bufferSize) {
audioSampleBufferPtr = malloc(bufferSize);
if(audioSampleBufferPtr) {
memcpy(audioSampleBufferPtr, (void *)audioSample.dataPtr, bufferSize);
}
}
FILE *file = fopen([dataPath UTF8String], "ab");
fwrite(audioSampleBufferPtr, 1, bufferSize, file);
fclose(file);
if (audioSampleBufferPtr) {
free(audioSampleBufferPtr);
audioSampleBufferPtr = NULL;
}
yuv数据流存储到.yuv视频文件
uint8_t *outputData = NULL;
int32_t w = videoSample.width;
int32_t h = videoSample.height;
if (outputData == NULL)
outputData = (uint8_t*)malloc(w*h*3/2);
}
memcpy(outputData, (uint8_t*)videoSample.dataYPtr,w*h*3/2);
memcpy(outputData + w*h, (uint8_t*)videoSample.dataUPtr, w*h/4);
memcpy(outputData + w*h*5/4, (uint8_t*)videoSample.dataVPtr, w*h/4);
fwrite(outputData, 1, w*h*3/2, file);
if (outputData) {
free(outputData);
outputData = NULL;
}
rgb数据存储为图片
UIImage *image = [self imageFromRGBData:(void *)videoSample.dataPtr width:videoSample.width height:videoSample.height];
NSString *filePath =[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/rgb.jpg"];
NSData *data = UIImageJPEGRepresentation(image, 0.1);
[data writeToFile:filePath atomically:YES];
- (UIImage *)imageFromRGBData:(void *)data width:(int)width height:(int)height {
CGColorSpaceRef rgbSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(data, width, height, 8, width*4, rgbSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little );
CGImageRef imageRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return image;
}
网友评论