HLS stream with different .ts segments

Hi

I’m writing an app which manipulates an M3U8 HLS manifest and replace certain .ts segments in the middle of the stream.
Both TS file playback separately in Kody without a problem, but it seems that Kody can’t continuously play the stream when switching between the two TS types.

Both TS files has same bitrates, profile, level, length, GOP structure and so on. Though they have different MPEG TS metadata.
Below are two TSINFO dumps from the two separate files:

File 1:

Code:
Reading from /some/path/file1_1.ts
Scanning 1000 TS packets

Packet 2 is PAT
Program list:
    Program 1 -> PID 1000 (4096)

Packet 3 is PMT with PID 1000 (4096)
  Program 1, version 0, PCR PID 01e1 (481)
  Program streams:
    PID 01e1 ( 481) -> Stream type 1b ( 27) H.264/14496-10 video (MPEG-4/AVC)
    PID 01e2 ( 482) -> Stream type 0f ( 15) 13818-7 Audio with ADTS transport syntax

Found 48 PAT packets and 24 PMT packets in 1000 TS packets

File 2:

Code:
Reading from /some/path/file2.ts
Scanning 1000 TS packets

Packet 1 is PAT
Program list:
    Program 1 -> PID 01e0 (480)

Packet 2 is PMT with PID 01e0 (480)
  Program 1, version 0, PCR PID 01e1 (481)
     Program info (5 bytes): 0e 03 c0 14 28
     maximum bitrate (3 bytes): c0 14 28
  Program streams:
    PID 01e1 ( 481) -> Stream type 1b ( 27) H.264/14496-10 video (MPEG-4/AVC)
        ES info (11 bytes): 28 04 4d 40 1f 3f 0e 03 c0 13 88
        Reserved (4 bytes): 4d 40 1f 3f
        maximum bitrate (3 bytes): c0 13 88
    PID 01e2 ( 482) -> Stream type 0f ( 15) 13818-7 Audio with ADTS transport syntax
        ES info (11 bytes): 0a 04 75 6e 64 00 0e 03 c0 00 a0
        Languages: und
        maximum bitrate (3 bytes): c0 00 a0

Found 2 PAT packets and 1 PMT packet in 1000 TS packets

This is an extract from Kodi’s log that refers to the problem:

Code:
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [http] header='Content-Type: video/MP2T'
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [http] header=''
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] Continuity check failed for pid 0 expected 1 got 7
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] PAT:
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] sid=0x1 pid=0x1e0
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] new_program: id=0x0001
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] Filter: pid=0x1e0 type=1
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] PMT: len 53
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] sid=0x1 sec_num=0/0 version=0 tid=2
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] pcr_pid=0x1e1
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] program tag: 0x0e len=3
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] Filter: pid=0x1e1 type=0
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] stream=2 stream_type=1b pid=1e1 prog_reg_desc=
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] tag: 0x28 len=4
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] tag: 0x0e len=3
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] Filter: pid=0x1e2 type=0
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] stream=3 stream_type=f pid=1e2 prog_reg_desc=
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] tag: 0x0a len=4
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] tag: 0x0e len=3
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] pid=1e2 pes_code=0x1c0
16:36:23 T:123145358114816   DEBUG: ffmpeg[700003538000]: [mpegts] pid=1e1 pes_code=0x1e0
16:36:23 T:123145358114816   DEBUG: Previous line repeats 1 times.
16:36:23 T:123145358114816   ERROR: ffmpeg[700003538000]: [hls,applehttp] stream index inconsistency: index 3, 2 main streams, 4 subdemuxer streams

Can anyone suggest a fix for this?
Thanks