Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problems with different mimetypes in different browsers (v4) #28

Open
DevRichter opened this issue Feb 2, 2023 · 6 comments
Open

problems with different mimetypes in different browsers (v4) #28

DevRichter opened this issue Feb 2, 2023 · 6 comments

Comments

@DevRichter
Copy link

DevRichter commented Feb 2, 2023

Hey,

i have a problem with the different encoding of the files. As far as i can tell the mime-types are different for each scenario: audio/webm (recorded with chrome, also different depending on which browser you record with.) for browser and audio/aac (with different sigantures) from android or apple devices.

We record the voice messages in different devices or different browsers and need to support as many platforms as possible, we upload them via aws s3 as the pure base64 string, then download them as these strings, create an instance of an Audio HTML object and play them. (current state)

The problem is that these files have for example different durations then the real duration or do not get played at all (only in safari an issue). The encoding of voice messages recorded by the android devices seem to work in every browser (we tested) for some reason.

Do you know of a way to record the voice messages with an encoding in e.g. Chrome that will work in Safari? Is there a way to convert this encoding, or record with a more broad possibilty to choose the encoding we want?

@tchvu3
Copy link
Owner

tchvu3 commented Feb 9, 2023

I've added a part in the readme that discuss these type of issues exactly.
unfortunately, there is no one type that is supported by all major browsers
and phones (at least not yet).
that's why the plugin returns different mime types for each platform.

for now, the best way to deal with this issue is to convert each file to your required format
before you upload it to s3 (you'll need another js library for that),
as it seems that keeping the base64 as-is will not be good enough.
besides, I think it makes sense to add the ability to choose
the mime type when you start recording, I will definitely add it as a parameter soon.

also, I will check again if there is a mime type that is supported by all
major browsers, the chance is slim but maybe over the last year things have changed
since the last time I've checked.

@nkalupahana
Copy link

nkalupahana commented Dec 28, 2023

@tchvu3 What about audio/mpeg (mp3)? It looks like it's supported in all major browsers, if I'm understanding these docs correctly, but it's not included as a possible web mime type.

update: never mind -- if you run MediaRecorder.isTypeSupported("audio/mpeg") or anything similar on Chrome, it always returns false. Seems like it only supports webm, which is crazy, but oh well.

@DevRichter
Copy link
Author

As we are using this plugin in production, we have now implemented a solution where we transform all audio files via ffmpeg to mp3 then save them as base64 strings and then all of our supported platforms can play them. I think @nkalupahana suggestion is the correct approach, if a viable implementation can be found for it.

@Eraldo
Copy link

Eraldo commented Apr 26, 2024

Seems related: #34 :)

@shyamal890
Copy link

@DevRichter once we get the base64 data how are you saving it as a file. I tried to save base64 data with FileSystem API but it seems once its saved with following code, I am not able to transform the file format with ffmpeg.

Here's the code:

let result = await VoiceRecorder.stopRecording();
let blob = base64StringToBlob(result.value.recordDataBase64, result.value.mimeType);
let data_text = await blob.text();
let extension = mime.getExtension(result.value.mimeType);
let write_result = await Filesystem.writeFile({
            path: `temp.${extension}`,
            data: data_text,
            directory: Directory.External,
            encoding: Encoding.UTF8,
        });
        let uri = write_result.uri;

ffmpeg.exec(`-i ${uri} -vn -c:a copy temp.mp3`,(response)=>{
    console.log(response);
},(error)=>{
    console.log(error);
});

ffmpeg just errors out. Most probably because the file is not saved in a format that ffmpeg can process any suggestions would be appreciated.

@DevRichter
Copy link
Author

DevRichter commented Dec 10, 2024

@shyamal890 sorry for the super late answer. for anyone thats still wondering, we are using @ffmpeg/ffmpeg

and then just

const convertAudioToMp3 = async (voiceMessageFile: Uint8Array): Promise<Uint8Array | undefined> => {
    const ffmpeg = createFFmpeg({
        log: true,
    });
    const uuidOriginal = `${randomUUID()}${FILE_SUFFIX}`;
    const uuidConverted = `${randomUUID()}${FILE_SUFFIX}`;
    try {
        await ffmpeg.load();
        ffmpeg.FS('writeFile', uuidOriginal, voiceMessageFile);
        await ffmpeg.run('-i', uuidOriginal, uuidConverted);

        return ffmpeg.FS('readFile', uuidConverted);
    } catch (e) {
        console.error(errors.ERROR_WHILE_CONVERTING_TO_MP3, e);
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants