2

I'm trying to convert .webm to .mp4 on backend in node in a format that can be played on mobile safari.

The reason for doing this is that we are using getUserMedia() & getDisplayMedia to record the user's camera/screen and webm doesn't play on mobile ios safari.

Following the second part of this answer - https://stackoverflow.com/a/63463669

On frontend -

        mediaStreamConstraints: {
          video: { width: 1280, height: 720, frameRate: { min: 24, ideal: 30, max: 60 } },
          audio: true,
          ...mediaStreamConstraints,
        },
        mediaRecorderOptions: {
          videoBitsPerSecond: 4000000,
          mimeType: 'video/webm;codecs=h264',
        },
      });

And on the backend

async uploadVideo(id: string, readableStream: stream.Readable, options: UploadBlobOptions) {
    if (disableVideoEncoding) {
      return this.uploadBlob(id, readableStream, options);
    }

    const buffer = await readableStreamToBuffer(readableStream);
    const inputData = new Uint8Array(buffer);
    const result = ffmpeg({
      MEMFS: [{ name: 'input', data: inputData }],
      print: () => {},
      printErr: () => {},
      arguments: [
        ...['-i', 'input'],
        // Encode audio with AAC https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Audio_codecs#choosing_an_audio_codec
        ...['-c:a', 'aac'],
        // Copy video
        ...['-c:v', 'copy'],
        // Output
        'output.mp4',
      ],
    });
    const out = result.MEMFS[0];
    const outStream = new Duplex();
    if (out) {
      outStream.push(out.data);
      outStream.push(null);
    } else {
      throw new Error('Could not find output file');
    }

    return this.uploadBlob(id, outStream, {
      ...options,
      mimeType: 'video/mp4',
    });
  }

The output video works in chrome and is .mp4, but still doesn't play in safari. If anyone can solve I'm very grateful!!

This is a video generated with the code above:

https://tendon-user-files-public.s3.eu-west-1.amazonaws.com/tendon-production/bc8cd560-4898-402b-8aa3-041937adf725.mp4

And this is another video i uploaded a while back, which works in safari: https://tendon-user-files-public.s3.eu-west-1.amazonaws.com/tendon-production/bafybeihjwawyuvirf4ymwununm25skxvzedlsd33ztav3rbofekv7z6yzu.mp4

Thanks! enter image description here

alastacat
  • 51
  • 1
  • 6
  • You're using `-c:v copy` to put a VP8 or VP9-encoded video into a `.mp4` container. A `.mp4` file with `h.264` encoded video and `aac` or `mp3` encoded audio would work. – RickN Feb 22 '22 at 14:31
  • ah sorry, i thought i was encoding originally in h264. Added an image of that to the bottom of the question. – alastacat Feb 22 '22 at 17:10

0 Answers0