From 6e65b5a285cd3f69c9cb3f5f8c3dd364aa118ab4 Mon Sep 17 00:00:00 2001 From: Xavier Henner Date: Wed, 20 May 2015 14:02:41 +0200 Subject: [PATCH] improve handling of corrupt frames --- mp3.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/mp3.go b/mp3.go index e2a9dff..bb08974 100644 --- a/mp3.go +++ b/mp3.go @@ -95,23 +95,20 @@ func getMp3Infos(r io.ReadSeeker, slow bool) (*mp3Infos, error) { if i < 4 { break } - if i==5 { - // invalid frame - continue - } - pos += int64(i) // looking for the synchronization bits switch { case (buf[0] == 255) && (buf[1] >= 224): // found a valid mp3 frame. we read the header to know where the // next one is - pos, _ = r.Seek(h.readHeader(buf)-4, 1) - - bitrateSum += h.Bitrate - frameCount++ - if h.vbr > 2 { - nbscan = 100 + offset := h.readHeader(buf) - 4 + if offset != 1 { // only count valid frames + bitrateSum += h.Bitrate + frameCount++ + if h.vbr > 2 { + nbscan = 100 // the file is VBR, use more sample to estimate its size + } } + pos, _ = r.Seek(offset, 1) // jump to the next frame case string(buf[:3]) == "TAG": pos, _ = r.Seek(128-4, 1) // id3v1 tag, bypass it default: