title: replaceFormatDescription:withFormatDescription:
body: <p>I am having trouble with customized CMFormatDescription in AVMutableMovieTrack.</p>
  
  <p>It seems to work as intended though, the modification seems to be volatile and I am unable to write out modified formatDescription into movie header. I guess this is a bug of movieHeaderWithFileType:error:.</p>
  
  <p>Is there any way to make movie header with modified format description?</p>
  
  <p><strong>In detail:</strong></p>
  
  <p>From macOS 10.13 AVMutableMovieTrack supports format description replacement in AVMutableMovieTrack.</p>
  
  <pre><code>- (void)replaceFormatDescription:(CMFormatDescriptionRef)formatDescription 
             withFormatDescription:(CMFormatDescriptionRef)newFormatDescription;
  </code></pre>
  
  <p>When I do AVMovie's movieHeaderWithFileType:error: or writeMovieHeaderToURL:fileType:options:error:, resulted movie header contains original unchaged video media format description. So it is unable to save.</p>
  
  <pre><code>- (NSData *)movieHeaderWithFileType:(AVFileType)fileType 
                                error:(NSError * _Nullable *)outError;
  
  - (BOOL)writeMovieHeaderToURL:(NSURL *)URL 
                       fileType:(AVFileType)fileType 
                        options:(AVMovieWritingOptions)options 
                          error:(NSError * _Nullable *)outError;
  </code></pre>
  
  <p><strong>Sample source:</strong></p>
  
  <blockquote>
    <p><a href="https://github.com/MyCometG3/cutter2/blob/master/cutter2/MovieMutator.swift" rel="nofollow noreferrer">https://github.com/MyCometG3/cutter2/blob/master/cutter2/MovieMutator.swift</a></p>
  </blockquote>
  
  <pre><code>var newFormat : CMVideoFormatDescription? = nil
  let codecType = CMFormatDescriptionGetMediaSubType(format) as CMVideoCodecType
  let dimensions = CMVideoFormatDescriptionGetDimensions(format)
  let result = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
                                                 codecType,
                                                 dimensions.width,
                                                 dimensions.height,
                                                 dict,
                                                 &amp;newFormat)
  if result == noErr, let newFormat = newFormat {
      track.replaceFormatDescription(format, with: newFormat)
      count += 1
  } else {
  </code></pre>
  
  <blockquote>
    <p><a href="https://github.com/MyCometG3/cutter2/blob/master/cutter2/MovieMutatorBase.swift" rel="nofollow noreferrer">https://github.com/MyCometG3/cutter2/blob/master/cutter2/MovieMutatorBase.swift</a></p>
  </blockquote>
  
  <pre><code>let movie : AVMovie = internalMovie.mutableCopy() as! AVMutableMovie
  let data = try? movie.makeMovieHeader(fileType: AVFileType.mov)
  return data
  </code></pre>