javascript 如何获取 mp3 文件的大小和持续时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20771845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:15:51  来源:igfitidea点击:

How to get the size and duration of an mp3 file?

javascriptphpjquery

提问by Ricky

I need to calculate the total length of an mp3 file.

我需要计算一个 mp3 文件的总长度。

Currently I am using a PHP class which I found @ http://www.zedwood.com/article/php-calculate-duration-of-mp3.

目前我正在使用一个 PHP 类,我发现它 @ http://www.zedwood.com/article/php-calculate-duration-of-mp3

This is working perfectly if the mp3 file in same server.

如果 mp3 文件在同一服务器中,这将完美运行。

but if I have a URL from other site it throwing error. Please help me.

但是如果我有来自其他站点的 URL,它会抛出错误。请帮我。

Is there any JavaScript J-Query function to get the length of the mp3 file

是否有任何 JavaScript J-Query 函数来获取 mp3 文件的长度

<?php include("mp3.class.php");
$f = 'http://cdn.enjoypur.vc/upload_file/5570/5738/5739/7924/Blue%20Eyes%20-%20Yo%20Yo%20Honey%20Singh%20(PagalWorld.com)%20-192Kbps%20.mp3';
$m = new mp3file($f);
$a = $m->get_metadata();

if ($a['Encoding']=='Unknown')
    echo "?";
else if ($a['Encoding']=='VBR')
    print_r($a);
else if ($a['Encoding']=='CBR')
    print_r($a);
unset($a);
?>

回答by user2486495

There is actually a library that can run at client-side, attempting to fetch just enough of the MP3 to read the ID3 tags:

实际上有一个可以在客户端运行的库,试图获取足够的 MP3 来读取 ID3 标签:

http://github.com/aadsm/JavaScript-ID3-Reader

http://github.com/aadsm/JavaScript-ID3-Reader

or

或者

Try

尝试

HTML File API.

HTML 文件 API。

http://lostechies.com/derickbailey/2013/09/23/getting-audio-file-information-with-htmls-file-api-and-audio-element/

http://lostechies.com/derickbailey/2013/09/23/getting-audio-file-information-with-htmls-file-api-and-audio-element/

回答by Ilyich

Here's how you can get mp3 duration using Web Audio API:

以下是使用 Web Audio API 获取 mp3 时长的方法:

const mp3file = 'https://raw.githubusercontent.com/prof3ssorSt3v3/media-sample-files/master/doorbell.mp3'
const audioContext = new (window.AudioContext || window.webkitAudioContext)()
const request = new XMLHttpRequest()
request.open('GET', mp3file, true)
request.responseType = 'arraybuffer'
request.onload = function() {
    audioContext.decodeAudioData(request.response,
        function(buffer) {
            let duration = buffer.duration
            console.log(duration)
            document.write(duration)
        }
    )
}
request.send()

回答by Adarsh Nahar

Use getID3()PHP library that works for VBR files as well.

使用getID3()也适用于 VBR 文件的 PHP 库。

This link help you sourceforge.net.

此链接可帮助您sourceforge.net

It's very much active in development.

它在开发中非常活跃。

回答by Kirk

A Famous and Very SPI that you can use MP3 SPIand the code is also very simple

一个非常有名的SPI,可以使用MP3 SPI,代码也很简单

File file = new File("filename.mp3");
AudioFileFormat baseFileFormat = AudioSystem.getAudioFileFormat(file);
Map properties = baseFileFormat.properties();
Long duration = (Long) properties.get("duration");