title: Slicing mp3 files with python
body: <p>Firstly this questions is inferred from this answer <a href="https://stackoverflow.com/a/51965410/9089647">answer</a> .In that answer provides us a splitting a <strong>mp3</strong> file with <em>Python</em> . That code is usefull , but for splitting two pieces  . For instance if I want to split 30.00 th second to end of the audio , it is cool , but if I want to split from 30.00 to 35.00 it is useless for it . Below of that answer there is a comment about how to trim audio, like I said ,specific piece. When I imply that instructions to code it looked like that :</p>
  <pre><code>import struct
  import sys
  
  #MP3 frames are not independent because of the byte reservoir. This script does not account for
  #that in determining where to do the split.
  
  def SplitMp3(fi, firstSplit_sec,secondSplit_sec, out):
  
      #Constants for MP3
      bitrates = {0x0: &quot;free&quot;, 0x1: 32, 0x2: 40, 0x3: 48, 0x4: 56, 0x5: 64, 0x6: 80, 0x7: 96, 0x8: 112,
          0x9: 128, 0xa: 160, 0xb: 192, 0xc: 224, 0xd: 256, 0xe: 320, 0xf: &quot;bad&quot;}
      freqrates = {0x0: 44100, 0x1: 48000, 0x2: 32000, 0x3: &quot;reserved&quot;}
      countMpegFrames = 0
      frameDuration = 0.026
      unrecognizedBytes = 0
      firstSplitFrame = int(round(firstSplit_sec / frameDuration))
      secondSplitFrame = int(round(secondSplit_sec / frameDuration))
  
  
      while True:
  
          startPos = fi.tell()
  
          #Check for 3 byte headers
          id3Start = fi.read(3)
          if len(id3Start) == 3:
  
              if id3Start == b'TAG':
                  #print (&quot;Found ID3 v1/1.1 header&quot;)
                  fi.seek(startPos + 256)
                  continue
  
              if id3Start == b'ID3':
                  #Possibly a ID3v2 header
                  majorVer, minorVer, flags, encSize = struct.unpack(&quot;&gt;BBBI&quot;, fi.read(7))
                  if majorVer != 0xFF and minorVer != 0xFF:
                      encSize1 = (encSize &amp; 0x7f000000) &gt;&gt; 24
                      encSize2 = (encSize &amp; 0x7f0000) &gt;&gt; 16
                      encSize3 = (encSize &amp; 0x7f00) &gt;&gt; 8
                      encSize4 = (encSize &amp; 0x7f)
                      if encSize1 &lt; 0x80 and encSize2 &lt; 0x80 and encSize3 &lt; 0x80 and encSize4 &lt; 0x80:
                          size = ((encSize &amp; 0x7f000000) &gt;&gt; 3) + ((encSize &amp; 0x7f0000) &gt;&gt; 2) + ((encSize &amp; 0x7f00) &gt;&gt; 1) + (encSize &amp; 0x7f)
                          unsync = (flags &gt;&gt; 7) &amp; 0x1
                          extendedHeader = (flags &gt;&gt; 6) &amp; 0x1
                          experimental = (flags &gt;&gt; 5) &amp; 0x1
                          #print (&quot;Found ID3v2 header&quot;)
                          #print (&quot;version&quot;, majorVer, minorVer, unsync, extendedHeader, experimental)
                          #print (&quot;size&quot;, size)
                          #TODO extendedHeader not supported yet
  
                          fi.seek(startPos + 10 + size)
                          continue
  
          #Check for 4 byte headers
          fi.seek(startPos)
          headerRaw = fi.read(4)
          if len(headerRaw) == 4:
              headerWord = struct.unpack(&quot;&gt;I&quot;, headerRaw)[0]
  
              #Check for MPEG-1 audio frame
              if headerWord &amp; 0xfff00000 == 0xfff00000:
                  #print (&quot;Possible MPEG-1 audio header&quot;, hex(headerWord))
                  countMpegFrames += 1
                  ver = (headerWord &amp; 0xf0000) &gt;&gt; 16
                  bitrateEnc = (headerWord &amp; 0xf000) &gt;&gt; 12
                  freqEnc = (headerWord &amp; 0xf00) &gt;&gt; 8
                  mode = (headerWord &amp; 0xf0) &gt;&gt; 4
                  cpy = (headerWord &amp; 0xf)
                  if ver &amp; 0xe == 0xa and freqEnc != 0xf:
                      #print (&quot;Probably an MP3 frame&quot;)
                      bitrate = bitrates[bitrateEnc]
                      freq = freqrates[freqEnc &gt;&gt; 2]
                      padding = ((freqEnc &gt;&gt; 1) &amp; 0x1) == 1
                      #print (&quot;bitrate&quot;, bitrate, &quot;kbps&quot;)
                      #print (&quot;freq&quot;, freq, &quot;Hz&quot;)
                      #print (&quot;padding&quot;, padding)
                      frameLen = int((144 * bitrate * 1000 / freq ) + padding)
  
                      #Copy frame to output
                      fi.seek(startPos)
                      frameData = fi.read(frameLen)
                      if (secondSplitFrame &gt;= countMpegFrames) and (countMpegFrames &gt;= firstSplitFrame):
                          out.write(frameData)
  
                      fi.seek(startPos + frameLen)
                      continue
                  else:
                      raise RuntimeError(&quot;Unsupported format:&quot;, hex(ver), &quot;header:&quot;, hex(headerWord))
  
          #If no header can be detected, move on to the next byte
          fi.seek(startPos)
          nextByteRaw = fi.read(1)
          if len(nextByteRaw) == 0:
              break #End of file
          unrecognizedBytes += 1
  
      #print (&quot;unrecognizedBytes&quot;, unrecognizedBytes)
      #print (&quot;countMpegFrames&quot;, countMpegFrames)
      #print (&quot;duration&quot;, countMpegFrames * frameDuration, &quot;sec&quot;)
  </code></pre>
  <p>When I use this function it produces loosy output . For instance if I want to split 0.0 to 41.00 it gives me 0.00 to 37.00 , and this loosieness is increasing with amount of slice. I've struggled to understand some parts of code . So I am asking how can I solve that loosiness ? Am I  missing something ?</p>
  <p>Note : I already tried to <strong>pydub</strong> and similar modules . But they are useless . Always giving memory error and slow . This is really fast .</p>