Skip to content
المشاريع

caffco/get-audio-duration

عرض على GitHub

حول

Returns duration of an audio file via ffprobe

التصميم والوسائط

كيف تُقاس هذه الأرقام

النمو المقاس للنجوم

خلال 24 ساعة
٠
خلال 7 أيام
٠
خلال 15 يومًا
٠
خلال شهر
جارٍ جمع السجل

تغيّر صافي النجوم خلال فترات متداخلة، وليس مجموعًا يوميًا. تبقى القياسات المفقودة فارغة. النجوم تقيس الاهتمام لا جودة البرمجيات.

README.md

الأصل

get-audio-duration

NPM version Build Status codecov License Downloads

Get the duration of audio files with music-metadata.

Supported platforms

  • Node.js: Requires Node.js ≥ 22. Give a file path, a Blob, a File or a Uint8Array (including Buffer).
  • Browser: Give a Blob, a File or a Uint8Array. A file path does not work in the browser. The tests use Vite and Chromium.

The package uses ES modules ("type": "module"). It also has a CommonJS entry point. TypeScript declarations are available for the two module formats.

Install

$ npm install --save get-audio-duration

Usage

The optional second argument selects the duration in seconds. By default, durationSource: 'audio' uses the first audio track. Use { durationSource: 'container' } for the complete presentation or { durationSource: 'video' } for the first video track. Every source falls back to the format duration when its preferred duration is unavailable.

TypeScript callers can import GetAudioDurationOptions and DurationSource from either module format:

import {
  getAudioDurationInSeconds,
  type GetAudioDurationOptions,
} from 'get-audio-duration'

const options: GetAudioDurationOptions = { durationSource: 'container' }
const duration = await getAudioDurationInSeconds('audio.flac', options)

ESM

import { getAudioDurationInSeconds } from 'get-audio-duration'

getAudioDurationInSeconds('audio.flac').then((duration) => {
  console.log(duration)
})

CommonJS

const { getAudioDurationInSeconds } = require('get-audio-duration')

getAudioDurationInSeconds('audio.flac').then((duration) => {
  console.log(duration)
})

The two module formats also export getAudioDurationInSeconds as the default export. In CommonJS, use require('get-audio-duration').default. The CommonJS entry point loads the ES module implementation. The function always returns a promise.

Browser

import { getAudioDurationInSeconds } from 'get-audio-duration'

// Give a Blob or a File, for example from a file input.
const [file] = document.querySelector('input[type="file"]').files
getAudioDurationInSeconds(file).then((duration) => {
  console.log(duration)
})

You can also pass audio bytes directly, without converting them to a Blob:

const response = await fetch('audio.flac')
const bytes = new Uint8Array(await response.arrayBuffer())
const duration = await getAudioDurationInSeconds(bytes)
console.log(duration)

Inputs created in another browser realm, such as a same-origin iframe, are also supported.

Unsupported input types reject with a TypeError.

استكشف مشاريع مشابهة ومرتبطة

جارٍ التحميل…