# this patch is meant to address trac ticket <https://trac.ffmpeg.org/ticket/3622>
# where more than one stream/track was being enabled for the same media type.
# this caused playback failures in Quicktime and especially Apple TVs when both
# stereo and surround streams were being written to the same container.
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 01bc3c9..a11378f 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -3673,7 +3673,7 @@ static int mov_create_timecode_track(AVFormatContext *s, int index, int src_inde
 /*
  * st->disposition controls the "enabled" flag in the tkhd tag.
  * QuickTime will not play a track if it is not enabled.  So make sure
- * that one track of each type (audio, video, subtitle) is enabled.
+ * that at most one track of each type (audio, video, subtitle) is enabled.
  *
  * Subtitles are special.  For audio and video, setting "enabled" also
  * makes the track "default" (i.e. it is rendered when played). For
@@ -3700,21 +3700,43 @@ static void enable_tracks(AVFormatContext *s)
             st->codec->codec_type >= AVMEDIA_TYPE_NB)
             continue;
 
+        // note the first stream number for each type
         if (first[st->codec->codec_type] < 0)
             first[st->codec->codec_type] = i;
-        if (st->disposition & AV_DISPOSITION_DEFAULT) {
-            mov->tracks[i].flags |= MOV_TRACK_ENABLED;
+
+        // note whether each media type has any enabled streams
+        if (st->disposition & AV_DISPOSITION_DEFAULT)
             enabled[st->codec->codec_type] = 1;
-        }
+
+        // clear the enables for each stream and track
+        st->disposition &= ~AV_DISPOSITION_DEFAULT;
+        mov->tracks[i].flags &= ~MOV_TRACK_ENABLED;
     }
 
+    // for each media type, if no stream was explicitly enabled
+    // then enable the first one found for that type
     for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
         switch (i) {
         case AVMEDIA_TYPE_VIDEO:
         case AVMEDIA_TYPE_AUDIO:
         case AVMEDIA_TYPE_SUBTITLE:
             if (!enabled[i] && first[i] >= 0)
+                enabled[i] = 1;
+            break;
+        }
+    }
+
+    // translate the first enabled stream of each media type
+    // into its stream being default and its output track being enabled
+    for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
+        switch (i) {
+        case AVMEDIA_TYPE_VIDEO:
+        case AVMEDIA_TYPE_AUDIO:
+        case AVMEDIA_TYPE_SUBTITLE:
+            if (enabled[i]) {
+                s->streams[first[i]]->disposition |= AV_DISPOSITION_DEFAULT;
                 mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
+            }
             break;
         }
     }
