Skip to content

resolve-url fails for blob URLs #42

@mannux-git

Description

@mannux-git

i need to modify some dash manifest. i'm using the setSource middleware.

videojs.use("application/dash+xml", function () {
  return {
    setSource: function (srcObj: { src: string; type: string }, next: (error: Error | null, source?: { src: string; type: string }) => void) {
      const originalSrc = srcObj.src;
      videojs.xhr(
        {
          uri: originalSrc,
          responseType: "text",
        },
        async function (err, resp) {
          if (err) {
            next(err);
            return;
          }
          // using XML
          const parser = new DOMParser();
          const xmlDoc = parser.parseFromString(resp.body as string, "text/xml");

          // i'm modifying the XML here ....

          // Convert back to string
          const serializer = new XMLSerializer();
          const modifiedManifest = serializer.serializeToString(xmlDoc);
          const blob = new Blob([modifiedManifest], { type: "application/dash+xml" });
          const modifiedSrc = URL.createObjectURL(blob);
          srcObj.src = modifiedSrc;
          next(null, srcObj);
        },
      );
    },
  };
});

the modifiedSrc value looks like this: blob:http://localhost:9926/944f5231-606b-40ad-a9a5-835af75ef184

resolveUrl is called by setupMediaPlaylists during the parsing of the manifest:
playlist.resolvedUri = resolveUrl(main.uri, playlist.uri);

that fails :
TypeError: Failed to construct 'URL': Invalid URL
at resolveUrl (resolve-url.js:22:1)

A simple solution is to remove 'blob:' before creating the new URL():
baseUrl = baseUrl.replace("blob:","");

I'm not a pro, so not sure this is the right fix, but that worked for me

i also tried using the other method to modify the manifest using the application/vnd.videojs.vhs+json type.
That fails as well as the segments are not created when parsing the manifest.

using the same middleware with import { parse } from "mpd-parser";

const parsedManifest = parse(resp.body as string, { manifestUri: srcObj.src });
parsedManifest.playlists.filter ((playList:any)=> "some_code_to_filter") === -1);
srcObj = {
  src: `data:application/vnd.videojs.vhs+json,${JSON.stringify(parsedManifest)}`,
  type: "application/vnd.videojs.vhs+json",
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions