laravel-ffmpeg icon indicating copy to clipboard operation
laravel-ffmpeg copied to clipboard

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

Open sscotti opened this issue 2 years ago • 1 comments

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
}

sscotti avatar Apr 12 '23 19:04 sscotti