0
- (void)makeVideoWithImages:(NSArray *)images seconds:(CGFloat)seconds block:(void (^)(NSString *videopath)) block  {  
...

UIImage *source = [images objectAtIndex:0];  
CGSize size = CGSizeMake(source.size.width*source.scale, source.size.height*source.scale);  

videoSettings = @{AVVideoCodecKey : AVVideoCodecH264,  
                  AVVideoWidthKey : [NSNumber numberWithInt:size.width],  
                  AVVideoHeightKey : [NSNumber numberWithInt:size.height]};  
self.outputWriter = [[AVAssetWriter alloc] initWithURL:url  
                                              fileType:AVFileTypeQuickTimeMovie error:&error];  

self.outputWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo  
                                                            outputSettings:videoSettings];  
[self.outputWriter addInput:self.outputWriterInput];  
NSDictionary *bufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys:  
                                  [NSNumber numberWithInt:kCVPixelFormatType_32RGBA], kCVPixelBufferPixelFormatTypeKey, nil];  
self.bufferAdapter = [[AVAssetWriterInputPixelBufferAdaptor alloc] initWithAssetWriterInput:self.outputWriterInput sourcePixelBufferAttributes:bufferAttributes];  
frameTime = CMTimeMake(seconds*600, 600);  

[self.outputWriter startWriting];  
[self.outputWriter startSessionAtSourceTime:kCMTimeZero];  

...  
}  

When I use the above method to synthesize the video, the color changes.

Preview effect: preview

And demo is Here. https://github.com/qdvictory/TestForSyncthesizeVideo

Can someone tell me what the cause is?

Seamus
  • 243
  • 1
  • 12

1 Answers1

0

When setting up the video settings the videoSettings have the AVVideoCodecKey set to AVVideoCodecH264 (according to the AVFoundation Video Settings Docs this seems to be deprecated), which - as the name implies - uses the H.264/MPEG-4 Advanced Video Codec or as put by Wikipedia:

one of the most commonly used formats for the recording, compression, and distribution of video content.

I believe that when using this format, the AVAssetWriterInput and by the documentation I am lead to thing that it is compressing the image to fit said format as it is stated:

previously-supplied media data is processed and written to the output

Which leads me to believe that the data is always compressed (processed) regardless of the original format.

To help cement my point, if you look at the image, you can see that the color in the video is "lighter" around the particles and the main focus point of the photo where the colors have a large contrast with the dark background which is a pretty good indicator that lossy compression is present.

Looking at other lossy compression formats you can see that similar behavior occurs. For example JPEG, has a nice example on its Wikipedia page of a picture with heavy compression on the left, and low compression on the right (ironically the picture is in .png format, and I am unable to post it here). Though the picture is obviously showing extreme compression, I think it's useful to see what the extreme is to understand the less extreme. It's also worth noting that although this example is a still and not a video, it's still similar enough to see what's going on.

As for a solution... I'm afraid I can't help, my expertise does not lie in Objective-C. But you should ask yourself this: How important is it that the color be 100% accurate? Would a normal user notice or even care about such a minor loss in detail?

Patrick Barr
  • 1,123
  • 7
  • 17
  • I was thinking about the possibility of also the `kCVPixelFormatType_32RGBA` changing the color range available, but I wasn't sure if that was making a conversion rather than describing what the input format is – Patrick Barr Jun 19 '17 at 14:30
  • Also sorry if my lack of experience in Objective-C makes my answer less credible. I do believe, however, that the issue stems from something that's fairly language agnostic – Patrick Barr Jun 19 '17 at 14:32