title: Use javascript to detect if an MP4 video has a sound track
body: <p>I am creating a custom controller for MP4 video on a web page. The controller includes a volume slider. Some of the videos that are to be played have no sound track. It would be good to disable the volume slider for these videos, so that the user is not confused when changing the position of the volume slider has no effect.</p>
  
  <p>Is there a property or a trick for checking if an MP4 file has an audio track? (jQuery is an option).</p>
  
  <p>Edit: using @dandavis's suggestion, I now have this solution for Chrome (and .ogg on Opera):</p>
  
  <pre><code>var video = document.getElementById("video")
  var volume = document.getElementById("volume-slider")
  
  function initializeVolume() {
    var enableVolume = true
    var delay = 1
  
    if (video.webkitAudioDecodedByteCount !== undefined) {
      // On Chrome, we can check if there is audio. Disable the volume
      // control by default, and reenable it as soon as a non-zero value
      // for webkitAudioDecodedByteCount is detected.
      enableVolume = false
  
      startTimeout()
  
      function startTimeout () {
        if (!!video.webkitAudioDecodedByteCount) {
          enableVolume = true
          toggleVolumeEnabled(enableVolume)
        } else {
          // Keep trying for 2 seconds
          if (delay &lt; 2048) {
            setTimeout(startTimeout, delay)
            delay = delay * 2
          }
        }
      }
    }
  
    toggleVolumeEnabled(enableVolume)
  }
  
  
  function toggleVolumeEnabled(enableVolume) {
    volume.disabled = !enableVolume
  }
  </code></pre>
  
  <p>The video.webkitAudioDecodedByteCount value is initially 0. In my tests, it may take up to 256ms to get populated with a non-zero value, so I have included a timeout to keep checking (for a while).</p>