-
Notifications
You must be signed in to change notification settings - Fork 22
/
RoboFile.php
322 lines (254 loc) · 9.34 KB
/
RoboFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/**
* This is project's console commands configuration for Robo task runner.
*
* @see http://robo.li/
*/
class RoboFile extends \Robo\Tasks
{
// define public methods as commands
/**
* 将文本文件转化为mp3
*/
public function convert( $from = null , $to = null , $type = null )
{
// 准备key
@include 'account.php' ;
if( !isset( $GLOBALS['baidu_akey'] ) )
{
$akey = $this->ask("请输入百度语音合成服务的appkey");
$skey = $this->ask("请输入百度语音合成服务的appsecret");
}
else
{
$akey = $GLOBALS['baidu_akey'];
$skey = $GLOBALS['baidu_skey'];
}
$i = 0;
// 每次发送 400 个字符
$max = 400;
$len = 0;
$ship = [];
$audio_count = 1;
$last_file = 'last.json';
if( file_exists( $last_file ) )
{
$info = json_decode( file_get_contents( $last_file ) , 1 );
$this->save = $info['save'];
$content_lines = $info['content_lines'];
if( isset( $info['voice_type'] ) )
$this->voice_type = $info['voice_type'];
}
else
{
if( $from === null )
{
// 获取txt文件路径
$path = $this->askDefault("请输入要转换的txt文件(仅支持UTF8格式)","/Users/Easy/Desktop/money.txt");
}else
{
$path = $from;
}
if( !file_exists( $path ) )
{
$this->say("该文件不存在");
return false;
}
if( $to === null )
$this->save = $this->askDefault("请输入生成mp3文件的地址",'out.mp3');
else
$this->save = $to;
if( $type === null )
$this->voice_type = $this->askDefault("请输入生成语音的风格,3-情感男声;4-情感女生",'3');
else
$this->voice_type = $type;
$content_lines = file( $path );
$EC = mb_detect_encoding( join( "\r\n" , $content_lines ) , "UTF8, GB2312, GBK , CP936");
if( $EC != "UTF8" )
{
foreach( $content_lines as $key => $value )
{
$content_lines[$key] = mb_convert_encoding( $value , "UTF8" , $EC );
}
}
$new_lines = [];
foreach( $content_lines as $key => $value )
{
// 如果单行文字超过了最大长度
if( mb_strlen( $value , 'UTF8' ) > $max )
{
// 分拆成几句
$subs = mb_str_split( $value , $max , 'UTF8' );
foreach( $subs as $item )
{
array_push( $new_lines , $item );
}
}
else
{
$new_lines[] = $value;
}
//$content_lines[$key] = mb_convert_encoding( $value , "UTF8" , $EC );
}
$content_lines = $new_lines;
}
// 读取全部文件内容
// 直接按行分割
$this->say("读取文件成功,共".count($content_lines)."行");
// show( $content_lines , 100 );
while( $len <= $max && count( $content_lines ) > 0 )
{
$snap_lines = $content_lines;
// 从最上边取出
$now_line = array_shift( $content_lines );
$do_convert = false;
if( $len + mb_strlen( $now_line , 'UTF8' ) > $max )
{
array_unshift( $content_lines , $now_line );
// 调用音频转换函数
$do_convert = true;
}
else
{
array_push( $ship , $now_line );
$len += mb_strlen( $now_line , 'UTF8' );
if( count( $content_lines ) == 0 ) $do_convert = true;
}
if( $do_convert )
{
if( $this->txt_to_audio( $audio_count++, $akey , $skey , $ship ) )
{
if( count( $content_lines ) === 0 )
{
$this->say("转化完成 🥇");
@unlink( 'last.json' );
exit;
}
$this->say("待转化段数". count( $content_lines ) );
// 保存当前工作数据和目标文件
$last = [];
$last['save'] = $this->save;
$last['voice_type'] = $this->voice_type;
$last['content_lines'] = $content_lines;
file_put_contents( 'last.json' , json_encode( $last , JSON_UNESCAPED_UNICODE ) );
// 清空
$ship = [];
$len = 0;
}
else
{
// 回滚
// // 保存当前工作数据和目标文件
// $last = [];
// $last['save'] = $this->save;
// $last['content_lines'] = $snap_lines;
// file_put_contents( 'last.json' , json_encode( $last , JSON_UNESCAPED_UNICODE ) );
$this->say("音频转换失败,程序中止");
break;
}
}
$i++;
// if( $i > 500 ) break;
}
if( $len >= $max )
{
$this->say("len = $len , max = $max ,转化结束");
}
if( count( $content_lines ) <= 0 )
{
$this->say( count( $content_lines ) . "<--待处理行数为零 ,转化结束");
}
}
private function txt_to_audio( $count , $akey , $skey , $data_array )
{
// 如果没有token
if( !isset( $GLOBALS['token'] ) )
{
$this->say( "Token 不存在,换取 token" );
$ret = file_get_contents( "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" . urlencode( $akey ) . "&client_secret=" . urlencode( $skey ) );
// print_r( json_decode( $ret , 1 ) );
$bad_token = true;
if( $ret )
{
if( $info = json_decode( $ret , 1 ) )
{
if( isset( $info['access_token'] ) )
{
$this->say("换取Token成功");
$GLOBALS['token'] = $info['access_token'];
$bad_token = false;
}
}
}
if( $bad_token )
{
$this->say("换取Token失败");
return false;
}
}
$text = join( "\r\n" , $data_array );
$this->say("转换..." . mb_substr( trim( $text ) , 0 , 30 , 'UTF8' ));
if( mb_strlen( trim( $text ) , 'UTF8' ) < 1 )
{
$this->say("文字空白,跳过");
return true;
}
// 获取音频下载地址:
$re_try = 0;
get_audio:
$audio = file_get_contents( 'http://tsn.baidu.com/text2audio?lan=zh&ctp=1&cuid=LOCALMAC1022&tok=' . urlencode( $GLOBALS['token'] ) . '&tex=' . urlencode( urlencode( $text ) ) . '&vol=9&per=' . intval( $this->voice_type ) . '&spd=5&pit=5');
$headers = parseHeaders( $http_response_header );
if( $headers['Content-Type'] == 'audio/mp3' )
{
file_put_contents( $this->save , $audio , FILE_APPEND );
$this->say( "此部分已追加写入音频文件 🤠 \r\n" );
return true;
}
else
{
// $this->say( "音频转码失败,转换中止" );
$re_try++;
print_r( $audio );
if( $re_try < 2 ) goto get_audio;
else return false;
}
}
}
function parseHeaders( $headers )
{
$head = array();
foreach( $headers as $k=>$v )
{
$t = explode( ':', $v, 2 );
if( isset( $t[1] ) )
$head[ trim($t[0]) ] = trim( $t[1] );
else
{
$head[] = $v;
if( preg_match( "#HTTP/[0-9\.]+\s+([0-9]+)#",$v, $out ) )
$head['reponse_code'] = intval($out[1]);
}
}
return $head;
}
function mb_str_split($string,$string_length=1,$charset='utf-8')
{
if(mb_strlen($string,$charset)>$string_length || !$string_length)
{
do {
$c = mb_strlen($string,$charset);
$parts[] = mb_substr($string,0,$string_length,$charset);
$string = mb_substr($string,$string_length,$c-$string_length,$charset);
}while(!empty($string));
} else {
$parts = array($string);
}
return $parts;
}
function show( $array , $len )
{
for( $i = 0 ; $i<$len ; $i++ )
{
echo "[$i]>>>>".$array[$i]."<<<<<\r\n";
}
}