-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathvoice.php
62 lines (58 loc) · 1.76 KB
/
voice.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
<?php
function exceptions_error_handler($severity, $message, $filename, $lineno) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
set_error_handler('exceptions_error_handler');
try {
include 'mp.php';
if(!defined('CONVERT_VOICE_MESSAGES') || !CONVERT_VOICE_MESSAGES) {
echo 'Voice messages converting disabled';
die;
}
$user = MP::getUser();
if(!$user) {
http_response_code(401);
die;
}
$MP = MP::getMadelineAPI($user);
if(!isset($_GET['c']) || !isset($_GET['m'])) die;
$cid = $_GET['c'];
$mid = $_GET['m'];
if(!is_numeric($cid) || !is_numeric($mid)) die;
if(MP::isChannel($cid)) {
$msg = $MP->channels->getMessages(['channel' => $cid, 'id' => [$mid]]);
} else {
$msg = $MP->messages->getMessages(['peer' => $cid, 'id' => [$mid]]);
}
if($msg && isset($msg['messages']) && isset($msg['messages'][0])) {
$msg = $msg['messages'][0];
}
$di = $MP->getDownloadInfo($msg['media']);
if(!file_exists(VOICE_TMP_DIR)) mkdir(VOICE_TMP_DIR);
// automatically delete converted voices
try {
$scan = scandir(VOICE_TMP_DIR);
foreach($scan as $n) {
if(strpos($n, '.mp3') === false) continue;
if(date('d.m.y', filemtime(VOICE_TMP_DIR.$n)) == date('d.m.y', time())) {
continue;
}
unlink(VOICE_TMP_DIR.$n);
}
} catch (Exception) {}
$inpath = VOICE_TMP_DIR.\hash('crc32',$user).$cid.'_'.$mid;
$outpath = $inpath.'.mp3';
if(!file_exists($outpath)) {
$MP->downloadToFile($di, $inpath);
$res = shell_exec(FFMPEG_DIR.'ffmpeg -i "'.$inpath.'" -b:a 64k -ac 1 -y -acodec mp3 "'.$outpath.'"'.(WINDOWS?'':' 2>&1')) ?? '';
unlink($inpath);
if(strpos($res, 'failed') !== false) {
echo 'Conversion failed';
die;
}
}
header('Content-Type: audio/mpeg');
echo file_get_contents($outpath);
} catch (Exception $e) {
echo $e;
}