bschultz Posted March 1, 2017 Share Posted March 1, 2017 (edited) I have a directory of mp3 files that I need to find out the combined length in minutes and seconds of all the audio files.The files are all 128kbps stereo mp3's. Here's what I have so far... <?php //connect to remote server (hostname, port) $connection = ssh2_connect('192.168.2.4', 22); //authenticate ssh2_auth_password($connection, 'username', 'password'); //execute remote command (replace /path/to/directory with absolute path) $stream = ssh2_exec($connection, 'du -k /remotedirectory'); stream_set_blocking($stream, true); //get the output $dirSize = stream_get_contents($stream); //show the output and close the connection $showsize = $dirSize; //echo $showsize; exit; $math = (($showsize * 1000) / 128); //without the /128 it shows 34308000...which is correct. the files are 128kbps //echo $math; exit; echo gmdate("i:s", $math); // shows 27:11 which is wrong...the actual total time of all of the files in the directory is 36:34 ...minutes and seconds fclose($stream); ?> Any ideas where I'm off in the logic of the math?Thanks. Edited March 1, 2017 by bschultz Quote Link to comment https://forums.phpfreaks.com/topic/303321-math-from-filesize/ Share on other sites More sharing options...
requinix Posted March 1, 2017 Share Posted March 1, 2017 Are the files all constant-rate 128Kbps? Have they been stripped of all ID3 tags? $showsize is in KB so the /1000 would be both wrong (check the man page for what -k means) and unnecessary. 34308 KB * 8 = 274464 Kb / 128 Kbps = 2144.25 sec = 35:44. So there's something else going on. Quote Link to comment https://forums.phpfreaks.com/topic/303321-math-from-filesize/#findComment-1543559 Share on other sites More sharing options...
bschultz Posted March 1, 2017 Author Share Posted March 1, 2017 (edited) All files are 128k constant. id3 tags are empty. That's how they're downloaded. I read that -k would force KB...but without it, $showsize was off from what right clicking the directory showed me for file size. Would it be better to get size of all files instead of directory size? Does du take into account the size of all files individually, or just the combined disk space used? Edited March 1, 2017 by bschultz Quote Link to comment https://forums.phpfreaks.com/topic/303321-math-from-filesize/#findComment-1543560 Share on other sites More sharing options...
Psycho Posted March 1, 2017 Share Posted March 1, 2017 Using file size of a bunch of MP3 files to determine the total play length seems a bit clunky. How many files are there? Why not get the actual play length of the files directly from the file headers? Quote Link to comment https://forums.phpfreaks.com/topic/303321-math-from-filesize/#findComment-1543564 Share on other sites More sharing options...
bschultz Posted March 1, 2017 Author Share Posted March 1, 2017 30 files in this directory Quote Link to comment https://forums.phpfreaks.com/topic/303321-math-from-filesize/#findComment-1543565 Share on other sites More sharing options...
Solution Psycho Posted March 1, 2017 Solution Share Posted March 1, 2017 Here's a class to get the duration of an MP3 file from the header information. http://www.zedwood.com/article/php-calculate-duration-of-mp3 Quote Link to comment https://forums.phpfreaks.com/topic/303321-math-from-filesize/#findComment-1543567 Share on other sites More sharing options...
bschultz Posted March 1, 2017 Author Share Posted March 1, 2017 That class will work....thank you! Quote Link to comment https://forums.phpfreaks.com/topic/303321-math-from-filesize/#findComment-1543568 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.