Convert aac / webm to mp3 - FFMpeg [ionic]

I’ve implemented capacitor voice recorder, so it gives me on android and ios m4a. in web it gives me webm format audio

i want to convert it to mp3 and send to backend to then kick to whisper from open ai

i tough FFMpeg would be a good idea but i dont understand if its compatible with capacitor. Has anyone implement it? Examples?

You want to format it in the frontend? J would suggest to convert in the backend if needed :thinking:

idc, I want it to work :pensive:

do you know how I could do it in backend with laravel?

No idea, but imo tasks like this are better in the backend :smiley:

I use FFmpeg with Laravel. What specific questions do you have?

I understand that I have to install the ffmpeg program globally on the computer/server

then you should use a library to “link” them. is that so?

I tried (pbmedia/laravel-ffmpeg - Packagist) but I couldn’t do it. Do you use another library? which?

Do you have a tutorial to give me please?

I don’t use any package. I just call it directly using exec. This was created prior to the release of Laravel’s Process facade and before I knew about the File facade (a simple wrapper around PHP’s file functions that can be used outside of your Storage disks).

<?php

namespace App;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class FfmpegHelper
{
    public static function normalizeVolume(string $file, bool $overwrite = true): void
    {
        $timestamp = time();
        $pathInfo = pathinfo($file);
        $newFile = "{$pathInfo['dirname']}/{$pathInfo['filename']}.{$timestamp}.mp3";
        $results = [];

        exec("ffmpeg -i $file -filter:a loudnorm=I=-16:dual_mono=true:TP=-1.5 -b:a 48K -ar 24000 $newFile 2>&1", $results);

        self::validateSuccess($results);

        if ($overwrite) {
            unlink($file);
            rename($newFile, $file);
        }
    }

    public static function concatAudio(Collection $filesToCombine, string $newFile): void
    {
        $allFiles = $filesToCombine->join('|');
        $results = [];

        exec("ffmpeg -i 'concat:{$allFiles}' -acodec copy {$newFile} 2>&1", $results);

        self::validateSuccess($results);
    }

    protected static function validateSuccess(array $results)
    {
        if (! Str::contains(strtolower(end($results)), 'audio:')) {
            throw new \Exception('ffmpeg exception: ' . implode('|', $results));
        }
    }
}

i get it now. you have to get installed on your server ffmpeg program

you say that Laravel Process and File facade can do the convert task instead of using ffmpeg?

laravel process its available from version 10, I use version 8

i’ll have to use ffmpeg

update: can’t download it because my OS server. is linux centos 6.8 throws error

No, you would have to use FFmpeg no matter what. Laravel Process could replace calling exec directly and File could replace using unlink and rename directly.

Yeah, it might not be available in the CentOS package manager. In that case, just download the executable directly from FFmpeg - Download FFmpeg. You would want a Linux Static build. I do this for one of my applications.

Once extracted, I run the following command:

cp -f ffmpeg/ffmpeg ffmpeg/ffprobe /usr/local/bin

Putting it in /usr/local/bin allows it to be called like any other executable. This assumes that location exists on CentOS too :slight_smile:

@twestrick
im new at this, i have made this

put in that folder static build(tar.xz)
i have extracted locally, and then pass the extracted file (.tar) to my server folder
image

if thats ok,
now, you say that i have run in the same root this command?

cp -f ffmpeg/ffmpeg ffmpeg/ffprobe /usr/local/bin

then move, .tar file in the same root as centOs? i dont know where it its, i should ask my coworker

You need to extract the tar file too. It’s kinda like a double “archive/zip”.

You should see something like this (I’m on Linux, my daily driver :slight_smile:):

image

yes!
image

now, in that directory run this command? or in the parent folder?

cp -f ffmpeg/ffmpeg ffmpeg/ffprobe /usr/local/bin

ffmpeg/ is the folder.

So if you are in the current directory ffmpeg-git-20231229-amd64-static, just run the following:

cp -f ffmpeg ffprobe /usr/local/bin

This assumes like I said before, /usr/local/bin exists on CentOS. I would guess it does as it is still Linux.

image

has happened this to you?

You should probably update the server :sweat_smile: CentOS 6.8 was EOL November 30, 2020 (source).

To get around it, you’ll have to use an older version of FFmpeg - Index of /ffmpeg/old-releases. I would work backwards until you find a version that works.

hehe alright
i’ll check older versions

i do not have to do a “rollback” for the command i had run right?

cp -f ffmpeg ffprobe /usr/local/bin

now I’m going to do the extractions in another folder

thank u very much @twestrick !

No, the -f option for cp (copy) forces an overwrite.

image
image

worked!
i’ve installed 2019 ffmpeg-4.2.1-i686-static.tar.xz 2019-09-07 11:02 16M

i’ll update when I implement it with laravel

1 Like

big props to you @twestrick , thank u. worked perfectly

$filePathInStorage = storage_path('app/public/chatbot-audios/' . $fileName);

$pathInfo = pathinfo($filePathInStorage);
$fileToConvert = "{$pathInfo['dirname']}/{$pathInfo['filename']}.mp4";
$newFile = "{$pathInfo['dirname']}/{$pathInfo['filename']}.mp3";

exec("ffmpeg -i '$fileToConvert' -threads 12 -acodec libmp3lame -b:a 128k '$newFile'");
unlink($fileToConvert);
1 Like