+void Mux::write_header()
+{
+ AVDictionary *options = NULL;
+ vector<pair<string, string>> opts = MUX_OPTS;
+ for (pair<string, string> opt : opts) {
+ av_dict_set(&options, opt.first.c_str(), opt.second.c_str(), 0);
+ }
+
+ int err = avformat_write_header(avctx, &options);
+ if (err < 0) {
+ char errbuf[AV_ERROR_MAX_STRING_SIZE];
+ av_strerror(err, errbuf, sizeof(errbuf));
+ fprintf(stderr, "avformat_write_header() failed: %s\n", errbuf);
+ exit(EXIT_FAILURE);
+ }
+ for (MuxMetrics *metric : metrics) {
+ metric->metric_written_bytes += avctx->pb->pos;
+ }
+
+ // Make sure the header is written before the constructor exits
+ // (assuming we are in WRITE_FOREGROUND mode).
+ avio_flush(avctx->pb);
+
+}
+