Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FantomX1-github/e934b3104d617cbb66f5da9752759a3c to your computer and use it in GitHub Desktop.
Save FantomX1-github/e934b3104d617cbb66f5da9752759a3c to your computer and use it in GitHub Desktop.
bilingual subtitles generator - two languages subtitles srt from google translate
<?php
/**
* Created by PhpStorm.
* User: fantomx1
* Date: 22.9.2018
* Time: 21:31
*/
require 'vendor/autoload.php';
use Stichoza\GoogleTranslate\TranslateClient;
$tr = new TranslateClient('en', 'sk');
set_time_limit(3600*3);
$contents = file("theSting.srt");
$elements = [];
$words = [];
$text = [];
$majorKey = 1;
foreach ($contents as $rowKey => $row) {
if ($rowKey % 500 == 0) {
$majorKey++;
}
if (is_numeric($row[0]) OR !$row) {
$text[$majorKey] = $text[$majorKey] . '^';
$rowKeys[$rowKey] = $row . '';
continue;
}
$row = str_replace(' ', '|', $row);
$text[$majorKey] = $text[$majorKey] . '' . $row . '^';;
}
$fullText = '';
$fullTextOrigi = '';
foreach($text as &$textItem) {
$fullText .=$tr->translate($textItem);
//$fullText .=$textItem;
$fullTextOrigi .=$textItem;
}
$texta = explode('^', $fullText);
$textaOrigi = explode('^', $fullTextOrigi);
//$pom = $texta;
//$pom = $rowKeys+$texta;
foreach ($textaOrigi as $key=>&$textaItem) {
$text = $texta[$key] ?? 0;
$textaItem .= "".$text;
}
$pom = $rowKeys+$textaOrigi;
ksort($pom);
//var_dump($pom); die();
array_walk($pom, function(&$item){
$item = trim($item);
});
file_put_contents("oky.srt",
implode("\n", $pom)
//var_export($texta, true)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment