Value from array in file method

I’m working on a new Kirby 3 plugin and I need to get the key value from a key in a multi dimensional array. Is there something in the toolkit that can help me here?

My plugin uses an external library to get ID3 information from audio files. I’ve fed it a file object from Kirby and it gives me back a multidimensional array of all the files ID3 data.

Basically I want to be able to do this in a template:

$page->file('sorrow.mp3')->idthree('artist');

Where ‘artist’ is a key somewhere in the array and I want to get the value of that key. Sometimes the value is an int, sometimes its a string. In this case I should get back ‘Pink Floyd’.

I thought A::pluck would help but its doesn’t seem to, unless i’m doing it wrong.

Plugins looks like this so far…

require('lib/getid3/getid3.php');

Kirby::plugin('hashandsalt/idthree', [
	'fileMethods' => [
		'idthree' => function ($mediaitem = 'playtime_string') {

			$getID3 = new getID3;
			$mediaid = $getID3->analyze($this->root());

			$mediainfo = A::pluck($mediaid, $mediaitem);

			return $mediainfo;


		}
	]
]);

What is the structure of that array?

I was hoping you wouldn’t ask that because It’s pretty huge but here goes…

Array
(
    [GETID3_VERSION] => 1.9.16-201812050141
    [filesize] => 8457715
    [filepath] => /public/content/1_about-us
    [filename] => sorrow.mp3
    [filenamepath] => /public/content/1_about-us/sorrow.mp3
    [avdataoffset] => 16384
    [avdataend] => 8457715
    [fileformat] => mp3
    [audio] => Array
        (
            [dataformat] => mp3
            [channels] => 2
            [sample_rate] => 44100
            [bitrate] => 128000
            [channelmode] => joint stereo
            [bitrate_mode] => cbr
            [lossless] =>
            [encoder_options] => CBR128
            [compression_ratio] => 0.090702947845805
            [streams] => Array
                (
                    [0] => Array
                        (
                            [dataformat] => mp3
                            [channels] => 2
                            [sample_rate] => 44100
                            [bitrate] => 128000
                            [channelmode] => joint stereo
                            [bitrate_mode] => cbr
                            [lossless] =>
                            [encoder_options] => CBR128
                            [compression_ratio] => 0.090702947845805
                        )

                )

        )

    [tags] => Array
        (
            [id3v2] => Array
                (
                    [title] => Array
                        (
                            [0] => Sorrow
                        )

                    [artist] => Array
                        (
                            [0] => Pink Floyd
                        )

                    [album] => Array
                        (
                            [0] => A Momentary Lapse of Reason
                        )

                    [genre] => Array
                        (
                            [0] => Rock
                        )

                    [year] => Array
                        (
                            [0] => 1987 09 08
                        )

                    [composer] => Array
                        (
                            [0] => David Gilmour
                        )

                    [part_of_a_set] => Array
                        (
                            [0] => 1/1
                        )

                    [text] => Array
                        (
                            [Style] => Album Rock
Prog-Rock/Art Rock
                            [AccurateRipResult] => AccurateRip: Inaccurate (confidence 13)   [6019750C]
                            [AccurateRipDiscID] => 010-001346da-009a0b66-7a0c020a-10
                        )

                    [track_number] => Array
                        (
                            [0] => 10/10
                        )

                )

        )

    [comments] => Array
        (
            [picture] => Array
                (
                    [0] => Array
                        (
                            [data] => 'data'
                            [image_mime] => image/jpeg
                            [image_width] => 250
                            [image_height] => 245
                            [picturetype] => Cover (front)
                            [description] =>
                            [datalength] => 15093
                        )

                )

        )

    [encoding] => UTF-8
    [id3v2] => Array
        (
            [header] => 1
            [flags] => Array
                (
                    [unsynch] =>
                    [exthead] =>
                    [experim] =>
                )

            [majorversion] => 3
            [minorversion] => 0
            [headerlength] => 16384
            [tag_offset_start] => 0
            [tag_offset_end] => 16384
            [encoding] => UTF-8
            [comments] => Array
                (
                    [title] => Array
                        (
                            [0] => Sorrow
                        )

                    [artist] => Array
                        (
                            [0] => Pink Floyd
                        )

                    [album] => Array
                        (
                            [0] => A Momentary Lapse of Reason
                        )

                    [genre] => Array
                        (
                            [0] => Rock
                        )

                    [year] => Array
                        (
                            [0] => 1987 09 08
                        )

                    [composer] => Array
                        (
                            [0] => David Gilmour
                        )

                    [part_of_a_set] => Array
                        (
                            [0] => 1/1
                        )

                    [text] => Array
                        (
                            [Style] => Album Rock
Prog-Rock/Art Rock
                            [AccurateRipResult] => AccurateRip: Inaccurate (confidence 13)   [6019750C]
                            [AccurateRipDiscID] => 010-001346da-009a0b66-7a0c020a-10
                        )

                    [track_number] => Array
                        (
                            [0] => 10/10
                        )

                )

            [TIT2] => Array
                (
                    [0] => Array
                        (
                            [frame_name] => TIT2
                            [frame_flags_raw] => 0
                            [data] => Sorrow
                            [datalength] => 7
                            [dataoffset] => 10
                            [framenamelong] => Title/songname/content description
                            [framenameshort] => title
                            [flags] => Array
                                (
                                    [TagAlterPreservation] =>
                                    [FileAlterPreservation] =>
                                    [ReadOnly] =>
                                    [compression] =>
                                    [Encryption] =>
                                    [GroupingIdentity] =>
                                )

                            [encodingid] => 0
                            [encoding] => ISO-8859-1
                        )

                )

            [TPE1] => Array
                (
                    [0] => Array
                        (
                            [frame_name] => TPE1
                            [frame_flags_raw] => 0
                            [data] => Pink Floyd
                            [datalength] => 11
                            [dataoffset] => 27
                            [framenamelong] => Lead performer(s)/Soloist(s)
                            [framenameshort] => artist
                            [flags] => Array
                                (
                                    [TagAlterPreservation] =>
                                    [FileAlterPreservation] =>
                                    [ReadOnly] =>
                                    [compression] =>
                                    [Encryption] =>
                                    [GroupingIdentity] =>
                                )

                            [encodingid] => 0
                            [encoding] => ISO-8859-1
                        )

                )

            [TALB] => Array
                (
                    [0] => Array
                        (
                            [frame_name] => TALB
                            [frame_flags_raw] => 0
                            [data] => A Momentary Lapse of Reason
                            [datalength] => 28
                            [dataoffset] => 48
                            [framenamelong] => Album/Movie/Show title
                            [framenameshort] => album
                            [flags] => Array
                                (
                                    [TagAlterPreservation] =>
                                    [FileAlterPreservation] =>
                                    [ReadOnly] =>
                                    [compression] =>
                                    [Encryption] =>
                                    [GroupingIdentity] =>
                                )

                            [encodingid] => 0
                            [encoding] => ISO-8859-1
                        )

                )

            [TCON] => Array
                (
                    [0] => Array
                        (
                            [frame_name] => TCON
                            [frame_flags_raw] => 0
                            [data] => Rock
                            [datalength] => 5
                            [dataoffset] => 86
                            [framenamelong] => Content type
                            [framenameshort] => genre
                            [flags] => Array
                                (
                                    [TagAlterPreservation] =>
                                    [FileAlterPreservation] =>
                                    [ReadOnly] =>
                                    [compression] =>
                                    [Encryption] =>
                                    [GroupingIdentity] =>
                                )

                            [encodingid] => 0
                            [encoding] => ISO-8859-1
                        )

                )

            [TYER] => Array
                (
                    [0] => Array
                        (
                            [frame_name] => TYER
                            [frame_flags_raw] => 0
                            [data] => 1987 09 08
                            [datalength] => 11
                            [dataoffset] => 101
                            [framenamelong] => Year
                            [framenameshort] => year
                            [flags] => Array
                                (
                                    [TagAlterPreservation] =>
                                    [FileAlterPreservation] =>
                                    [ReadOnly] =>
                                    [compression] =>
                                    [Encryption] =>
                                    [GroupingIdentity] =>
                                )

                            [encodingid] => 0
                            [encoding] => ISO-8859-1
                        )

                )

            [TCOM] => Array
                (
                    [0] => Array
                        (
                            [frame_name] => TCOM
                            [frame_flags_raw] => 0
                            [data] => David Gilmour
                            [datalength] => 14
                            [dataoffset] => 122
                            [framenamelong] => Composer
                            [framenameshort] => composer
                            [flags] => Array
                                (
                                    [TagAlterPreservation] =>
                                    [FileAlterPreservation] =>
                                    [ReadOnly] =>
                                    [compression] =>
                                    [Encryption] =>
                                    [GroupingIdentity] =>
                                )

                            [encodingid] => 0
                            [encoding] => ISO-8859-1
                        )

                )

            [TPOS] => Array
                (
                    [0] => Array
                        (
                            [frame_name] => TPOS
                            [frame_flags_raw] => 0
                            [data] => 1/1
                            [datalength] => 4
                            [dataoffset] => 146
                            [framenamelong] => Part of a set
                            [framenameshort] => part_of_a_set
                            [flags] => Array
                                (
                                    [TagAlterPreservation] =>
                                    [FileAlterPreservation] =>
                                    [ReadOnly] =>
                                    [compression] =>
                                    [Encryption] =>
                                    [GroupingIdentity] =>
                                )

                            [encodingid] => 0
                            [encoding] => ISO-8859-1
                        )

                )

            [TXXX] => Array
                (
                    [0] => Array
                        (
                            [frame_name] => TXXX
                            [frame_flags_raw] => 0
                            [data] => Album Rock
Prog-Rock/Art Rock
                            [datalength] => 36
                            [dataoffset] => 160
                            [framenamelong] => User defined text information frame
                            [framenameshort] => text
                            [flags] => Array
                                (
                                    [TagAlterPreservation] =>
                                    [FileAlterPreservation] =>
                                    [ReadOnly] =>
                                    [compression] =>
                                    [Encryption] =>
                                    [GroupingIdentity] =>
                                )

                            [encodingid] => 0
                            [encoding] => ISO-8859-1
                            [description] => Style
                        )

                    [1] => Array
                        (
                            [frame_name] => TXXX
                            [frame_flags_raw] => 0
                            [data] => AccurateRip: Inaccurate (confidence 13)   [6019750C]
                            [datalength] => 71
                            [dataoffset] => 15355
                            [framenamelong] => User defined text information frame
                            [framenameshort] => text
                            [flags] => Array
                                (
                                    [TagAlterPreservation] =>
                                    [FileAlterPreservation] =>
                                    [ReadOnly] =>
                                    [compression] =>
                                    [Encryption] =>
                                    [GroupingIdentity] =>
                                )

                            [encodingid] => 0
                            [encoding] => ISO-8859-1
                            [description] => AccurateRipResult
                        )

                    [2] => Array
                        (
                            [frame_name] => TXXX
                            [frame_flags_raw] => 0
                            [data] => 010-001346da-009a0b66-7a0c020a-10
                            [datalength] => 52
                            [dataoffset] => 15436
                            [framenamelong] => User defined text information frame
                            [framenameshort] => text
                            [flags] => Array
                                (
                                    [TagAlterPreservation] =>
                                    [FileAlterPreservation] =>
                                    [ReadOnly] =>
                                    [compression] =>
                                    [Encryption] =>
                                    [GroupingIdentity] =>
                                )

                            [encodingid] => 0
                            [encoding] => ISO-8859-1
                            [description] => AccurateRipDiscID
                        )

                )

            [APIC] => Array
                (
                    [0] => Array
                        (
                            [frame_name] => APIC
                            [frame_flags_raw] => 0
                            [data] => 'image'
                            [datalength] => 15093
                            [dataoffset] => 206
                            [framenamelong] => Attached picture
                            [framenameshort] => attached_picture
                            [flags] => Array
                                (
                                    [TagAlterPreservation] =>
                                    [FileAlterPreservation] =>
                                    [ReadOnly] =>
                                    [compression] =>
                                    [Encryption] =>
                                    [GroupingIdentity] =>
                                )

                            [encodingid] => 0
                            [encoding] => ISO-8859-1
                            [mime] => image/jpeg
                            [picturetypeid] => 3
                            [picturetype] => Cover (front)
                            [description] =>
                            [image_mime] => image/jpeg
                            [image_width] => 250
                            [image_height] => 245
                        )

                )

            [TRCK] => Array
                (
                    [0] => Array
                        (
                            [frame_name] => TRCK
                            [frame_flags_raw] => 0
                            [data] => 10/10
                            [datalength] => 6
                            [dataoffset] => 15323
                            [framenamelong] => Track number/Position in set
                            [framenameshort] => track_number
                            [flags] => Array
                                (
                                    [TagAlterPreservation] =>
                                    [FileAlterPreservation] =>
                                    [ReadOnly] =>
                                    [compression] =>
                                    [Encryption] =>
                                    [GroupingIdentity] =>
                                )

                            [encodingid] => 0
                            [encoding] => ISO-8859-1
                        )

                )

            [POPM] => Array
                (
                    [0] => Array
                        (
                            [frame_name] => POPM
                            [frame_flags_raw] => 0
                            [datalength] => 6
                            [dataoffset] => 15339
                            [framenamelong] => Popularimeter
                            [framenameshort] => popularimeter
                            [flags] => Array
                                (
                                    [TagAlterPreservation] =>
                                    [FileAlterPreservation] =>
                                    [ReadOnly] =>
                                    [compression] =>
                                    [Encryption] =>
                                    [GroupingIdentity] =>
                                )

                            [counter] => 0
                            [email] =>
                            [rating] => 77
                        )

                )

            [padding] => Array
                (
                    [start] => 15498
                    [length] => 886
                    [valid] => 1
                )

        )

    [mime_type] => audio/mpeg
    [mpeg] => Array
        (
            [audio] => Array
                (
                    [raw] => Array
                        (
                            [synch] => 4094
                            [version] => 3
                            [layer] => 1
                            [protection] => 1
                            [bitrate] => 9
                            [sample_rate] => 0
                            [padding] => 0
                            [private] => 0
                            [channelmode] => 1
                            [modeextension] => 0
                            [copyright] => 0
                            [original] => 0
                            [emphasis] => 0
                        )

                    [version] => 1
                    [layer] => 3
                    [channelmode] => joint stereo
                    [channels] => 2
                    [sample_rate] => 44100
                    [protection] =>
                    [private] =>
                    [modeextension] =>
                    [copyright] =>
                    [original] =>
                    [emphasis] => none
                    [padding] =>
                    [bitrate] => 128000
                    [framelength] => 417
                    [bitrate_mode] => cbr
                )

        )

    [playtime_seconds] => 527.5831875
    [tags_html] => Array
        (
            [id3v2] => Array
                (
                    [title] => Array
                        (
                            [0] => Sorrow
                        )

                    [artist] => Array
                        (
                            [0] => Pink Floyd
                        )

                    [album] => Array
                        (
                            [0] => A Momentary Lapse of Reason
                        )

                    [genre] => Array
                        (
                            [0] => Rock
                        )

                    [year] => Array
                        (
                            [0] => 1987 09 08
                        )

                    [composer] => Array
                        (
                            [0] => David Gilmour
                        )

                    [part_of_a_set] => Array
                        (
                            [0] => 1/1
                        )

                    [text] => Array
                        (
                            [Style] => Album Rock
Prog-Rock/Art Rock
                            [AccurateRipResult] => AccurateRip: Inaccurate (confidence 13)   [6019750C]
                            [AccurateRipDiscID] => 010-001346da-009a0b66-7a0c020a-10
                        )

                    [track_number] => Array
                        (
                            [0] => 10/10
                        )

                )

        )

    [bitrate] => 128000
    [playtime_string] => 8:48
)

Can this information be anywhere in the array or is the structure always the same?

I think its a standard structure, always. ID3 is a standard for audio information, so i’m pretty sure thats it, always.

But some of the of the information does appear twice. Part of me wants to flatten the array and strip the duplicates, but the problem there is that some of the keys end up being numbered rather then named.

You can either just get it like this:

$artist = $array['tags']['id3v2']['artist'];

or

array_walk_recursive ($array, function($value,$key) use ($array){
  if($key == 'artist') echo $value;
});

But you can return anything from that.

Maybe better use the RecursiveIterator, although that is probably overkill if you don’t want to get anything but the artist.


Doesn’t the class provide any methods to get details from that big array?

The ‘artist’ key seems to be present in these places:

  • $array['tags']['artist']
  • $array['id3v2']['comments']['artist']
  • $array['tags_html']['id3v2']['artist'] // this one is an array

You could perhaps just use isset() to check whether any of the values exist?

No. The examples I have seen, like this one, seem to hit up the values directly rather then searching.

I guess I could build up my own array of the most useful information from that big one and work from that.

Im building a podcast site, and for the RSS feed I need a few bits of information from the tag, like track duration. I figured it might be useful to others so I was trying to make it possible to get any information from the ID tag, as long as you know the key name.

The library has some example, see what he does here:

