1

I'm making a GET request to receive a list of videos that I then want to display in my React Application. I am making the request as such:

axios({
      method: "get",
      url: `http://localhost:3001/getGalleryVideos`,
    })
      .then((res) => {
        console.log(res.data[0]);
        // setVideo(URL.createObjectURL(res.data[0]));
      })

Now what I receive on the front end in the console.log you see above looks like this:

Browser Console

So my first question is, what is the format or type of this data I have received? I can see the actual data of the files seems to be there as it shows "data: (773491) 0, 0, 0, 32, 102..." im assuming that is the size of the data and then followed by the actual value of the data, but I don't know what type or format this is in.

My next question is, how can I convert this video to the correct format to set it as the source of a html video element like this

<video controls src={video} />

I have tried converting this to a format where I can set this to be the video source of a video element in a react component, but you can see my one of my unsuccessful attempt to set the source above with:

        // setVideo(URL.createObjectURL(res.data[0]));

but this gives me the error:

TypeError: Failed to execute 'createObjectURL' on 'URL': Overload resolution failed.
  • 1
    Do you see this `type: 'Buffer'`? – Konrad Jan 04 '23 at 14:20
  • yeah I saw that, but I'm not sure exactly what that means, is that some kind of in build JavaScript type? Also you can see a little "0" with a drop down next to it, what is that for? – willnotgetbanned Jan 04 '23 at 14:22
  • It's raw data in bytes (see [buffer](https://stackoverflow.com/questions/648309/what-does-it-mean-by-buffer)). Why this `setVideo(URL.createObjectURL(res.data[0]))` didn't work? Did you get any errors? – Konrad Jan 04 '23 at 14:26
  • 1
    yeah I just get the error "TypeError: Failed to execute 'createObjectURL' on 'URL': Overload resolution failed. at Gallery.js:28:1" (I added it to the question) – willnotgetbanned Jan 04 '23 at 14:37

1 Answers1

1

When using Axios in the browser, you can set the responseType to be a blob object.
Media tags (audio/video/image) will accept a blob as input.

axios(
        {
            method: "get",
            url: "http://localhost:3001/getGalleryVideos",
            responseType: "blob",
        }
)
.then( 
        (res) => { setVideo( res.data ); } 
);

Then the setVideo() function looks something like this:

function setVideo ( inputData )
{
    //## Update path first with returned result of the "createObjectURL" process...
    let path = (window.URL || window.webkitURL).createObjectURL( inputData );
    
    //## Test playback... 
    //## Note: must first "load()" the "src" before trying to play media
    myVidObj.src = path;
    myVidObj.load();
    myVidObj.play();
}

Where myVidObj is a reference to your <video> tag.
I don't use React but in pure Javascript it would be referenced by its id name.

...For example:

var myVidObj = document.getElementById("testVid");

...If the tag is setup like this example:

<video id="testVid" width="640" height="480" controls>
VC.One
  • 14,790
  • 4
  • 25
  • 57
  • Thanks for this, this is very helpful however I do have the problem that I am receiving a list of buffers so I dont want to make this all into one blob. So how would I go about dealing with that? – willnotgetbanned Jan 05 '23 at 19:57
  • I don't understand your exact meaning of _"receiving a list of buffers"_... If you have multiple MP4 files to load then just put their **res** into a slot of some Array (_eg:_ called `myVideos`). So the first loaded file's **res** (blob) goes into `myVideos[0];` then second file's blob goes into `myVideos[1];`, third file goes to `myVideos[2];` etc etc... Then you can play whichever blob you need from the Array. For example to play second video file/blob you just use `createObjectURL( myVideos[1] );`. – VC.One Jan 06 '23 at 09:51
  • so I have sent them over as an array, but I don't know how to tell axios to interpret the response as a list of blobs as a pose to a blob. Here is a question where I explain my problem more fully https://stackoverflow.com/questions/75028672/how-can-i-send-a-list-of-videos-from-node-js-to-react-js – willnotgetbanned Jan 08 '23 at 13:48