/    Sign up×
Community /Pin to ProfileBookmark

Can anyone show me how to display ‘filesize’ & ‘duration’ from my MP3s?

I’m constructing an Mp3 player for my site that is fed it’s songlist dynamically through the xml.php. All I need in the script is a way to get the ‘filesize’ and ‘duration’ from the songs, and output them in the XML rendering section of the PHP file.

I was just wondering if it is possible to do this with a few lines of code, rather than implement a whole ID3 class into my script. I don’t yet know enough to rip the classes apart and take the functions that I need out. So if anyone here may have a no nonsense, lightweight approach for me, I would love to see it and try it out.

The urls where the physical MP3 files reside are listed in the script here.
I placed ?????? in the XML area for the ‘filesize’&’duration’ variables.
Many thanks to responders.

xml.php

[code]
<?php
session_start();
header(“Cache-control: private”);
if (!$_SESSION[’email’]) {
echo “You aren’t logged in.”;
exit();
}
echo “<?xml version=”1.0″?>n”;
echo “<songs>n”;
echo “<song artist='” . $_SESSION[‘username’] . “‘ title='” . $_SESSION[‘songtitle_1’] . “‘ duration='” . $_SESSION[‘????????’] . “‘ filesize='” . $_SESSION[‘????????’] . “‘ url=’../user_files/song1/” . $_SESSION[‘song_1’] . “‘ />n”;

echo “<song artist='” . $_SESSION[‘username’] . “‘ title='” . $_SESSION[‘songtitle_2’] . “‘ duration='” . $_SESSION[‘????????’] . “‘ filesize='” . $_SESSION[‘????????’] . “‘ url=’../user_files/song2/” . $_SESSION[‘song_2’] . “‘ />n”;

echo “<song artist='” . $_SESSION[‘username’] . “‘ title='” . $_SESSION[‘songtitle_3’] . “‘ duration='” . $_SESSION[‘????????’] . “‘ filesize='” . $_SESSION[‘????????’] . “‘ url=’../user_files/song3/” . $_SESSION[‘song_3’] . “‘ />n”;

echo “<song artist='” . $_SESSION[‘username’] . “‘ title='” . $_SESSION[‘songtitle_4’] . “‘ duration='” . $_SESSION[‘????????’] . “‘ filesize='” . $_SESSION[‘????????’] . “‘ url=’../user_files/song4/” . $_SESSION[‘song_4’] . “‘ />n”;

echo “<song artist='” . $_SESSION[‘username’] . “‘ title='” . $_SESSION[‘songtitle_5’] . “‘ duration='” . $_SESSION[‘????????’] . “‘ filesize='” . $_SESSION[‘????????’] . “‘ url=’../user_files/song5/” . $_SESSION[‘song_5’] . “‘ />n”;

echo “</songs>n”;
?>
[/code]

And this is the XML it renders:

[code]
<?xml version=”1.0″ ?>
– <songs>
<song artist=”Allison” title=”Midnight Ride” duration=”???????” filesize=”???????” url=”../user_files/song1/16.mp3″ />
<song artist=”Allison” title=”Almost Me Again” duration=”???????” filesize=”???????” url=”../user_files/song2/16.mp3″ />
<song artist=”Allison” title=”Hope you Live” duration=”???????” filesize=”???????” url=”../user_files/song3/16.mp3″ />
<song artist=”Allison” title=”Midnight Ride” duration=”???????” filesize=”???????” url=”../user_files/song4/16.mp3″ />
<song artist=”Allison” title=”punched a hole in it” duration=”???????” filesize=”???????” url=”../user_files/song5/16.mp3″ />
</songs>
[/code]

