Created
December 17, 2017 08:35
-
-
Save lav45/3163ee3cb7f5e683d7a1d7aa61ed1d1f to your computer and use it in GitHub Desktop.
Озвучка текста с помощью API yandex speechkit
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
#!/usr/bin/env php | |
<?php | |
/** | |
* Using | |
* ~$ php speechkit.php book.txt | |
* | |
* @see https://tech.yandex.ru/speechkit/cloud/doc/guide/concepts/tts-http-request-docpage/ | |
*/ | |
$apiUrl = 'https://tts.voicetech.yandex.net/generate'; | |
$options = [ | |
'key' => '1e58cabf-fa4a-45df-b461-174f331d44ff', | |
'speaker' => 'oksana', | |
'format' => 'mp3', | |
'lang' => 'ru-RU', | |
'speed' => 1.2 | |
]; | |
$max_text_size = 2000; | |
if (empty($argv[1])) { | |
echo "Первым параметром должен быть путь к txt файлу\n"; | |
exit(1); | |
} | |
$book = $argv[1]; | |
$path_parts = pathinfo($book); | |
$out_file_name = $path_parts['dirname'] . '/' . $path_parts['filename'] . '.' . $options['format']; | |
$out_handler = fopen($out_file_name, 'a+'); | |
fseek($out_handler, -1, SEEK_END); | |
$handle = fopen($book, 'r'); | |
$i = 0; | |
$text = null; | |
while (($buffer = fgets($handle)) !== false) { | |
$i++; | |
$buffer = trim($buffer); | |
if (empty($buffer)) { | |
continue; | |
} | |
if (preg_match('~[\d\w]+~u', $buffer) === 0) { | |
continue; | |
} | |
if ((strlen($text) + strlen($buffer)) < $max_text_size) { | |
$text .= ' ' . $buffer; | |
continue; | |
} | |
echo "\rline: {$i}\tsize: " . strlen($text) . "\t"; | |
$options['text'] = $text; | |
$text = $buffer; | |
$url = $apiUrl . '?' . http_build_query($options); | |
$data = @file_get_contents($url); | |
if (!$data) { | |
break; | |
} | |
fwrite($out_handler, $data); | |
} | |
if ($text) { | |
$options['text'] = $text; | |
$url = $apiUrl . '?' . http_build_query($options); | |
$data = @file_get_contents($url); | |
fwrite($out_handler, $data); | |
} | |
echo "\n"; | |
fclose($out_handler); | |
fclose($handle); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment