8

I've written some PHP code to pull out ID3 tags from mp3 files. The next step is to do the same with .m4a files. From the research I've done it looks like most m4a files do not use ID3 but instead a format using 'atoms'.

Are there any PHP libraries out there that can parse out these 'atoms'? I've seen some C#/C++ ones but haven't been able to find any PHP ones. Any other guides or documentation would be great as well.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Nick
  • 6,375
  • 5
  • 36
  • 53
  • 2
    You can try [`getID3()`](http://getid3.sourceforge.net/) - I have had great success with this library in the past for various types of media tags. – DaveRandom Jan 30 '12 at 20:10
  • One thing I forgot to mention is that I dont have the full file. I only have the first X bytes. Does getID3 support partial files? – Nick Jan 30 '12 at 20:17
  • 1
    I think it only reads the tags in the header, and I think this is usually in the first X bytes of the file - but the answer really is "Try it and see" – DaveRandom Jan 30 '12 at 20:29
  • This does not work well with getID3. I have forked the library and integrated it to use partial data files. I have it working, but for an 8MB song, it requires and atom of at least 325kb of data. Is there a better way to parse this data from m4a without downloading so much data? I want to make this more efficient. Are there any other libraries that handle this better, in any language without downloading so much data? – Du3 Sep 24 '12 at 22:41
  • Few years ago I was looking for a reliable mp3 library but never found one. There were many but nothing 100% stable. The situation could change but if not do what I did. Download the source of mp3info (http://ibiblio.org/mp3info/) and rewrite a part which interest you into PHP. If you don't want to do it you can just install the mp3info and call it from your script to get all info. – Lukasz Kujawa Sep 29 '12 at 19:00

2 Answers2

3

This project handles many audio files formats including AAC (M4A is AAC) : http://getid3.sourceforge.net/

hope this can help

fred727
  • 2,644
  • 1
  • 20
  • 16
  • Thx for sharing link. – Gowri Mar 30 '16 at 03:57
  • Not too bad. It seems to have issues with VBR files, though (i.e. it comes up with the wrong duration). – Alexis Wilke Jun 29 '22 at 18:53
  • @Alexis Wilke, in rare cases duration is wrong. That's why for this purpose I prefer this sox shell command which is the strongest solution I found : sox "file.mp3" -n stat 2>&1 | sed -n 's#^Length (seconds):[^0-9]*\([0-9.]*\)$#\1#p' – fred727 Aug 19 '22 at 15:35
-1

I came across a similar issue not too long ago and had a look around. The simplest and most effective method (in my opinion) is to use the exec command in order to extra media information.

I based my code on a forum post over at longtailvideo http://www.longtailvideo.com/support/forums/jw-player/setup-issues-and-embedding/9448/how-to-get-video-duration-with-ffmpeg-and-php

<?php
$videofile="/var/video/user_videos/partofvideo.avi";
ob_start();
passthru("/usr/bin/ffmpeg -i \"{$videofile}\" 2>&1");
$duration = ob_get_contents();
ob_end_clean();

$search='/Duration: (.*?),/';
$duration=preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE, 3);

echo $matches[1][0]; <-- Duration
?>

This script can handle anything ffmpeg is prepared to handle (which is a lot!) I know the above example illustrates a video file but, it will work fine with audio also

Simon
  • 816
  • 2
  • 7
  • 16