-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.php
112 lines (83 loc) · 4.9 KB
/
upload.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
<?php
function delete_files($target)
{
if (is_dir($target)) {
$files = glob($target . '*', GLOB_MARK); //GLOB_MARK adds a slash to directories returned
foreach ($files as $file) {
delete_files($file);
}
if (file_exists($target)) {
rmdir($target);
}
} elseif (is_file($target)) {
unlink($target);
}
}
$servername = "db-mysql-sgp1-28255-do-user-14089511-0.b.db.ondigitalocean.com";
$dbusername = "doadmin";
$dbpassword = 'AVNS_rLM2YTJyAioumr-mBT3';
try {
while (1) {
$conn = new PDO("mysql:host=$servername;dbname=defaultdb;port=25060", $dbusername, $dbpassword);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM streamings where status = 'up' LIMIT 1";
$query = $conn->prepare($sql);
$query->execute();
$result = $query->fetch();
if ($result != false && $result['id'] != null) {
$conn = new PDO("mysql:host=$servername;dbname=defaultdb;port=25060", $dbusername, $dbpassword);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$update = "UPDATE streamings SET status = 'process' WHERE id = " . $result['id'];
$conn->exec($update);
$savefile_name = preg_replace('/\\.[^.\\s]{3,4}$/', '', $result['name']);
$savefile_name = str_replace(" ", "_", $savefile_name);
$save_path_640_360 = "/var/www/stream/uploads/" . $savefile_name . "/640-360/" . $savefile_name . ".m3u8";
$save_path_854_480 = "/var/www/stream/uploads/" . $savefile_name . "/854-480/" . $savefile_name . ".m3u8";
$save_path_1280_720 = "/var/www/stream/uploads/" . $savefile_name . "/1280-720/" . $savefile_name . ".m3u8";
$directory_640_360 = "/var/www/stream/uploads/" . $savefile_name . "/640-360";
$directory_854_480 = "/var/www/stream/uploads/" . $savefile_name . "/854-480";
$directory_1280_720 = "/var/www/stream/uploads/" . $savefile_name . "/1280-720";
exec('sudo mkdir -p ' . $directory_640_360);
exec('sudo mkdir -p ' . $directory_854_480);
exec('sudo mkdir -p ' . $directory_1280_720);
$localpath = $result["path"];
$conn = new PDO("mysql:host=$servername;dbname=defaultdb;port=25060", $dbusername, $dbpassword);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$update = "UPDATE streamings SET status = 'transcoding' WHERE id = " . $result['id'];
$conn->exec($update);
exec("ffmpeg -i " . $localpath . " -profile:v baseline -r 30 -level 3.0 -s 640x360 -start_number 0 -hls_time 10 -hls_list_size 0 -vcodec h264 -acodec aac -b:v 400k -b:a 64k -f hls " . $save_path_640_360 . " -profile:v baseline -r 30 -level 3.0 -s 854x480 -start_number 0 -hls_time 4 -hls_list_size 0 -vcodec h264 -acodec aac -b:v 600k -b:a 96k -f hls " . $save_path_854_480 . " -profile:v baseline -r 30 -level 3.0 -s 1280x720 -start_number 0 -hls_time 4 -hls_list_size 0 -vcodec h264 -acodec aac -b:v 2000k -b:a 128k -f hls " . $save_path_1280_720);
$content = "#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:BANDWIDTH=800000,RESOLUTION=640x360
./640-360/" . $savefile_name . ".m3u8
#EXT-X-STREAM-INF:BANDWIDTH=1400000,RESOLUTION=854x480
./854-480/" . $savefile_name . ".m3u8
#EXT-X-STREAM-INF:BANDWIDTH=2800000,RESOLUTION=1280x720
./1280-720/" . $savefile_name . ".m3u8";
$fp = fopen("/var/www/stream/uploads/" . $savefile_name . "/playlist.m3u8", "w");
fwrite($fp, $content);
fclose($fp);
exec('ncftpput -R -v -u "postvod" -p "3de83dbf-ef8f-4a90-a3862c083a6b-309d-4918" "sg.storage.bunnycdn.com" uploads /var/www/stream/uploads/' . $savefile_name);
$conn = new PDO("mysql:host=$servername;dbname=defaultdb;port=25060", $dbusername, $dbpassword);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$update = "UPDATE streamings SET status = 'yes' WHERE id = " . $result['id'];
$conn->exec($update);
$stream_url = "https://mypostvod.b-cdn.net/uploads/" . $savefile_name . "/playlist.m3u8";
$url = curl_init("http://165.232.171.9/api/posts/video/approve");
curl_setopt($url, CURLOPT_POST, true);
curl_setopt($url, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($url, CURLOPT_POSTFIELDS, json_encode(["post_id" => $result["id"], "url" => $stream_url]));
$result = curl_exec($url);
$code = curl_getinfo($url, CURLINFO_HTTP_CODE);
curl_close($url);
delete_files("/var/www/stream/uploads/" . $savefile_name);
echo "success";
return;
} else {
sleep(10);
}
}
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}