Skip to content

Commit

Permalink
Update AI dubbing
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Neto committed Sep 9, 2024
1 parent 9299134 commit b6db941
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 30 deletions.
43 changes: 37 additions & 6 deletions plugin/AI/Objects/Ai_transcribe_responses.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
}
}
46 changes: 22 additions & 24 deletions plugin/AI/receiveAsync.json.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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__;
}
Expand Down

0 comments on commit b6db941

Please sign in to comment.