Playing audio files from the web

I’m recently making a project and trying to store songs with a link to their youtube song tied in. However, I was wondering if there was a way to play the song from the web through ocaml without bringing up the page in a browser.
Cheers all and happy Thanksgiving!

These kind of open ended are a bit hard to answer (e.g. which platform, which ui context).

Not sure exactly what’s the shape of youtube APIs these days but for a proof-of-concept I would just youtube-dl the file to disk. Then I’m sure you’ll find among the many packages that liquidoap is made of something to playback the audio channel.

1 Like

Sorry, I should have included that in my question. I was using oCaml through a commandline based music player and was hoping to avoid having to manually deal with all the music as individual files but instead store them as youtube URLs. It would be amazing if there was a way for it to connect to the internet as we played the audio directly from youtube preferably bypassing actually opening the webpage itself.

I believe MPV has some youtube-dl integration, so you can probably do something like:

mpv --no-video "<youtube-video-url>"

(I think you need youtube-dl installed probably)

Thank you! Although I’m not sure about being able to use youtube-dl since it is in python i think?

You’d need python and mpv installed on your system, but your OCaml application could just call out to mpv as a sub-process:

let play_url url =
  ignore @@ Sys.command ("mpv --no-video " ^ url)

let () =
  play_url ""

Awesome, thank you so much, I’ll definitely try this!

Note that Sys.command invokes the command via a shell or cmd.exe on Windows and URLs can contain characters that are interpreted by these (e.g. &). So this may break.

You can try to quote them (e.g. via Filename.quote_command) but save yourself the hassle and just use Unix.create_process for this. The arguments are WYSIWYG. As a bonus point your program will be able to signal the process.