Created
July 9, 2024 16:16
-
-
Save acrossoffwest/a1895f97867474083640493143aa4bfc to your computer and use it in GitHub Desktop.
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 | |
namespace App\Console\Commands; | |
use App\Models\Burword; | |
use App\Models\BurWordSpeech; | |
use App\Services\WordsElasticSearch; | |
use Carbon\Carbon; | |
use Illuminate\Console\Command; | |
class LoadBurWordSpeechs extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'bur-words:speech:load'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = ''; | |
/** | |
* Execute the console command. | |
* | |
* @return int | |
*/ | |
public function handle() | |
{ | |
$q = Burword::query()->where(function ($q) { | |
$q->where('name', 'NOT LIKE', '%һ%'); | |
$q->where('name', 'NOT LIKE', '%h%'); | |
})->whereDoesntHave('speechs'); | |
$offset = 0; | |
$this->output->progressStart($q->count()); | |
/** @var Burword $word */ | |
while ($word = $q->offset($offset)->first()) { | |
if (empty(trim($word->name))) { | |
$this->output->progressAdvance(); | |
$offset++; | |
continue; | |
} | |
$filepath = '/speechs/'.$word->id.'.mp3'; | |
if (\Storage::disk('s3')->exists($filepath)) { | |
BurWordSpeech::query()->create([ | |
'burword_id' => $word->id, | |
'filepath' => $filepath, | |
]); | |
$this->output->progressAdvance(); | |
$offset++; | |
continue; | |
} | |
$this->info($word->name.': downloaded'); | |
$speechData = $this->generateSpeech($word->name); | |
if ($speechData['status'] !== 'success') { | |
$offset++; | |
continue; | |
} | |
$base64audioData = $speechData['audioData']; | |
\Storage::disk('s3')->put($filepath, base64_decode($base64audioData)); | |
BurWordSpeech::query()->create([ | |
'burword_id' => $word->id, | |
'filepath' => $filepath, | |
]); | |
$this->output->progressAdvance(); | |
$offset++; | |
} | |
$this->output->progressFinish(); | |
} | |
private function generateSpeech(string $name): array | |
{ | |
return \Http::withHeaders([ | |
"apikey" => config('services.ttsfree.apikey'), | |
]) | |
->asJson() | |
->post('https://ttsfree.com/api/v1/tts', [ | |
"text" => $name, | |
"voiceService" => 'servicebin', | |
"voiceID" => 'mn-MN', // <-- here target language | |
"voiceSpeed" => '-24' | |
])->json(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment