GOP is missing in result file using AVAssetWriter on macOS

I'm trying to save h.264 frames that comes to the app from network into a .mov file. Using AVAssetWriter with AVAssetWriterInput appending CMSampleBuffer.

Output settings are nil which means that frames will not be encoded during saving:

Code Block
videoWriter = AVAssetWriterInput(mediaType: .video, outputSettings: nil, sourceFormatHint: format)
videoWriter.append(buffer)


I have the code that saves frames to a file. Tried to run it in iOS app and in macOS app using same frames data

iOS result video file plays correctly
macOS result video file plays with artefacts

I've inspected both result files with MediaInfo and all the data is equal except 1 line (bold line):
Code Block
Video
ID : 1
Format : AVC
Format/Info : Advanced Video Codec
Format profile : High@L4
Format settings : CABAC / 1 Ref Frames
Format settings, CABAC : Yes
Format settings, Reference frames : 1 frame
Format settings, GOP : M=1, N=15
Codec ID : avc1
Codec ID/Info : Advanced Video Coding
Duration : 9 s 398 ms
Source duration : 9 s 432 ms
Bit rate mode : Variable
Bit rate : 14.3 Mb/s
Width : 1 920 pixels
Height : 1 080 pixels
Display aspect ratio : 16:9
Frame rate mode : Variable
Frame rate : 30.000 FPS
Minimum frame rate : 30.000 FPS
Maximum frame rate : 31.579 FPS
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) : 0.231
Stream size : 16.1 MiB (100%)
Source stream size : 16.1 MiB (100%)
Title : Core Media Video
Encoded date : UTC 2021-03-04 13:30:21
Tagged date : UTC 2021-03-04 13:30:23
Codec configuration box : avcC


iOS result file has this line and macOS result file missing it:
Format settings, GOP : M=1, N=15

Is there a way to provide some settings to AVAssetWriter/AVAssetWriterInput to have correct Group of Pictures in result file on macOS?

The goal is to save raw H.264 stream to file without additional encoding.


Accepted Reply

Just want to add a comment that it wasn't a bug or inconsistency between platforms. Maybe AVAssetWriter works a little bit different on macOS side, but the problem was related to overwriting data in memory on macOS.

Looks like AVAssetWriter is caching data before actually writing it to disk and that data was incorrect already at that moment.

I used Feedback Assistant and Apple team found a bug in my code, they've suggested that I need to copy frame data before appending it.

Media Engineer  thanks for your help :)

  • I'm glad to hear you got it sorted out!

Add a Comment

Replies

The inconsistency between platforms suggests that this is unexpected behavior. Please file a bug report using the feedback assistant. Thanks!

Just want to add a comment that it wasn't a bug or inconsistency between platforms. Maybe AVAssetWriter works a little bit different on macOS side, but the problem was related to overwriting data in memory on macOS.

Looks like AVAssetWriter is caching data before actually writing it to disk and that data was incorrect already at that moment.

I used Feedback Assistant and Apple team found a bug in my code, they've suggested that I need to copy frame data before appending it.

Media Engineer  thanks for your help :)

  • I'm glad to hear you got it sorted out!

Add a Comment