Quote for extracting audio duration from mp3 file via file upload element

uktran

Member
Hi can anyone quote me for a bit of calc element code to show either the audio duration of an mp3 file either already on my server or a file uploaded via file upload element?

Was a pro subscriber for a while, but it lapsed and thought this might be easier
 
Last edited:
Code:
$getID3 = FabrikWorker::getID3Instance();
$fileinfo = $getID3->analyze($pathToFile);
return ArrayHelper::getValue('playtime_seconds', $fileinfo, 'no data available');

You'll have to figure out $pathToFile, which if it's an upload element in the same form would be something like ...

Code:
$uploadPath = '{yourtable___upload_element}';
if (!empty($uploadPath)) {
   $pathToFile = JPATH_ROOT . '/' . $uploadPath;
   // rest of code goes here
}

-- hugh
 
Thanks for the start. I just resubbed.

First of all I got a boolean error but a github update fixed that.

Before tackling the file path issue, I'm testing with a single static url as follows.... would this work in theory?

$getID3 = FabrikWorker::getID3Instance();
$fileinfo = $getID3->analyze('https://www.myjoomlasite.com/audio/example.mp3');
return ArrayHelper::getValue('playtime_seconds', $fileinfo, 'no data available');

I'm getting a blank cell. Also tested with relative paths and

return $fileInfo['playtime_string'];



I've tried setting 'calc on save' only to on and saving the form. (as another thread suggest, but I'd ideally want this to work in list view without forms)
 
Last edited:
Is the file local, on your server? I'm pretty sure getid3 needs the file locally, you can't pass it a URL. And you have to specify a full path, like ...

Code:
$fileinfo = $getID3->analyze('/var/www/html/audio/example.mp3');

Hence the JPATH_ROOT prefix in my example. File upload elements store the path relative to the J! root, so you prepend that to the upload path.

If the file is remote, I believe there a trick you can do by reading the first 32k or so of the audio file yourself, using fopen/fread and storing it to a tmp file, and passing that tmp file to analyze. Getid3 doesn't need the whole file to get the metadata, it's all stored in the first few blocks. But that isn't something you'd want to do in a calc element.

-- hugh
 
Tbh, for an upload element, I'd consider writing a little form submission plugin, which does the analyze when the file is uploaded, and stores that in another element on your form, so you don't have to do the fairly expensive analyze for every file in the list view - which could slow your list loading down dramatically.

-- hugh
 
Thanks.

Got it working now I think.


$getID3 = FabrikWorker::getID3Instance();
$uploadPath = '{audio___filepath}';
$path = JPATH_ROOT . $uploadPath;
$fileInfo = $getID3->analyze($path);
return $fileInfo['playtime_string'];


Re form submission, I've had a quick go at writing a script, but hit my wall. Part of the issue is that I am using ajax upload and using a join with the upload repeat table to show the individual files. Is this still possible?
 
Last edited:
Thanks.

Got it working now I think.


$getID3 = FabrikWorker::getID3Instance();
$uploadPath = '{audio___filepath}';
$path = JPATH_ROOT . $uploadPath;
$fileInfo = $getID3->analyze($path);
return $fileInfo['playtime_string'];


Re form submission, I've had a quick go at writing a script, but hit my wall. Part of the issue is that I am using ajax upload and using a join with the upload repeat table to show the individual files. Is this still possible?
Hmm, not easy if you are using multiple Ajax upload. One of these days I'm going to add some built in analysis for audio and video, like we have for images (extracting location from EXIF data).

BTW, you should add the empty test (see my code in previous post) to avoid errors being thrown if no file has been uploaded.

Sent from my HTC6545LVW using Tapatalk
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top