/
generate.php
executable file
·62 lines (44 loc) · 1.48 KB
/
generate.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
ob_start();
try {
$redir_url = $_SERVER["REDIRECT_SCRIPT_URL"];
echo "Redirect URL: " . $redir_url . "\n";
$dest_file = substr($redir_url, strpos($redir_url, "/cache/") + 1);
$src_file = "../../uploads/" . substr($dest_file, 6, -4);
$ext = substr($dest_file, -3);
echo "Destination file: " . $dest_file . "\n";
echo "Source file: " . $src_file . "\n";
$dir = dirname($dest_file);
echo "Creating directories: " . $dir . "\n";
@mkdir(escapeshellarg($dir), 0775, true);
switch ($ext) {
case "jpg":
$args = "-ss 1 -vframes 1 -r 1";
break;
case "flv":
$args = "-ar 22050";
break;
default:
throw new Exception("Don't know how to generate " . $ext);
}
$cmd = "ffmpeg -i " . escapeshellarg($src_file) . " " . $args . " "
. escapeshellarg($dest_file) . " 2>&1";
echo "Command: " . $cmd . "\n";
exec($cmd, $output, $ret_code);
echo " " . implode("\n ", $output) . "\n";
if ($ret_code != 0) {
@unlink(escapeshellarg($dest_file));
throw new Exception("Return code was " . $ret_code);
}
if (!file_exists($dest_file)) {
throw new Exception("Destination file not generated");
}
ob_end_clean();
header("Location: " . $_SERVER["REDIRECT_SCRIPT_URI"]);
} catch (Exception $e) {
echo "Generation failed: ", $e->getMessage();
header("HTTP/1.1 404 Not Found");
header("Content-Type: text/plain");
ob_end_flush();
}
?>