Getting the length of an MP3 the quick way

  |   By  |  0 Comments

When you create content for a podcast for iTunes, Amazon Music and Spotify, the xml feed requires the length of the MP3 – that can be a problem if you are using an external mp3 file. It’s not easy to use the PHP get_id3 libraries to determine the length. If you can get them to open the external file, they often time out.

There’s a much easier way – do it client side with javascript!

<script>// Create a non-dom allocated Audio element
var au = document.createElement('audio');

// Define the URL of the MP3 audio file
au.src = "https://mydomain.com/file.mp3";

// Once the metadata has been loaded, display the duration in the console
au.addEventListener('loadedmetadata', function(){
    // Obtain the duration in seconds of the audio file (with milliseconds as well, a float value)
    var duration = parseInt(au.duration);

 
    console.log("The duration of the song is : " + duration + " seconds");
    
    var date = new Date(null);
    date.setSeconds(duration); 
    var hhmmssFormat = date.toISOString().substr(11, 8);
    console.log(hhmmssFormat);
},false);

</script>

Here is how I used that snippet in one of my WordPress plugins – when the form field is filled in, the length is detected and the value in hh:mm:ss format is put in the hidden form field.

echo'<tr><th scope="row">'.__('External Audio mp3/M4a URL','church-admin').'</th><td><input type="text" name="audio_url" id="audio_url"';
    if(!empty( $errors['audio_url'] ) ) echo 'style="border:1px red solid"';
    if(!empty( $current_data->external_file) )echo' value="'.esc_url( $current_data->external_file).'" ';
    echo'/>';
    echo'<input Type="hidden" name="external_duration" id="external_duration" ';
    if( !empty( $current_data->length ) )echo' value="'.esc_attr($current_data->length).'"';
    echo '/></td></tr>';
    //javascript to detect length of external audio
    echo'<script>
    jQuery(document).ready(function($){
        $("#audio_url").change(function(){
            var url=$(this).val();
            var au = document.createElement("audio");

            // Define the URL of the MP3 audio file
            au.src = url;

            // Once the metadata has been loaded, display the duration in the console
            au.addEventListener("loadedmetadata", function(){
                    
                    var duration = parseInt(au.duration);
                    //convert to ISO format
                    var date = new Date(null);
                    date.setSeconds(duration); 
                    var hhmmssFormat = date.toISOString().substr(11, 8);
                    console.log("External duration is "+ hhmmssFormat);
                    $("#external_duration").val(hhmmssFormat);
                },false);
            })

    });
    
    </script>';
name

ABOUT THE AUTHOR - ANDY MOYLE

Andy Moyle is a church leader and web developer. His biggest project is the Church Admin WordPress plugin and app. He also runs, mainly so he can eat pizza.