The member’s various mp3s all have the same name, just spread to 5 different folders.
Thanks

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 22.2006 — File size is easy: [url=http://www.php.net/filesize]filesize()[/url].
Copy linkTweet thisAlerts:
@WebjedikungfuauthorDec 22.2006 — Thanks

Would this work?

[code=php]
echo "<song artist='" . $_SESSION['username'] . "' title='" . $_SESSION['songtitle_1'] . "' duration='" . $_SESSION['????????'] . "' filesize='" . $_SESSION['filesize()'] . "' url='../user_files/song1/" . $_SESSION['song_1'] . "' />n";
[/code]

Which variable should go inside the filesize brackets? $song_1?
Copy linkTweet thisAlerts:
@NightShift58Dec 22.2006 — You may want to take a look at: GetID3.org

This is what it does:
getID3() is a PHP script that extracts useful information (such as ID3 tags, bitrate, playtime, etc.) from MP3s & other multimedia file formats (Ogg, WMA, WMV, ASF, WAV, AVI, AAC, VQF, FLAC, MusePack, Real, QuickTime, Monkey's Audio, MIDI and more).[/QUOTE]
Copy linkTweet thisAlerts:
@WebjedikungfuauthorDec 23.2006 — OK, here is the solution I created for finding MP3 file sizes(tried a few different file types, and they all worked txt,php,html)
<i>
</i>&lt;?php

// this line puts the file into variable($filename) from it's url location

$filename = '../../user_files/song3/' . $_SESSION['song_3'] . '';


// and then the XML rendering with variable in place

echo "&lt;song artist='" . $_SESSION['username'] . "' title='" . $_SESSION['songtitle_1'] . "' duration='" . $_SESSION['song1time'] . "' filesize='" . filesize($filename) . "' url='../user_files/song1/" . $_SESSION['song_1'] . "' /&gt;n";


?&gt;


Now that that's out of the way, does anybody know how to determine MP3 "duration" or "playtime"? It is the last bit of coding I need for the player.

Would it work in the same fashion as filesize? I'll go lookin'
Copy linkTweet thisAlerts:
@NightShift58Dec 23.2006 — Take a look at the preceding post...
Copy linkTweet thisAlerts:
@WebjedikungfuauthorDec 23.2006 — Yeah, I went there the first time you showed me in the preceding post. I can't seem to figure out where and within getid3 they display and figure the playtime or duration. Plus I'm just looking for a few lines of code like I did with filesize. I don't wanna have to go fishing through files of arrays that I don't get quite yet.

But thanks for the link

Just looking for someone who knows the code to show me how it is done. I've googled my brains out. I'm very new to coding, so those ID3 classes and packages are too complex for me to break apart and use
Copy linkTweet thisAlerts:
@NightShift58Dec 23.2006 — I think that that is the beauty of this program: you don't really have to "mess" with it all that to still get mileage out of it. This is how you could use it:[code=php]<?php
// include getID3() library
require_once('getid3.php');

// Initialize getID3 engine
$getID3 = new getID3;

// Analyze file and store returned data in $thisMP3info
$thisMP3info = $getID3->analyze($filename);

// Optional: copies data from all subarrays of [tags] into [comments] so
// metadata is all available in one location for all tag formats
// metainformation is always available under [tags] even if this is not called
getid3_lib::CopyTagsToComments($thisMP3info);

// Output desired information in whatever format you want
// Note: all entries in [comments] or [tags] are arrays of strings
// See structure.txt for information on what information is available where
// or check out the output of /demos/demo.browse.php for a particular file
// to see the full detail of what information is returned where in the array
$MP3artist = $thisMP3nfo['comments_html']['artist'][0]; // artist from any/all available tag formats
$MP3title = $thisMP3info['tags']['id3v2']['title'][0]; // title from ID3v2
$MP3bitrate $thisMP3nfo['audio']['bitrate']; // audio bitrate
$MP3duraton = $thisMP3nfo['playtime_string']; // playtime in minutes:seconds, formatted string
?>[/code]
This slighlty modified example was taken the getID3.org site. Not counting the comments, you can get all the information you need in 8 lines of code.
Copy linkTweet thisAlerts:
@WebjedikungfuauthorDec 24.2006 — Haha, it worked finally. Thanks for helping me NightShift. I need to learn PHP more. Kinda dissapointed though cuz I was so close to coding the whole thing myself, but on the other hand getID3 gives a whole lot of info on the file. Info it would take me a month to learn how to extract.

Happy holidays NightShift, and all
Copy linkTweet thisAlerts:
@NightShift58Dec 24.2006 — Best wishes to you as well!
×

Success!

Help @Webjedikungfu spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...