title: ffmpeg generate m3u8 from mp4 (Resume option)
body: <p>I have a mp4 file or other file (non mp4 format) and i need generate ts files and m3u8 playlist.</p>
  
  <p>I am using this command and works fine: </p>
  
  <blockquote>
    <p>ffmpeg -i foo.mp4 -codec copy -vbsf h264_mp4toannexb -map 0 -f segment
    -segment_list out.m3u8 -segment_time 10 out%03d.ts</p>
  </blockquote>
  
  <p>Now I need to generate many ts simultaneous so i need a "resume option".</p>
  
  <p>Please see the example below:</p>
  
  <p>One thread (first 20 seconds (0-20))</p>
  
  <blockquote>
    <p>ffmpeg -i foo.mp4 -codec copy -vbsf h264_mp4toannexb -map 0 -f segment
    -segment_list out.m3u8 -segment_time 10 out%03d.ts</p>
  </blockquote>
  
  <p>Seconds thread (20 seconds to 40 seconds)</p>
  
  <blockquote>
    <p>ffmpeg -i foo.mp4 ......</p>
  </blockquote>
  
  <p>Third thread (40 seconds to 60 seconds)</p>
  
  <blockquote>
    <p>ffmpeg -i foo.mp4 ......</p>
  </blockquote>
  
  <p>I have lots of core processor to do this jobs.</p>
  
  <p>In resume i need generate .ts files and m3u8 fastest way possible</p>
  
  <p>I need help or advises to resolve my problem.</p>
  
  <p>Prove of concept i build a litle script that use -ss and -t option:</p>
  
  <pre><code>&lt;?php
  
   //generate all the commands using -ss and -t &lt;seconds&gt; 
   $startTime = new DateTime("00:00:00");
   for ($i=1; $i &lt; 20; $i++) {
      $data = $startTime-&gt;format('H:i:s');
      $exec = 'ffmpeg  -i "&lt;FILE&gt;" -ss '.$data.' -t 10 -c:v copy -bsf h264_mp4toannexb -flags -global_header -map 0 -f segment -segment_time 10 -segment_start_number '.$i.' -segment_list '.sprintf("%04d", $i).'_test.m3u8 -segment_format mpegts '.$i.'stream%05d.ts';
      shell_exec($exec);
      $startTime-&gt;modify('+10 seconds');
      echo "\n";
  }
  
  //cycle all m3u8 and creates a master hls playlist
  $files = glob('*.{m3u8}', GLOB_BRACE);
  sort($files);
  $ret = "#EXTM3U
  #EXT-X-VERSION:3
  #EXT-X-MEDIA-SEQUENCE:0
  #EXT-X-ALLOW-CACHE:YES
  #EXT-X-TARGETDURATION:11
  ";
  foreach($files as $file) {
      $ret .= shell_exec('sed -n -e 6,7p '. $file);
  }
  $ret .= "#EXT-X-ENDLIST";
  
  file_put_contents('final.m3u8', $ret);
  
  ?&gt;
  </code></pre>
  
  <p>Thanks</p>