There seems to be a problem with playing a UI video, but interestingly it functions

I am facing an issue with playing MP4 (HD) videos on the UI that I receive from the Django backend. My setup involves using normal Javascript on the frontend and Django on the backend. Here is a snippet of the backend code:

file = FileWrapper(open(path, 'rb')) #MP4 file path is media/1648477263566_28-03-2022 19:51:05_video.mp4 

response = HttpResponse(file, content_type=content_type)
response['Content-Disposition'] = 'attachment; filename=my_video.mp4'
return response 

The video plays fine on Postman but doesn't work on the UI screen. Below is the UI code:

function getUploadedImageAndVideo(combined_item_id){

    request = {}
    request["combined_item_id"] = combined_item_id;

    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {

        vdata = this.responseText;

        var src1 = document.getElementById('src1');
        src1.setAttribute("src", "data:video/mp4;base64,"+vdata);

        var src2 = document.getElementById('src2');
        src2.setAttribute("src", "data:video/mp4;base64,"+vdata);

        return
      }
    }
    xhttp.open("POST", port + host + "/inventory_apis/getUploadedImageAndVideo", true);

    xhttp.setRequestHeader("Accept", "video/mp4");
    xhttp.setRequestHeader("Content-type", "application/json");
    xhttp.setRequestHeader("X-CSRFToken", getToken());

    xhttp.send( JSON.stringify(request) );
}

On the HTML side:

  <video controls="">
    <source type="video/webm" src="" id="src1">
    <source type="video/mp4" src="" id="src2">
  </video>

The network response (200 OK) of the function call contains a very long text of the video data, but I still cannot play it on the UI. I have tried on both Chrome and Mozilla browsers.

*I know there's an alternative to directly play from the media URL, but I intentionally want to process the video on the backend. Any assistance in resolving this issue would be greatly appreciated.

Answer №1

Upon studying the sequence of

"ftypmp42 ... isommp42 ... mdat ... ó! ... °}b ... $¥Ð ..."

The MP4 format can be segmented into two essential components.

The first component is MOOV, which contains crucial metadata necessary for processing before playback initiation. This metadata includes details about the byte positions of various frames, and without it, the decoder is unable to commence playback.

The second part is MDAT, which represents the actual media data comprising audio/video content without headers, as these details are now stored in the MOOV section.

If your video displays MDAT at the beginning, you will need to allow the MDAT bytes to stream through before accessing the metadata. Essentially, the entire file must be fully downloaded for playback to commence.

Resolution:
Employ a tool to relocate your MOOV atom to the file's forefront. You can experiment with command-line tools like FFmpeg or MP4Box, or opt for an application like Handbrake.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a decorative ribbon in three.js for your gift presentation

How can I create a ribbon for a gift box using three.js, similar to the example shown here: Is it possible to create the ribbon with just one piece or do I need multiple pieces to achieve the desired look? Thank you :) ...

Transforming JSON data into an interactive table with HTML

After spending four days searching for a solution, I am still unable to figure out my problem. My goal is to convert a JSON string data retrieved from a URL into a dynamic table using JQuery and JavaScript. To achieve this, I need to load the JSON string ...

Unexpected Behavior Arises from Axios Get API Request

Below is a functional example in my CodePen showing what should be happening. Everything is working as intended with hard coded data. CodePen: https://codepen.io/anon/pen/XxNORW?editors=0001 Hard coded data: info:[ { "id": 1, "title": "Title one ...

Encountering a problem with utilizing the equalTo() method in Firebase Realtime Database in a React

I'm having trouble randomizing and querying a specific node in my database based on the ShopNo When I use equalTo, I can't seem to retrieve the desired node. Instead, I'm only getting a randomized value based on the total number of Shop ent ...

Relocating sprite graphic to designated location

I am currently immersed in creating a captivating fish animation. My fish sprite is dynamically moving around the canvas, adding a sense of life to the scene. However, my next goal is to introduce food items for the fishes to feast on within the canvas. Un ...

What steps can I take to stop the mui TreeView component from intercepting events intended for another component?

Within my application, I have implemented a TreeView component that is displayed alongside an Options component containing two buttons. return ( <div style={{ display: "flex", flexDirection: "column", height: ...

Enhance your Vue.js 3 tables with sorting, filtering, and pagination capabilities using this custom component

After extensive research, I am still unable to find an updated Vue.js 3 component that offers sorting, filtering, and pagination for tables without the need for additional requests to a backend API. The options I have come across either seem outdated, are ...

Netlify is failing to recognize redirect attempts for a Next.js application

After successfully converting a react site to utilize next.js for improved SEO, the only hiccup I encountered was with rendering index.js. To work around this, I relocated all the code from index to url.com/home and set up a redirect from url.com to url.co ...

NodeJS Express throwing error as HTML on Angular frontend

I am currently facing an issue with my nodejs server that uses the next() function to catch errors. The problem is that the thrown error is being returned to the frontend in HTML format instead of JSON. I need help in changing it to JSON. Here is a snippe ...

Change glb files to draco glb in the frontend

Can we encode and transform glb files into draco glb format using only frontend technologies (client side)? ...

Error Message: REST Framework returns a 404 status code when attempting to

I created an endpoint with @api_view in my Django project, but for some reason the rest_framework does not seem to be registering it properly. As a result, whenever I try to send a request to api/main-filter/, I receive a 404 error. Why is this happening? ...

"Efficiently handle multiple instances of the same name using AJAX, JavaScript

I have a PHP-generated multiple form with HTML inputs containing IDs and NAMEs. I am struggling to capture all this information. When I click the "Done" button, everything is marked as completed due to the button names. <?$command = "SELECT * FROM exam ...

The Jquery css on my website is not functioning properly

After creating a website feature where clicking on a specific div triggers an onclick event processing a chat_generate function, I encountered some issues. This funciton should insert HTML for the chat into a designated .open_div based on the id generated ...

A chart using JavaScript that displays text values instead of a traditional website

I am a student with no background in programming, however, for my project I need to create a chart based on values from a txt file to display sensor data. I came across a chart that retrieves its values from a website, but I would like to modify it so it c ...

Activate the opening of a .docx file in a new tab by utilizing an anchor tag

I have attached a docx file containing the terms and conditions for our project. Now, I would like to open it in a new tab. By clicking this link, you can view the <a title="click to view terms and conditions" style="color:blue;" target="_blank" href ...

Creating a bold portion of a string

My task involves dynamically creating <p> elements within a div based on the contents of my codeArray, which can vary in size each time. Instead of hard-coding these elements, I have devised the following method: for(i=1;i<codeArray.length;i++) ...

How can I fetch data from a ManyToMany jointable using Typeorm?

Is there a way to retrieve the categories associated with posts when fetching user data? Data Models: @Entity() export class Category extends BaseEntity { @PrimaryGeneratedColumn() id: string; @Column() name: string; @Column() description: s ...

Here's a guide on effectively testing the PasswordChangeForm in Django

I'm in the process of creating a test for my password change view using the PasswordChangeForm. Here is the test script: def test_modify_password(self): self.client.force_login(self.user) self.user.set_password(self.password) new_password ...

Navigating through various versions of admin-on-rest can be perplexing

This question is likely directed towards maintainers. Currently, I am using the stable version of admin-on-rest (https://www.npmjs.com/package/admin-on-rest) which is at 1.3.4. It seems that the main project repository is only receiving bug fixes, while ...

Utilizing object UUID keys to associate products in VueJS

Hey there, I've gone ahead and created an object with UUIDs but now I'm looking for a way to link these UUIDs to specific items. It seems like there needs to be some separation between the UUID and the rest of the object, however, my main issue i ...