code: def iter_sync(fileobj, max_read):
      
  
      read = 0
      size = 2
      last_byte = b""
      is_second = lambda b: ord(b) & 0xe0 == 0xe0
  
      while read < max_read:
          data_offset = fileobj.tell()
          new_data = fileobj.read(min(max_read - read, size))
          if not new_data:
              return
          read += len(new_data)
  
          if last_byte == b"\xff" and is_second(new_data[0:1]):
              fileobj.seek(data_offset - 1, 0)
              yield
  
          size *= 2
          last_byte = new_data[-1:]
  
          find_offset = 0
          while True:
              index = new_data.find(b"\xff", find_offset)
              # if not found or the last byte -> read more
              if index == -1 or index == len(new_data) - 1:
                  break
  
              if is_second(new_data[index + 1:index + 2]):
                  fileobj.seek(data_offset + index, 0)
                  yield
              find_offset = index + 1
  
          fileobj.seek(data_offset + len(new_data), 0)
docs: Iterate over a fileobj and yields on each mpeg sync.
  
      When yielding the fileobj offset is right before the sync and can be
      changed between iterations without affecting the iteration process.
  
      Might raise IOError.