如何从其URL获取MP3的长度?

时间:2021-09-01 03:53:11

If I know the URL of an MP3 file, what is the easiest/fastest way to get its length, bitrate, size, etc?

如果我知道MP3文件的URL,获取其长度,比特率,大小等的最简单/最快的方法是什么?

How can I download just the ID3 tag part of the MP3 to get these details?

如何才能下载MP3的ID3标签部分以获取这些详细信息?

6 个解决方案

#1


You will need to look at the ID3 tags in the mp3 file.

您需要查看mp3文件中的ID3标签。

Unless you keep track of the metadata you want somewhere else.

除非您在其他地方跟踪您想要的元数据。

To specifically get the Track length of the file you will need to look into the ID3 metadata tag for sure, specifically the 'TRCK' frame of the tag.

要专门获取文件的跟踪长度,您需要查看ID3元数据标记,特别是标记的“TRCK”框架。

To only download the ID3 Tag part, you must first download the ID3 header part of the file.

要仅下载ID3标记部分,必须先下载文件的ID3标题部分。

This website contains very specific information about the ID3 Tag format. You will need to look at the version number of the ID3 Tag and then, based on that, you will need to find the information regarding how long the ID3 Tag is. Then, you must download the WHOLE tag because the frames are not in any specific order.

本网站包含有关ID3标签格式的非常具体的信息。您需要查看ID3标签的版本号,然后,基于此,您需要找到有关ID3标签的时间长度的信息。然后,您必须下载WHOLE标记,因为框架没有任何特定顺序。

Then you should be able to use a third party library to find the TRCK frame and its data.

然后,您应该能够使用第三方库来查找TRCK帧及其数据。

#2


Here is how you get file size, but how do you get bitrate and track length?

以下是获取文件大小的方法,但是如何获得比特率和跟踪长度?

import java.io.*;
import java.net.*;

public class FileSizeFromURL {
public static final void main(String[] args) {
URL url;
URLConnection conn;
int size;

if(args.length != 1) {
  System.out.println("Usage: FileSizeFromURL ");
  return;
  }

try {
  url = new URL(args[0]);
  conn = url.openConnection();
  size = conn.getContentLength();
  if(size < 0)
     System.out.println("Could not determine file size.");
  else
    System.out.println(args[0] + "\nSize: " + size);
  conn.getInputStream().close();
  } 
catch(Exception e) {
  e.printStackTrace();
  }
}
}

#3


what is the easiest/fastest way to get its length, bitrate, size, etc?

获得长度,比特率,大小等最简单/最快捷的方法是什么?

File size you can get with an HTTP HEAD request. If by ‘length’ you mean playing time in seconds, you cannot get this without fetching the entire file. You can guess, by fetching the first few MP3 frames, looking at their bitrate, and assuming that the rest of the file has the same bitrate, but given the popularity of Variable Bit-Rate encoding the likelihood this will be close to accurate is quite low.

使用HTTP HEAD请求可以获得的文件大小。如果按“长度”表示以秒为单位的播放时间,则无法获取整个文件。您可以猜测,通过获取前几个MP3帧,查看它们的比特率,并假设文件的其余部分具有相同的比特率,但考虑到可变比特率编码的普及,这将接近准确的可能性是相当的低。

ID3 tags can in theory contain information that might allow you to guess the length better, in the ASPI and ETCO tags. But in practice these are very rarely present.

理论上,ID3标签可以包含可能允许您在ASPI和ETCO标签中更好地猜测长度的信息。但在实践中,这些很少出现。

How can I download just the ID3 tag part of the MP3 to get these details?

如何才能下载MP3的ID3标签部分以获取这些详细信息?

For ID3v2 tags, grab the start of the file. (It's possible for ID3v2 frames to be elsewhere, but in practice they're always there.) You can't tell how long the tag is going to be in advance. For text-only tags you're likely to find the information you want in the first 512-1024 bytes. Unfortunately more and more MP3s have embedded ‘album art’ pictures, which can be much longer; try to pick an ID3 library that will gracefully ignore truncated ID3 information.

对于ID3v2标签,请抓取文件的开头。 (ID3v2帧可能在其他地方,但实际上它们总是在那里。)你无法预先确定标签的长度。对于纯文本标签,您可能会在前512-1024字节中找到所需的信息。不幸的是,越来越多的MP3嵌入了“专辑艺术”图片,这可能会更长;尝试选择一个优雅地忽略截断的ID3信息的ID3库。

