Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to get the duration of an mp3 file

how to get the duration of an mp3 file

Scheduled Pinned Locked Moved C#
csharptutorial
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sivaprasad C
    wrote on last edited by
    #1

    Hi, how to get the duration of an mp3 file using c#

    Regards, Sivaprasad

    J C G 3 Replies Last reply
    0
    • S Sivaprasad C

      Hi, how to get the duration of an mp3 file using c#

      Regards, Sivaprasad

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      This isn't something you can do natively in the .NET framework. You'll need to use a 3rd party library to do this. For example, Managed DirectX has APIs to do this.

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

      1 Reply Last reply
      0
      • S Sivaprasad C

        Hi, how to get the duration of an mp3 file using c#

        Regards, Sivaprasad

        C Offline
        C Offline
        ComCoderCsharp
        wrote on last edited by
        #3

        If you know the samplingsfrequency Fs and number of samples N: duration = N/Fs [sec] Cheers Al

        1 Reply Last reply
        0
        • S Sivaprasad C

          Hi, how to get the duration of an mp3 file using c#

          Regards, Sivaprasad

          G Offline
          G Offline
          greycrow
          wrote on last edited by
          #4

          If I oversimplify this or you all ready know parts of this, I appologize. But here goes... MP3 files are made up of a bunch of music sections, each having a header. At the end of the file is a TAG 128 bytes long. The Gist of getting the duration of an MP3 is to first get the size of the file, then subtract 128 bytes for the TAG. Divide the number of remaining "Bits" in the file by the bitrate. So to get the file length: . . using System.IO; . . FileStream MP3File; . MP3File = new FileStream("C:\\mySong.mp3", FileMode.Open); . FileLength = MP3File.Length; // gives file length in bytes . And for the duration: . Duration = ((FileLength - 128) * 8) / bitrate; //gives duration in seconds . The problem now lies in getting the bitrate. If you know all your files are say 128kbits/sec you can cheat an get a pretty good estimate. So your formula would be. Duration = ((FileLength - 128) * 8) / (1024*128); The problem is real MP3 files can have different bitrates within individiual song blocks. So it would be neccessary to read each block header for the bitrate and then add everything up at the end. Rather a tedious exercise. Hope that helps a bit greycrow

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups