protonemedia / laravel-ffmpeg

This package provides an integration with FFmpeg for Laravel. Laravel's Filesystem handles the storage of the files.
https://protone.media/en/blog/how-to-use-ffmpeg-in-your-laravel-projects
MIT License
1.62k stars 193 forks source link

Is there a method to convert to Mp3 and save it as an UploadedFile type ? Need it for OpenAI Whisper. #484

Open sscotti opened 1 year ago

sscotti commented 1 year ago

I started looking an OpenAI with the LaravelOpenAI package:

use OpenAI\Laravel\Facades\OpenAI;

On my front-end I am creating .webm audio files, and they are getting sent to the backend. I installed the ffmpeg library into my Docker container with PHP and I now want to use FFMpeg to convert that to an .mp3 file to send to Whisper.

That part seems to be working.

Converts the file and saves it. It works because the audio file plays.

        try {
            FFMpeg::open($file)
            ->export()
            ->toDisk('public')
            ->inFormat(new \FFMpeg\Format\Audio\Mp3)
            ->save('song_converted.mp3');
        }
        catch (EncodingException $exception) {
            $command = $exception->getCommand();
            $errorLog = $exception->getErrorOutput();
        }

I think the OpenAI package expects the 'file' to be a UploadedFile type. So I need a way to either convert to an .mp3 just in the script without saving the file, or I need to read the saved file from Disk, but as an UploadedFile.

I would prefer to just bypass having to save it to disk and then read it back again.

I am not all that familiar with Laravel yet, but seem like there should be a way to do that. I'll still have to test it, but should work with and API KEY.

        $response = OpenAI::audio()->transcribe([
        'model' => 'whisper-1',
        'file' => $mp3 ,
        'response_format' => 'verbose_json',
        ]);

dd($file) is: (not sure if I need to change the .wav extension.

This does work if I just read the saved file, using this client: https://github.com/openai-php/client

        $mp3 = Storage::disk('public')->path('song_converted.mp3');
        $response = $client->audio()->transcribe([
        'model' => 'whisper-1',
        'file' =>  fopen($mp3, 'r'),
        'response_format' => 'verbose_json',
        ]);
Illuminate\Http\UploadedFile {#487 // app/Http/MyControllers/OpenAIController.php:33
  -test: false
  -originalName: "audio.wav"
  -mimeType: "audio/webm"
  -error: 0
  #hashName: null
  path: "/tmp"
  filename: "phpdKl5qz"
  basename: "phpdKl5qz"
  pathname: "/tmp/phpdKl5qz"
  extension: ""
  realPath: "/tmp/phpdKl5qz"
  aTime: 2023-04-12 18:44:02
  mTime: 2023-04-12 18:44:02
  cTime: 2023-04-12 18:44:02
  inode: 1710981
  size: 8038
  perms: 0100600
  owner: 33
  group: 33
  type: "file"
  writable: true
  readable: true
  executable: false
  file: true
  dir: false
  link: false
}
HeadStudios commented 1 year ago

I'm in the exact same position - looking for code where I can download from a remote S3 public url (.wav) file and convert to mp3 - I have just tested and confirmed a command like this works:

ffmpeg -i input.wav -codec:a libmp3lame -qscale:a 4 -ar 22050 -ac 1 output.mp3

Just need to be able to convert using the wrapper.