ID3v1 tags are located at the end of the file. Again you can't tell how long they're going to be. And of course you don't know in advance whether the file has ID3v1 tags, ID3v2 tags, both or neither. Generally these days ID3v2 is a better bet though.

ID3v1标签位于文件末尾。你再也不知道他们将会有多长时间。当然,您事先并不知道该文件是否具有ID3v1标签,ID3v2标签,两者都兼有。一般来说,这些天ID3v2是更好的选择。

To read part of a file through HTTP you need the Range header. This too is not supported everywhere.

要通过HTTP读取文件的一部分,您需要Range标头。这在任何地方都不受支持。

In summary, there are enough problems with this that the best option may well be giving up and just fetching the whole file.

总之,这有足够的问题,最好的选择可能是放弃并只是获取整个文件。

#4


I am answering this question a year+ after it was asked but I do have some info to add for those happening by at a later time. I answered a similar question about getting the image dimensions from an image on a remote server without downloading the whole image. See that discussion at the following URL:

我在问了一年之后回答了这个问题,但我确实有一些信息可以在以后添加。我回答了类似的问题,关于从远程服务器上的图像获取图像尺寸而不下载整个图像。请在以下网址查看该讨论:

Get image dimensions with Curl

使用Curl获取图像尺寸

You could definitely use the same technique to pull info out of the ID3 tags without downloading the whole MP3 from a remote server.

您绝对可以使用相同的技术从ID3标签中提取信息,而无需从远程服务器下载整个MP3。

I hope that helps for future passers-by.

我希望这有助于未来的路人。

#5


You cannot get this information from the URL alone.

您无法仅通过URL获取此信息。

You'll have to load the first few K of content and use an mp3 library to decode the header and get the values.

您必须加载前几个K内容并使用mp3库解码标头并获取值。

#6


That data is encoded in the id3 tag, so you need to download at least that much of the file, which is at the beginning so you're in luck.

该数据在id3标签中进行编码,因此您需要至少下载该文件的大部分内容,这样您就很幸运了。

Alternatively you can look at the content-length header value in the http response header to know the length, if the server tells you which it may not.

或者,您可以查看http响应标头中的content-length标头值以了解长度,如果服务器告诉您它可能不是。

#1


You will need to look at the ID3 tags in the mp3 file.

您需要查看mp3文件中的ID3标签。

Unless you keep track of the metadata you want somewhere else.

除非您在其他地方跟踪您想要的元数据。

To specifically get the Track length of the file you will need to look into the ID3 metadata tag for sure, specifically the 'TRCK' frame of the tag.

要专门获取文件的跟踪长度,您需要查看ID3元数据标记,特别是标记的“TRCK”框架。

To only download the ID3 Tag part, you must first download the ID3 header part of the file.

要仅下载ID3标记部分,必须先下载文件的ID3标题部分。

This website contains very specific information about the ID3 Tag format. You will need to look at the version number of the ID3 Tag and then, based on that, you will need to find the information regarding how long the ID3 Tag is. Then, you must download the WHOLE tag because the frames are not in any specific order.

本网站包含有关ID3标签格式的非常具体的信息。您需要查看ID3标签的版本号,然后,基于此,您需要找到有关ID3标签的时间长度的信息。然后,您必须下载WHOLE标记,因为框架没有任何特定顺序。

Then you should be able to use a third party library to find the TRCK frame and its data.

然后,您应该能够使用第三方库来查找TRCK帧及其数据。

#2


Here is how you get file size, but how do you get bitrate and track length?

以下是获取文件大小的方法,但是如何获得比特率和跟踪长度?

import java.io.*;
import java.net.*;

public class FileSizeFromURL {
public static final void main(String[] args) {
URL url;
URLConnection conn;
int size;

if(args.length != 1) {
  System.out.println("Usage: FileSizeFromURL ");
  return;
  }

try {
  url = new URL(args[0]);
  conn = url.openConnection();
  size = conn.getContentLength();
  if(size < 0)
     System.out.println("Could not determine file size.");
  else
    System.out.println(args[0] + "\nSize: " + size);
  conn.getInputStream().close();
  } 
catch(Exception e) {
  e.printStackTrace();
  }
}
}

