audioduration

package module
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2026 License: GPL-3.0 Imports: 5 Imported by: 0

README

Audio Duration

This is a fork of https://github.com/hcl/audioduration

A naive go module to get a audio file's duration in second.

Usage

import "github.com/sydlexius/audioduration"

For an audio file (eg. mp3 format)

f, _ := os.Open("audio.mp3")
defer f.Close()
d, err := audioduration.Mp3(f)
if err != nil {
	// handling error
}

Or alternatively

f, _ := os.Open("audio.mp3")
defer f.Close()
d, err := audioduration.Duration(f, audioduration.TypeMp3)
if err != nil {
	// handling error
}

Supported formats

MP3, M4A, MP4, FLAC, DSF, OGG, WAV, AAC, WEBM

License

The code is licensed under GPLv3.

    audioduration - audio duration calculation library in go
    Copyright (C) 2021  hcl

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

Credits

The audio samples (files under samples) for package test are from following source:

Documentation

Index

Constants

View Source
const (
	TypeFlac int = 0
	TypeMp4  int = 1
	TypeMp3  int = 2
	TypeOgg  int = 3
	TypeDsd  int = 4
	TypeWav  int = 5
	TypeAac  int = 6
	TypeWebM int = 7
)

File Type Constant

Variables

This section is empty.

Functions

func AAC

func AAC(r io.ReadSeeker) (float64, error)

AAC parses raw AAC ADTS streams and returns duration in seconds. It scans ADTS frames, accumulating samples and dividing by sample rate. Ref: ISO/IEC 13818-7 (ADTS header)

func DSD

func DSD(r io.ReadSeeker) (float64, error)

DSD Calculate dsd files duration.

func Duration

func Duration(file io.ReadSeeker, filetype int) (float64, error)

Duration Get duration of specific music file type.

func FLAC

func FLAC(r io.ReadSeeker) (float64, error)

FLAC Calculate flac files duration.

func Mp3

func Mp3(r io.ReadSeeker) (float64, error)

Mp3 Calculate mp3 files duration.

I/O cost depends on what the stream declares.

  • Xing or VBRI header present: the frame count is read from it and only the first frame's region is touched.
  • Neither header present, constant bitrate: a bounded set of frame headers is sampled across the stream (see probeConstantBitRate) and the duration comes from dividing the audio byte range by the bitrate, which is exact for CBR. Cost is a fixed handful of small windows regardless of file size.
  • Neither header present, variable bitrate: the whole stream is read to end of audio in 256 KiB blocks to count frames, because nothing in the file declares the length and no cheaper method is correct. Only 4-byte frame headers are decoded, never audio, but the bytes are still read.

The last case is negligible for a local file but changes the cost profile for an io.ReadSeeker backed by the network, such as an HTTP range-request reader: size timeouts and any caching accordingly.

func Mp4

func Mp4(r io.ReadSeeker) (float64, error)

Mp4 Calculate mp4 files duration.

func Ogg

func Ogg(r io.ReadSeeker) (float64, error)

Ogg Calculate ogg files duration.

func Wav

func Wav(r io.ReadSeeker) (float64, error)

Wav Calculate wav files duration. It parses RIFF/WAVE with fmt and data chunks. PCM and non-PCM with block alignment are supported via byteRate/blockAlign.

func WebM

func WebM(r io.ReadSeeker) (float64, error)

WebM returns the duration in seconds by reading the EBML structure.

Types

type VBRI

type VBRI struct {
	// contains filtered or unexported fields
}

VBRI VBRI Header

type Xing

type Xing struct {
	// contains filtered or unexported fields
}

Xing Xing Header

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL