Hendré
Hendré

Reputation: 300

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

I have an online audio stream at https://iceant.antfarm.co.za/EdenFM. You can simply paste the audio stream in your browser to hear the audio. My question is: "How do I determine the audio format of the stream? Can this be done with a Linux command?

Upvotes: 0

Views: 457

Answers (2)

Matt
Matt

Reputation: 13943

You can do this with curl by fetching only the header with the -I or --head option. The audio format can be determined based on the Content-Type in the response.

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

With grep you can filter the relevant line:

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

Which outputs this:

Content-Type: audio/aac

Of course this can be done in Java as well by sending a HEAD request to the desired endpoint:

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

Here is a version with the new HttpClient in Java 11:

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

It might be possible that you need to overcome some additional hurdles with SSL/TLS certificates depending on the server, your Java version and other factors.

Upvotes: 2

kebs
kebs

Reputation: 6707

You tag your question with tag::Java but your question only mentions a shell command, so here you are:

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

will get you:

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

The relevant line is of course:

Content-Type: audio/aac

That you can get with a grep.

Upvotes: 1

Related Questions