--- mpegtsenc.c.org	2020-05-31 07:41:40.000000000 +0100
+++ mpegtsenc.c	2020-06-02 07:45:47.000000000 +0100
@@ -94,6 +94,7 @@
     int pmt_start_pid;
     int start_pid;
     int m2ts_mode;
+    int m2ts_streams;
     int m2ts_video_pid;
     int m2ts_audio_pid;
     int m2ts_pgssub_pid;
@@ -468,7 +469,7 @@
             break;
         }
 
-        stream_type = ts->m2ts_mode ? get_m2ts_stream_type(s, st) : get_dvb_stream_type(s, st);
+        stream_type = ts->m2ts_streams ? get_m2ts_stream_type(s, st) : get_dvb_stream_type(s, st);
 
         *q++ = stream_type;
         put16(&q, 0xe000 | ts_st->pid);
@@ -942,6 +943,14 @@
         }
     }
 
+    if (ts->m2ts_streams == -1) {
+        if (av_match_ext(s->url, "m2ts")) {
+            ts->m2ts_streams = 1;
+        } else {
+            ts->m2ts_streams = 0;
+        }
+    }
+
     ts->m2ts_video_pid   = M2TS_VIDEO_PID;
     ts->m2ts_audio_pid   = M2TS_AUDIO_START_PID;
     ts->m2ts_pgssub_pid  = M2TS_PGSSUB_START_PID;
@@ -2032,9 +2041,12 @@
     { "mpegts_start_pid", "Set the first pid.",
       offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT,
       { .i64 = 0x0100 }, FIRST_OTHER_PID, LAST_OTHER_PID, AV_OPT_FLAG_ENCODING_PARAM },
-    { "mpegts_m2ts_mode", "Enable m2ts mode.",
+    { "mpegts_m2ts_mode", "Enable m2ts packet mode.",
       offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_BOOL,
       { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
+    { "mpegts_m2ts_streams", "Enable m2ts stream allocation.",
+      offsetof(MpegTSWrite, m2ts_streams), AV_OPT_TYPE_BOOL,
+      { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
     { "muxrate", NULL,
       offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT,
       { .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
