Get length of media file

Discussion in 'JavaScript' started by zakula11, Jul 23, 2006.

  1. #1
    I was wondering... if you have a mp3 file, is it possible to get the length of the file? Like in Windows Media Player it displays the length of the song. Is this possible through javascript or any other web programming? If not straight from the file itself, what about if Windows Media Player was embedded in the website, can it be obtained through the player?
    Thanks
     
    zakula11, Jul 23, 2006 IP
  2. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #2
    You can do it with a server-side language. What kind of box are you on? (Microsoft, *Nix, etc...) Otherwise you could build a "pre-loader" for the mp3 file in Flash, - then have Flash communicate to your javascript with the size of the Preloaded mp3 file.
     
    ccoonen, Jul 24, 2006 IP
  3. zakula11

    zakula11 Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm running Microsoft Windows Server 2003. I can use any server-side language, such as PHP?

    How would I do that using PHP?
     
    zakula11, Jul 24, 2006 IP
  4. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #4
    <?php

    // outputs e.g. somefile.txt: 1024 bytes

    $filename = 'somefile.txt';
    echo $filename . ': ' . filesize($filename) . ' bytes';

    ?>

    for more examples, check out php.net: http://us3.php.net/filesize
     
    ccoonen, Jul 25, 2006 IP
  5. zakula11

    zakula11 Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you, this is good. But also I was wondering if it is possible to get the length, in time, of a media file. For example, take a mp3 file and output something like "3:45" for 3 minutes and 45 seconds long
     
    zakula11, Jul 25, 2006 IP
  6. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #6
    their might be some classes built out there to handle media types like mp3's - i would just google it :)
     
    ccoonen, Jul 27, 2006 IP
  7. AnimeGround

    AnimeGround Guest

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you use IIS with Windows 2003 Server, you can also do it with ASP (since it comes with it). Enabling by doing start/run/inetmgr and the going into Web Service Extension and enable Active Server Page.

    The simply make a file. asp with this

    <%
    dim fs,f
    set fs=Server.CreateObject("Scripting.FileSystemObject")
    set f=fs.GetFile("c:\mycoolmp3.mp3")
    Response.Write(f.Size & " bytes.")
    set f=nothing
    set fs=nothing
    %>

    Hope this helps, getting php to work on windows 2003 is a bit more complex, you should read a step by step guide if you wish to use php rather than asp.
     
    AnimeGround, Aug 3, 2006 IP