Skip to content

Instantly share code, notes, and snippets.

@qoobaa
Created February 18, 2022 12:36
Show Gist options
  • Select an option

  • Save qoobaa/d000fb89c6320f99497a1e5f0067be4a to your computer and use it in GitHub Desktop.

Select an option

Save qoobaa/d000fb89c6320f99497a1e5f0067be4a to your computer and use it in GitHub Desktop.

Revisions

  1. qoobaa created this gist Feb 18, 2022.
    19 changes: 19 additions & 0 deletions mixlr.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    require 'json'
    require 'open-uri'
    require 'open3'

    STREAMS_URL = 'https://mixlr-codetest.herokuapp.com/stations/netil-radio/streams'

    body = URI.open(STREAMS_URL).read
    json = JSON.parse(body)

    volumes = json.dig('data', 'attributes', 'streams').map do |stream_name, url|
    _, stderr, process_info = Open3.capture3('ffmpeg', '-t', '3', '-i', url, '-af', 'volumedetect', '-f', 'null', '/dev/null')

    mean_volume = stderr.match(/mean_volume: (.*) dB/)[1].to_f
    max_volume = stderr.match(/max_volume: (.*) dB/)[1].to_f

    {stream_name:, mean_volume:, max_volume:}
    end

    puts JSON.pretty_generate(volumes)