美文网首页
音视频开发 - pcm、yuv、rgb数据存储

音视频开发 - pcm、yuv、rgb数据存储

作者: 铁头娃_e245 | 来源:发表于2019-08-08 23:04 被阅读0次

    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;
        
    }
    

    相关文章

      网友评论

          本文标题:音视频开发 - pcm、yuv、rgb数据存储

          本文链接:https://www.haomeiwen.com/subject/tlbuqctx.html