Search code examples
phpffmpegid3

is it possible to read mp3 id3 tags in php using ffmpeg? if so then how?


Hi i have a few mp3 files in my server which constantly change and i need the mp3 id3 tags for people to know what song is being played at the moment preferably via php . I am a complete noob at this so any help is helpful.


Solution

  • While there are several possibilities, I was always a fan of using pipes. ffprobe should be included in ffmpeg.

    <?php
    $filename = escapeshellarg($filename);
    $output = shell_exec("ffprobe -print_format json -show_entries stream=codec_name:format -select_streams a:0 -v quiet $filename");
    echo "<pre>$output</pre>";
    

    Output:

    
        {
            "programs": [
        
            ],
            "streams": [
                {
                    "codec_name": "mp3"
                }
            ],
            "format": {
                "filename": "test.mp3",
                "nb_streams": 1,
                "nb_programs": 0,
                "format_name": "mp3",
                "format_long_name": "MP2/3 (MPEG audio layer 2/3)",
                "start_time": "0.000000",
                "duration": "303.755813",
                "size": "9721021",
                "bit_rate": "256021",
                "probe_score": 51,
                "tags": {
                    "title": "Pictures Of Home",
                    "artist": "Deep Purple",
                    "album": "Machine Head",
                    "date": "1972",
                    "track": "3",
                    "genre": "Rock"
                }
            }
        }
    
    

    Use json_decode to convert it into an assoicative array.