title: how to create an http live stream ( broadcast ) from a segmented mp4 without sound defects
body: <p>tl;dr - concatenating independent short videos to an hls playlist creates sound defects. A 'Tick' sound at the concatenation point. How do I avoid it ?</p>
  
  <p>I have a process that emits a 4 second MOV video every 3 seconds.
  Those 4s videos are segments of a longer video. But they are completely independent. 
  I want to be able to play the Longer video before it exists.
  In order to achieve that I'm creating an HLS even playlist
  <a href="https://developer.apple.com/documentation/http_live_streaming/example_playlists_for_http_live_streaming/event_playlist_construction" rel="nofollow noreferrer">hls event playlist</a>
  I first convert the first 4s video to hls using ffmpeg
  by running :</p>
  
  <pre><code>fmpeg -y -i  output0.mov -c:a aac -b:a 128000 -r:a 44100 -c:v libx264  -x264-params keyint=150:scenecut=0  -hls_flags omit_endlist -hls_playlist_type -b:t 800k -maxrate 2000k -bufsize 1200k -f hls -pix_fmt yuv420p -g 5 -hls_time 5 one.m3u8
  </code></pre>
  
  <p>this will create an .ts file and and an .m3u8 playlist</p>
  
  <pre><code>#EXT-X-PLAYLIST-TYPE:EVENT
  #EXT-X-TARGETDURATION:4
  #EXT-X-VERSION:4
  #EXT-X-MEDIA-SEQUENCE:0
  #EXTINF:4.00,
  part1.ts
  </code></pre>
  
  <p>notice the : <code>-hls_flags omit_endlist -hls_playlist_type event</code> flags. They make sure the playlist is an EVENT playlist and that it has no <code>EXT-X-ENDLIST</code> at the end of the list ( this  makes the player to start  polling  the .m3u8 file in order to "wait" for new .ts chunks to be appended to the playlist )</p>
  
  <p>When the next 4s video is available I'm running the ffmpeg command again.
  and then "merge" the new .m3u8 file to the old .m3u8 file. 
  by appending the new .ts file to the playlist.</p>
  
  <pre><code>#EXT-X-PLAYLIST-TYPE:EVENT
  #EXT-X-TARGETDURATION:4
  #EXT-X-VERSION:4
  #EXT-X-MEDIA-SEQUENCE:0
  #EXTINF:4.00,
  part1.ts &lt;- first 4 seconds chunk
  #EXTINF:4.00,
  part2.ts &lt;- second ( new ) 4 second chunk
  </code></pre>
  
  <p>since the player is polling the .m3u8 file , when its updated it fetches the second .ts file (part2.ts)
  and plays it.</p>
  
  <p>The only issue is that  sound defect.  exactly at the concatenation point of the two videos. ( at second 4 ) in this example.
  The video has no visible defects.
  I suspect this happens because of an encoding problem. ( every mov video is encoded to hls separately ) 
  But I must play the video after 4 seconds. ( can't wait for the whole duration to be ready )</p>
  
  <p>What am I doing wrong ? How to encode the hls chunks to avoid the sound defect ?</p>
  
  <p>In ffmpeg documentation I saw:
  <em>Segment the input file, and create an M3U8 live playlist (can be used as live HLS source):</em></p>
  
  <pre><code>ffmpeg -re -i in.mkv -codec copy -map 0 -f segment -segment_list playlist.m3u8 \
  -segment_list_flags +live -segment_time 10 out%03d.mkv
  </code></pre>
  
  <p>My problem is that i don't have the entire video. Its generated chunk by chunk.</p>