$ThisFileInfo = $getID3->analyze($FullFileName);
		getid3_lib::CopyTagsToComments($ThisFileInfo);
		// output desired information in whatever format you want
		echo '<tr>';
		echo '<td>'.htmlentities($ThisFileInfo['filenamepath']).'</td>';
		echo '<td>'              .htmlentities(!empty($ThisFileInfo['comments_html']['artist']) ? implode('<br>', $ThisFileInfo['comments_html']['artist'])         : chr(160)).'</td>';
		echo '<td>'              .htmlentities(!empty($ThisFileInfo['comments_html']['title'])  ? implode('<br>', $ThisFileInfo['comments_html']['title'])          : chr(160)).'</td>';
		echo '<td align="right">'.htmlentities(!empty($ThisFileInfo['audio']['bitrate'])        ?           round($ThisFileInfo['audio']['bitrate'] / 1000).' kbps' : chr(160)).'</td>';
		echo '<td align="right">'.htmlentities(!empty($ThisFileInfo['playtime_string'])         ?                 $ThisFileInfo['playtime_string']                  : chr(160)).'</td>';
		echo '</tr>';

So with thus CopyTagstToComments() methods, you get the important information in one place, it seems…, the just get the stuff by index

I went with this in the end… unless theres a slicker way but it works…

<?php
require('lib/getid3/getid3.php');

Kirby::plugin('hashandsalt/idthree', [
	'fileMethods' => [
		'idthree' => function ($mediainfo = 'artist') {

			$getID3 = new getID3;
			$mediaid = $getID3->analyze($this->root());

			getid3_lib::CopyTagsToComments($mediaid);

			$title = $mediaid['comments_html']['title'] ;
			$artist = $mediaid['comments_html']['artist'];
			$album = $mediaid['comments_html']['album'];
			$genre = $mediaid['comments_html']['genre'];
			$year = $mediaid['comments_html']['year'];
			$composer = $mediaid['comments_html']['composer'];
			$duration = $mediaid['playtime_string'];


			$mediadata = array(
				'title' => implode('', $title),
				'artist' => implode('', $artist),
				'album' => implode('', $album),
				'genre' => implode('', $genre),
				'year' => implode('', $year),
				'composer' => implode('', $composer),
				'duration' => $duration,
			);

			return $mediadata[$mediainfo];

		}
	]
]);
1 Like