What does shouldOptimizeForNetworkUse actually do?

前端 未结 1 1704
有刺的猬
有刺的猬 2021-01-31 18:39

From the Apple documentation it just says:

When the value of this property is YES, the output file will be written in such a way that playback can start

相关标签:
1条回答
  • 2021-01-31 19:27

    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.
    0 讨论(0)
提交回复
热议问题