diff --git a/plugin/AI/Objects/Ai_transcribe_responses.php b/plugin/AI/Objects/Ai_transcribe_responses.php index d081ca78c0b9..263495a9546f 100644 --- a/plugin/AI/Objects/Ai_transcribe_responses.php +++ b/plugin/AI/Objects/Ai_transcribe_responses.php @@ -135,18 +135,18 @@ function getAi_responses_id() return intval($this->ai_responses_id); } - static function getVTTPaths($videos_id, $lang='') + static function getVTTPaths($videos_id, $lang = '') { $video = new Video('', '', $videos_id); $filename = $video->getFilename(); - if(empty($filename)){ + if (empty($filename)) { return array('path' => '', 'relative' => '', 'url' => ''); } $videos_dir = getVideosDir(); - if(!empty($lang)){ + if (!empty($lang)) { $lang = ".{$lang}"; } - $relativePathVTT = $filename . DIRECTORY_SEPARATOR . $filename . $lang.'.vtt'; + $relativePathVTT = $filename . DIRECTORY_SEPARATOR . $filename . $lang . '.vtt'; $vtt = $videos_dir . $relativePathVTT; $subtitle = false; if (file_exists($vtt)) { @@ -157,12 +157,43 @@ static function getVTTPaths($videos_id, $lang='') public function save() { - if(empty($this->size_in_bytes)){ + if (empty($this->size_in_bytes)) { $this->size_in_bytes = strlen($this->vtt); - if(empty($this->size_in_bytes)){ + if (empty($this->size_in_bytes)) { $this->size_in_bytes = strlen($this->text); } } return parent::save(); } + + static function saveVTT($vtt, $language, $duration, $text, $total_price, $size_in_bytes, $mp3, $jsonDecoded) + { + _error_log('AI: saveVTT line=' . __LINE__); + $token = $jsonDecoded->token; + $o = new Ai_transcribe_responses(0); + $o->setVtt($vtt); + $o->setLanguage($language); + $o->setDuration($duration); + $o->setText($text); + $o->setTotal_price($total_price); + $o->setSize_in_bytes($size_in_bytes); + $o->setMp3_url($mp3); + $o->setAi_responses_id($token->ai_responses_id); + $jsonDecoded->Ai_transcribe_responses = $o->save(); + + $jsonDecoded->vttsaved = false; + if (!empty($vtt) && !empty($jsonDecoded->Ai_transcribe_responses)) { + _error_log('AI: ' . basename(__FILE__) . ' line=' . __LINE__); + //$jsonDecoded->lines[] = __LINE__; + $paths = Ai_transcribe_responses::getVTTPaths($token->videos_id, $language); + if (!empty($paths['path'])) { + $jsonDecoded->vttsaved = file_put_contents($paths['path'], $vtt); + _error_log("VTTFile saveVTT success videos_id={$token->videos_id}, language={$language} " . json_encode($paths)); + } else { + _error_log("VTTFile Path is empty videos_id={$token->videos_id}, language={$language} " . json_encode($paths)); + } + } + $jsonDecoded->error = false; + return $jsonDecoded; + } } diff --git a/plugin/AI/receiveAsync.json.php b/plugin/AI/receiveAsync.json.php index db719b5fdce5..14a35e47aa17 100644 --- a/plugin/AI/receiveAsync.json.php +++ b/plugin/AI/receiveAsync.json.php @@ -91,30 +91,15 @@ case AI::$typeTranscription: _error_log('AI: ' . basename(__FILE__) . ' line=' . __LINE__); if (!empty($_REQUEST['response'])) { - _error_log('AI: ' . basename(__FILE__) . ' line=' . __LINE__); - $o = new Ai_transcribe_responses(0); - $o->setVtt($_REQUEST['response']['vtt']); - $o->setLanguage($_REQUEST['response']['language']); - $o->setDuration($_REQUEST['response']['duration']); - $o->setText($_REQUEST['response']['text']); - $o->setTotal_price($_REQUEST['response']['total_price']); - $o->setSize_in_bytes($_REQUEST['response']['size_in_bytes']); - $o->setMp3_url($_REQUEST['mp3']); - $o->setAi_responses_id($token->ai_responses_id); - $jsonDecoded->Ai_transcribe_responses = $o->save(); - - $jsonDecoded->vttsaved = false; - if (!empty($_REQUEST['response']['vtt']) && !empty($jsonDecoded->Ai_transcribe_responses)) { - _error_log('AI: ' . basename(__FILE__) . ' line=' . __LINE__); - //$jsonDecoded->lines[] = __LINE__; - $paths = Ai_transcribe_responses::getVTTPaths($token->videos_id, $_REQUEST['response']['language']); - if (!empty($paths['path'])) { - $jsonDecoded->vttsaved = file_put_contents($paths['path'], $_REQUEST['response']['vtt']); - } else { - _error_log("VTTFile Path is empty videos_id={$token->videos_id}, language={$_REQUEST['response']['language']} " . json_encode($paths)); - } - } - $jsonDecoded->error = false; + $jsonDecoded = Ai_transcribe_responses::saveVTT( + $_REQUEST['response']['vtt'], + $_REQUEST['response']['language'], + $_REQUEST['response']['duration'], + $_REQUEST['response']['text'], + $_REQUEST['response']['total_price'], + $_REQUEST['response']['size_in_bytes'], + $_REQUEST['response']['mp3'], + $jsonDecoded); } break; case AI::$typeShorts: @@ -143,16 +128,29 @@ _error_log('Start line=' . __LINE__); require_once __DIR__ . '/../../plugin/VideoHLS/HLSAudioManager.php'; $mp3URL = AI::getMetadataURL() . $_REQUEST['relativeFile']; + $vttURL = AI::getMetadataURL() . $_REQUEST['relativeFileVTT']; $language = 'Default'; foreach (AI::DubbingLANGS as $key => $value) { if ($value['code'] == $_REQUEST['language']) { $language = $value['name']; + break; } } $jsonDecoded->addAudioTrack = HLSAudioManager::addAudioTrack($token->videos_id, $mp3URL, $language); $jsonDecoded->error = empty($jsonDecoded->addAudioTrack); + $vtt = file_get_contents($vttURL); + $jsonDecoded = Ai_transcribe_responses::saveVTT( + $vtt , + $language, + 0, + $vtt, + 0, + strlen($vtt), + $mp3URL, + $jsonDecoded); + _error_log('End line=' . __LINE__.' '.json_encode($jsonDecoded->addAudioTrack)); //$jsonDecoded->lines[] = __LINE__; }