Last active
July 26, 2023 02:09
-
-
Save jeankassio/01f6ec22d2ae15af2fb96fbf3ce3f990 to your computer and use it in GitHub Desktop.
Custom libopus PHP-FFMPEG to use in Baileys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function ConvertAudio($b64){ | |
try{ | |
if(!$this->CheckBase64($b64)){ | |
return false; | |
}elseif(!$this->CheckSizeBase64($b64, 500, false)){ | |
return false; | |
} | |
$audioPath = tempnam(sys_get_temp_dir(), 'audio') . ".ogg"; | |
file_put_contents($audioPath, base64_decode(preg_replace('#^data:[^;]+;base64,#', '', $b64, 1))); | |
$ffmpeg = FFMpeg::create(['timeout' => 3600]); | |
$ogg = tempnam(sys_get_temp_dir(), 'audio_n') . ".ogg"; | |
$audio = $ffmpeg->openAdvanced([$audioPath]); | |
$format = new CustomOGG(); | |
$audio->setAdditionalParameters( | |
array( | |
'-acodec', 'libopus', | |
'-b', '19.1k', | |
'-ac', '1', | |
'-r', '16k', | |
'-f', 'ipod' | |
) | |
); | |
$audio | |
->map(array('0:a'), $format, $ogg) | |
->save(); | |
$mp4Data = file_get_contents($ogg); | |
$mp4Base64 = base64_encode($mp4Data); | |
unlink($audioPath); | |
unlink($ogg); | |
return $mp4Base64; | |
}catch(\Exception $e){ | |
return false; | |
} | |
} | |
class CustomOGG extends DefaultAudio{ | |
public function __construct(){ | |
$this->audioCodec = 'libopus'; | |
} | |
public function getAvailableAudioCodecs(){ | |
return ['opus']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment