wapmorgan / Mp3Info

The fastest PHP library to extract mp3 meta information (duration, bitrate, samplerate and so on) and tags (id3v1, id3v2).
https://wapmorgan.github.io/Mp3Info/
GNU Lesser General Public License v3.0
139 stars 41 forks source link

Missing the last character of the Id3v23 frames which encoded in unicode #8

Closed wizwizcao closed 5 years ago

wizwizcao commented 6 years ago

For example, in tags2, the TPE1 shows "Bruno Mar" which should be assumed "Bruno Mars". This issue seems applies to all the tags2 fields. Guess the root cause is converting the non-ISO-8859-1 data from 'utf-16' to 'utf-8'.

wapmorgan commented 6 years ago

Thanks for info. Will see it soon.

parzibyte commented 5 years ago

Hey, i have a small fix for it. I spent a lot of time debugging with that, but at the end it works adding the nul char at the end of the string. I didn't do a pull request because idk if the code will break with another songs, but at least works for me. I have forked your repo:

https://github.com/parzibyte/Mp3Info/blob/master/src/Mp3Info.php

The important code is, in the else of the function handleTextFrame put this:

$data["information"] .= chr(0);

So the function is like this:

private function handleTextFrame($frameSize, $raw)
    {
        $data = unpack('C1encoding/A' . ($frameSize - 1) . 'information', $raw);

        if ($data['encoding'] == 0x00) # ISO-8859-1
            return mb_convert_encoding($data['information'], 'utf-8', 'iso-8859-1');
        else{ # utf-16
            # Fix the missing last char of the info bug
            # Add NUL character at the end of the string. 
            # Don't ask, just enjoy. Idk why it works, but it works!
            $data["information"] .=  chr(0);
            return mb_convert_encoding($data['information'], 'utf-8', 'utf-16');
        }
    }

Hope it helps! and thank you for making this library :-)

wapmorgan commented 5 years ago

I think that's good, added in 9d5d136