#3


what is the easiest/fastest way to get its length, bitrate, size, etc?

获得长度,比特率,大小等最简单/最快捷的方法是什么?

File size you can get with an HTTP HEAD request. If by ‘length’ you mean playing time in seconds, you cannot get this without fetching the entire file. You can guess, by fetching the first few MP3 frames, looking at their bitrate, and assuming that the rest of the file has the same bitrate, but given the popularity of Variable Bit-Rate encoding the likelihood this will be close to accurate is quite low.

使用HTTP HEAD请求可以获得的文件大小。如果按“长度”表示以秒为单位的播放时间,则无法获取整个文件。您可以猜测,通过获取前几个MP3帧,查看它们的比特率,并假设文件的其余部分具有相同的比特率,但考虑到可变比特率编码的普及,这将接近准确的可能性是相当的低。

ID3 tags can in theory contain information that might allow you to guess the length better, in the ASPI and ETCO tags. But in practice these are very rarely present.

理论上,ID3标签可以包含可能允许您在ASPI和ETCO标签中更好地猜测长度的信息。但在实践中,这些很少出现。

How can I download just the ID3 tag part of the MP3 to get these details?

如何才能下载MP3的ID3标签部分以获取这些详细信息?

For ID3v2 tags, grab the start of the file. (It's possible for ID3v2 frames to be elsewhere, but in practice they're always there.) You can't tell how long the tag is going to be in advance. For text-only tags you're likely to find the information you want in the first 512-1024 bytes. Unfortunately more and more MP3s have embedded ‘album art’ pictures, which can be much longer; try to pick an ID3 library that will gracefully ignore truncated ID3 information.

对于ID3v2标签,请抓取文件的开头。 (ID3v2帧可能在其他地方,但实际上它们总是在那里。)你无法预先确定标签的长度。对于纯文本标签,您可能会在前512-1024字节中找到所需的信息。不幸的是,越来越多的MP3嵌入了“专辑艺术”图片,这可能会更长;尝试选择一个优雅地忽略截断的ID3信息的ID3库。

ID3v1 tags are located at the end of the file. Again you can't tell how long they're going to be. And of course you don't know in advance whether the file has ID3v1 tags, ID3v2 tags, both or neither. Generally these days ID3v2 is a better bet though.

ID3v1标签位于文件末尾。你再也不知道他们将会有多长时间。当然,您事先并不知道该文件是否具有ID3v1标签,ID3v2标签,两者都兼有。一般来说,这些天ID3v2是更好的选择。

To read part of a file through HTTP you need the Range header. This too is not supported everywhere.

要通过HTTP读取文件的一部分,您需要Range标头。这在任何地方都不受支持。

In summary, there are enough problems with this that the best option may well be giving up and just fetching the whole file.

总之,这有足够的问题,最好的选择可能是放弃并只是获取整个文件。

#4


I am answering this question a year+ after it was asked but I do have some info to add for those happening by at a later time. I answered a similar question about getting the image dimensions from an image on a remote server without downloading the whole image. See that discussion at the following URL:

我在问了一年之后回答了这个问题,但我确实有一些信息可以在以后添加。我回答了类似的问题,关于从远程服务器上的图像获取图像尺寸而不下载整个图像。请在以下网址查看该讨论:

Get image dimensions with Curl

使用Curl获取图像尺寸

You could definitely use the same technique to pull info out of the ID3 tags without downloading the whole MP3 from a remote server.

您绝对可以使用相同的技术从ID3标签中提取信息,而无需从远程服务器下载整个MP3。

I hope that helps for future passers-by.

我希望这有助于未来的路人。

#5


You cannot get this information from the URL alone.

您无法仅通过URL获取此信息。

You'll have to load the first few K of content and use an mp3 library to decode the header and get the values.

您必须加载前几个K内容并使用mp3库解码标头并获取值。

#6


That data is encoded in the id3 tag, so you need to download at least that much of the file, which is at the beginning so you're in luck.

该数据在id3标签中进行编码,因此您需要至少下载该文件的大部分内容,这样您就很幸运了。

Alternatively you can look at the content-length header value in the http response header to know the length, if the server tells you which it may not.

或者,您可以查看http响应标头中的content-length标头值以了解长度,如果服务器告诉您它可能不是。