Search Unity

Question How can I extract the audio from a video tag in the js side?

Discussion in 'Web' started by Marks4, Jul 26, 2022.

  1. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    547
    @The_Island @brendanduncan_u3d Could you help me out here on webgl? I am trying to figure out where on the build.framework.js the audio from a video can be redirected to an audio source. Or how can I create an audio clip in the js side from a video html tag. So the user can use an audio node with an audio clip from the audio of the video. I'm looking at functions such as "jsAudioCreateUncompressedSoundClipFromCompressedAudio", etc.
    The reason I'm asking is because I have this plugin and there's a client that wants to mess with the audio from the video, so I need to make this scheme of extracting the audio from the video and put it into an audio clip/audio source work.

    For example, I have this video tag <video src="somevideourl.mp4"></video> . How can I extract an audio clip or audio source out of it, or redirect the audio from the video to an existing audio source/audio clip on the scene? This video tag is from my plugin, not the normal Video Player. Thank you.
     
  2. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    547
    Hey @brendanduncan_u3d . I managed to take an HTML5 video and extract its AudioBuffer, convert it into a wav file format, then into an object url, which is then downloaded into a Unity Audio Clip. I basically followed this tutorial. It works, but it's slow, the wav file is huge, and not optimal.

    However, I am pretty sure Unity doesn't do this. It must be a much simpler and effective process of redirecting the audio from a video into an audio clip, instead of converting the audio into a wav file first. Do you know how I can redirect the audio from an HTML5 video into an existing/new audio clip in Unity? I know that all the audios in Unity 2021 are stored at WEBAudio.audioInstances in the build.framework.js, and the HTML5 videos are stored at videoInstances . What I need to understand is how the audio from the videos in these video instances can be redirected into an audio from an audioInstance. Because then I will do it with one of my HTML5 videos. I know that WEBAudio.audioInstances only exist if there's at least one audio source node with an audio clip in the scene, but that's ok. I can have a dummy audio clip. I want to learn how can I hijack one of these audio instances with an audio from a random HTML5 video.

    Can you give a code snippet or something?
     
    Last edited: Jul 27, 2022
  3. Foreman_Dev

    Foreman_Dev

    Joined:
    Feb 4, 2018
    Posts:
    82
    Marks4 likes this.
  4. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
    Hey. The reason I didn't answer was that I am really not familiar with the VideoPlayer on WebGL. And my colleague who worked on this is on vacation. So with my little knowledge, I did some digging and found out the VideoPlayer doesn't seem to be aware of the AudioSource. So the Video and Audio are completely decoupled, which is not the case on all other platforms. I did a little more digging, and I found out that we are using WEBAudio.audioContext.createMediaElementSource, which seems to work for <audio> and <video>, and call AudioBuffer.getChannelData to get the PCM data. I don't know if it answers your question.
     
    De-Panther, Marks4 and Foreman_Dev like this.
  5. brendanduncan_u3d

    brendanduncan_u3d

    Unity Technologies

    Joined:
    Jul 30, 2019
    Posts:
    437
    The browser's WebAudio API can certainly connect a video element into the audio graph. We use that same mechanism to connect Audio elements to the web audio graph for playback of compressed audio. But it's been a while since I worked in that code to remember if there might be a way of connecting a video element. I can take a look and ask someone who has been in that code recently.
     
    Foreman_Dev and De-Panther like this.
  6. Foreman_Dev

    Foreman_Dev

    Joined:
    Feb 4, 2018
    Posts:
    82
    Thank you both for your replies! I've been doing some testing related to this problem myself. I tested a WebGL build set up with a 3D audio source linked to Unity's video player and found that when you build for WebGL, even if the Unity video player is set up to use a 3D/spatialized audio source, it only outputs via direct audio. This appears to align with @The_Island's findings that in a WebGL build, the VideoPlayer is not aware of the AudioSource. I also found this thread from 2020 that discusses this a bit.

    That being said, is there actually an opportunity for improvement here if a video element can actually be connected into the audio graph in order to use the WebGL-specific implementation of an audio source? Spatial audio is quite important to us in WebGL for the multiplayer platform we are building out, and we'd love to be able to spatialize the audio for the video player (and for @Marks4's plugin).
     
  7. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
  8. brendanduncan_u3d

    brendanduncan_u3d

    Unity Technologies

    Joined:
    Jul 30, 2019
    Posts:
    437
    The PR for the spatialized audio regression on webgl is in the review queue.
     
  9. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    547
    I understand that the audio is decoupled. How can I redirect the audio from an HTML5 video to an audio source in Unity? That's what Unity does for spatialization to work using an Audio Source. The audio from the video is piped to an audio source(or audio clip?). I just need to understand exactly how/where this is done in Unity 2021 on the build.framework.js so that I can hijack an audio instance and use it to play an audio from my video. Thanks!
     
  10. Foreman_Dev

    Foreman_Dev

    Joined:
    Feb 4, 2018
    Posts:
    82
    @The_Island @brendanduncan_u3d Ah, interesting that it was a regression! So, if it is actually possible for the video player to route its stream into the WebGL implementation of an Audio Source, could you (or one of your colleagues) please provide @Marks4 with a code snippet or something, so that they are able to add this functionality to their video player plugin? We'd really appreciate it!
     
  11. brendanduncan_u3d

    brendanduncan_u3d

    Unity Technologies

    Joined:
    Jul 30, 2019
    Posts:
    437
    There was a regression with positional audio, not with video audio. I asked a dev that's been working with the webgl audio code a lot lately to look into video audio connections. It may take a little time, things are busy from vacations and sick days backing things up.
     
    Foreman_Dev likes this.
  12. Foreman_Dev

    Foreman_Dev

    Joined:
    Feb 4, 2018
    Posts:
    82
    Hey @brendanduncan_u3d, I appreciate you looking into this and understand about things getting busy from vacations and sick days backing things up. Looking forward to hearing a solution for this when ready. Thanks! :)
     
  13. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    547
    This forum thread can be closed. I discovered how to successfully hijack an audio source node to play audio(and manipulate it) from any HTML5 videos, including those from my plugin.
     
  14. brendanduncan_u3d

    brendanduncan_u3d

    Unity Technologies

    Joined:
    Jul 30, 2019
    Posts:
    437
    Foreman_Dev likes this.
  15. amelie_1

    amelie_1

    Joined:
    Feb 22, 2020
    Posts:
    15
    And how?
     
    Foreman_Dev and De-Panther like this.