-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathapi.php
executable file
·125 lines (105 loc) · 3.8 KB
/
api.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
<?php
require_once "./config.php";
$OS_TYPE = DIRECTORY_SEPARATOR == '\\' ? 'windows' : 'linux';
/**
* 解决 ffmpeg 对 windows 系统的绝对路径“不友好问题”。
* 好像解决的方法有点笨...
*/
if ($OS_TYPE == 'windows') {
define('ROOT' ,'.');
} else {
define('ROOT' ,__DIR__);
}
$type = isset($_POST['type']) ? $_POST['type'] : false;
$data = isset($_POST['data']) ? $_POST['data'] : false;
/**
* 文字内容鉴别
* 必须在配置文件中开启
*/
if (INVALID_TEXT === true) {
// 配置必须不为空
if (UPYUN_KEY != '' && UPYUN_SECRET != '') {
// 导入guzzle
require_once ROOT . '/vendor/autoload.php';
// 导入Upyun
require_once ROOT . '/usr/lib/Upyun.php';
// 实例化
$upyun = new Upyun(UPYUN_KEY ,UPYUN_SECRET);
// 逐句校验
foreach ($data as $text) {
$validResult = $upyun->execute($text);
if ($validResult['code'] != 200) {
exit(json_encode($validResult));
}
}
} else {
exit(json_encode(sprintf('请补全UPYUN API参数 ,配置文件: %s' ,ROOT . '/config.php') ,JSON_UNESCAPED_UNICODE));
}
}
/**
* 鉴别结束
*/
$small = isset($_POST['small']) ? $_POST['small'] : false;
$request_time = time(true);
if ($type && $data && $small) {
$TEMP_ROOT = ROOT . '/templates/' . $type . '/';
$TEMP_ASS = $TEMP_ROOT . 'template.ass';
$CACHE_ASS_PATH = ROOT . '/cache/' . $type . '_' . $request_time . '.ass';
if ($small == 'true' || DEFAULT_CREATE_SMALL_GIF === true) {
$TEMP_VIDEO = $TEMP_ROOT . 'template-small.mp4';
if (! file_exists($TEMP_ROOT . 'template-small.mp4')) {
$TEMP_VIDEO = $TEMP_ROOT . 'template.mp4';
}
} else {
$TEMP_VIDEO = $TEMP_ROOT . 'template.mp4';
}
/**
* 判断根目录是否存在 cache 目录,不存在则创建
*/
if (! file_exists(ROOT . '/cache')) {
if (mkdir(ROOT . '/cache' ,0777) === false) {
$result['code'] = 500;
$result['msg'] = '服务端 `cache` 目录不存在,尝试创建 `cache` 目录,但创建失败,请网站管理员在网站根目录手动创建 `cache` 目录';
exit(json_encode($result));
}
}
if (file_exists($TEMP_ROOT)) {
$ass_file = file_get_contents($TEMP_ASS);
for ($i = 0; $i < count($data); $i++) {
$str_source[$i] = '<?=[' . $i . ']=?>';
}
$change_ass = str_replace($str_source ,$data ,$ass_file);
$create_temporary_ass = fopen($CACHE_ASS_PATH ,"w") or die('{"code":501,"msg":"临时字幕文件创建失败,请网站管理员检查 `cache` 目录是否具有读写权限或用户组设否设置正确!"}');
fwrite($create_temporary_ass ,$change_ass) or die('{"code":502,"msg":"临时字幕文件已创建,但写入失败,请网站管理员检查 `cache` 目录是否具有读写权限或用户组设否设置正确!"}');
fclose($create_temporary_ass);
$out_put_file = ROOT . '/cache/' . $request_time . '.gif';
$command = 'ffmpeg -y -i ' . $TEMP_VIDEO . ' -vf "ass=' . $CACHE_ASS_PATH . '" ' . $out_put_file;
system($command);
unlink($CACHE_ASS_PATH);//删除临时生成的字幕文件
$result['code'] = 200;
$result['type'] = $type;
$result['msg'] = '应该生成成功...';
if (UPLOAD_TO_SOGOU_IMG === true && filesize($out_put_file) < 10000000) {
require_once ROOT . '/usr/lib/functions.php';
$r = upload_to_sogou($out_put_file);
if ($r === false) {
//如果上传失败,则返回本地路径
$result['path'] = './cache/' . $request_time . '.gif';
$result['upload_status'] = 'fail';
} else {
$result['path'] = 'https://' . $r['host'][rand(0 ,4)] . $r['path'];
$result['upload_status'] = 'success';
}
} else {
$result['path'] = './cache/' . $request_time . '.gif';
}
} else {
$result['code'] = 404;
$result['type'] = $type;
$result['msg'] = '该模板文件不存在!';
}
} else {
$result['code'] = 400;
$result['msg'] = '缺少必要参数,请检查!';
}
echo json_encode($result);