title: How to use node-fluent-ffmpeg to split a large video by fixed size to small videos?
body: <p>How to use node-fluent-ffmpeg to split a large video by fixed size to small videos?</p>
  <p>Here are some tips questions</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/38259544/using-ffmpeg-to-split-video-files-by-size">Using ffmpeg to split video files by size</a></li>
  <li><a href="https://stackoverflow.com/questions/35133119/ffmpeg-splitting-large-files">FFmpeg splitting large files</a></li>
  </ul>
  <p>I split the video file by stream ,but the file could be read.</p>
  <pre class="lang-js prettyprint-override"><code>
  const path = require('path');
  const fs = require('fs');
  const { Duplex, Writable ,Transform} = require('stream');
  const ffmpeg = require('fluent-ffmpeg');
  const inputFile = './data/test.mp4';
  const glob = require('glob')
  const maxPieceSize = 1024 * 1024 * 100;
  const ext = '.mp4';
  const tempext = '.temp.mp4';
  
  const readStream = fs.createReadStream(inputFile);
  
  async function split(readStream){
    let currPieceNumber = 0;
    let currPieceSize = 0;
    
    let writeStream = fs.createWriteStream(`./split/${currPieceNumber}${tempext}`);
    const writable = new Writable({
      write: (chunk, encoding, callback) =&gt; {
        if(chunk.length+ currPieceSize &gt;= maxPieceSize){
          writeStream.end()
          currPieceNumber++
          currPieceSize = 0
          writeStream = fs.createWriteStream(`./split/${currPieceNumber}${tempext}`);
        }
        
        currPieceSize+= chunk.length
        writeStream.write(chunk)
        callback()
      },
    });
    
    readStream.pipe(writable).on('unpipe',()=&gt;{
  
      const files=  glob.sync('./split/**.mp4')
      // files.forEach(file=&gt;{
  
      //   const readStream = fs.createReadStream(file);
      //   const writeStream = fs.createWriteStream(`./split/${currPieceNumber}${ext}`);
  
      //   ffmpeg().input(readStream).outputOptions(['-c copy']).output(writeStream)
      //   .on('error')
      //   .on('end',()=&gt;{
  
      //   }).run()
      // })
  
    });
  }
  split(readStream);
    
  </code></pre>