1. Fabrik 3.8.1 has been released. It is mostly bug fixes and feature enhancements, but does include two new plugins (push notifications, and the sequence element). As usual we strongly recommend testing the new release on a sandbox if your application is mission critical, and always do an Akeeba backup before updating.
    Dismiss Notice

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

Discussion in 'Community' started by uktran, Jan 23, 2018.

  1. uktran

    uktran Member

    Level: Standard
    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: Jan 23, 2018
  2. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Code (Text):

    $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 (Text):

    $uploadPath = '{yourtable___upload_element}';
    if (!empty($uploadPath)) {
       $pathToFile = JPATH_ROOT . '/' . $uploadPath;
       // rest of code goes here
    }
     
    -- hugh
     
  3. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    There's also a 'playtime_string' in $fileinfo, which is formatted as M:S

    -- hugh
     
  4. uktran

    uktran Member

    Level: Standard
    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: Jan 25, 2018
  5. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    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 (Text):

    $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
     
  6. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    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
     
  7. uktran

    uktran Member

    Level: Standard
    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: Jan 26, 2018
  8. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    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
     

Share This Page