如何确定在线流的音频格式?

How do I determine the audio format of a online stream?

我在 https://iceant.antfarm.co.za/EdenFM 有在线音频流。您只需将音频流粘贴到浏览器中即可收听音频。我的问题是:“如何确定流的音频格式?可以使用 Linux 命令完成吗?

您可以使用 curl 执行此操作,方法是使用 -I--head 选项仅获取 header。可以根据响应中的Content-Type确定音频格式。

curl -I https://iceant.antfarm.co.za/EdenFM

使用grep您可以过滤相关行:

curl -I -s https://iceant.antfarm.co.za/EdenFM | grep -i "^Content-Type:"

输出这个:

Content-Type: audio/aac

当然,这也可以在 Java 中通过向所需端点发送 HEAD 请求来完成:

URL url = new URL("https://iceant.antfarm.co.za/EdenFM");
HttpURLConnection con = (HttpURLConnection)  url.openConnection();
con.setRequestMethod("HEAD");
con.connect();
String contentType = con.getContentType();

System.out.println(contentType);
// output: audio/aac

这是 Java11 中带有新 HttpClient 的版本:

HttpClient client = HttpClient.newHttpClient();
        
HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://iceant.antfarm.co.za/EdenFM"))
        .method("HEAD", HttpRequest.BodyPublishers.noBody())
        .build();
HttpResponse<Void> response = client.send(request,
        HttpResponse.BodyHandlers.discarding());

HttpHeaders headers = response.headers();
headers.firstValue("Content-Type").ifPresent(System.out::println);
// output: audio/aac

根据服务器、您的 Java 版本和其他因素,您可能需要使用 SSL/TLS 证书来克服一些额外的障碍。

你用 tag::Java 标记了你的问题,但你的问题只提到了 shell 命令,所以你在这里:

$ curl -I https://iceant.antfarm.co.za/EdenFM

会给你:

HTTP/1.1 200 OK
Content-Type: audio/aac
Date: Thu, 08 Jul 2021 13:33:16 GMT
icy-description:EDEN FM – Your Voice in Paradise
icy-genre:Various
icy-metadata:1
icy-name:EdenFM
icy-pub:1
Server: Icecast 2.4.0-kh13
Cache-Control: no-cache, no-store
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, Accept, X-Requested-With, Content-Type
Access-Control-Allow-Methods: GET, OPTIONS, HEAD
Connection: Close
Expires: Mon, 26 Jul 1997 05:00:00 GMT

相关行当然是:

Content-Type: audio/aac

你可以用 grep 得到。