What does shouldOptimizeForNetworkUse actually do? What does shouldOptimizeForNetworkUse actually do? objective-c objective-c

What does shouldOptimizeForNetworkUse actually do?


When shouldOptimizeForNetworkUse is set to YES calling finishWriting will move the MP4 moov atom (movie atom) from the end of the file to the beginning of the file. The moov atom contains information about the movie file like timescale and duration. The moov also contains "subatoms" which contain information like the tracks, the data offsets in the file etc.

Playback is only possible using the information in the moov atom and when it is located in the beginning of the file rather than in the end of the file, a movie player can begin playing the file even if it has only loaded the beginning of it.

In practice modern browsers will fetch the moov atom from the end of the file using HTTP/1.1 Range header to load the moov atom even if it is at the end of the file.

Setting the shouldOptimizeForNetworkUse to YES also has a few side-effects:

  • Finishing the movie takes a bit longer because the file has to be modified along with the offsets in the atom.
  • The output file is locked and not accessible because finishWriting will need to modify the whole file (move and update atoms). When set to NO, the movieFragmentInterval sets the flush interval for writing to the output video file, making the file readable during writing as the fragments are flushed to the file in their final form. The finishWriting will just need to append the moov atom to the end of the file instead of touching the already written fragments.