-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlocallib.php
185 lines (154 loc) · 6.63 KB
/
locallib.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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Version information
*
* @package filter_html5avtomp4
* @copyright 2019 Université de Lausanne
* @author [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
define('FILTER_HTML5AVTOMP4_JOBSPERPASS', 5);
define('FILTER_HTML5AVTOMP4_JOBSTATUS_INITIAL', 0);
define('FILTER_HTML5AVTOMP4_JOBSTATUS_RUNNING', 1);
define('FILTER_HTML5AVTOMP4_JOBSTATUS_DONE', 2);
define('FILTER_HTML5AVTOMP4_JOBSTATUS_FAILED', 3);
define('FILTER_HTML5AVTOMP4_INPUTFILE_PLACEHOLDER', '{%INPUTFILE%}');
define('FILTER_HTML5AVTOMP4_OUTPUTFILE_PLACEHOLDER', '{%OUTPUTFILE%}');
/**
* @param int|null $jobid
* @param bool|null $displaytrace
*
* @throws dml_exception
* @throws file_exception
*/
function filter_html5avtomp4_processjobs(?int $jobid = null, ?bool $displaytrace = true) {
$pathtoffmpeg = get_config('filter_html5avtomp4', 'pathtoffmpeg');
if (empty($pathtoffmpeg) || !is_executable(trim($pathtoffmpeg))) {
// don't bother if ffmpeg is not usable
if ($displaytrace) {
mtrace('ffmpeg not available, aborting');
}
return;
}
global $DB;
if ($jobid > 0) {
$jobs = $DB->get_records('filter_html5avtomp4_jobs', [
'id' => $jobid,
'status' => FILTER_HTML5AVTOMP4_JOBSTATUS_INITIAL
]);
}
else {
// take one job at a time
$jobs = $DB->get_records('filter_html5avtomp4_jobs', ['status' => FILTER_HTML5AVTOMP4_JOBSTATUS_INITIAL],
'id ASC', '*', '0', FILTER_HTML5AVTOMP4_JOBSPERPASS);
if ($displaytrace) {
mtrace('found ' . count($jobs) . ' jobs');
}
}
while ($job = array_shift($jobs)) {
if (!$job) {
if ($displaytrace) {
mtrace('no jobs found');
}
return;
}
$fs = get_file_storage();
$inputfile = $fs->get_file_by_id($job->fileid);
if (!$inputfile) {
$job->status = FILTER_HTML5AVTOMP4_JOBSTATUS_FAILED;
$DB->update_record('filter_html5avtomp4_jobs', $job);
if ($displaytrace) {
mtrace('file ' . $job->fileid . ' not found');
}
return;
}
// to make sure we don't try to run the same job twice
$job->status = FILTER_HTML5AVTOMP4_JOBSTATUS_RUNNING;
$DB->update_record('filter_html5avtomp4_jobs', $job);
$tempdir = make_temp_directory('filter_html5avtomp4');
$tmpinputfilepath = $inputfile->copy_content_to_temp('filter_html5avtomp4');
$tmpoutputfilename = str_replace('.ogg', '.m4a', $inputfile->get_filename());
$tmpoutputfilename = str_replace('.webm', '.mp4', $tmpoutputfilename);
$tmpoutputfilename = str_replace('.ogv', '.mp4', $tmpoutputfilename);
$tmpoutputfilepath = $tempdir . DIRECTORY_SEPARATOR . $tmpoutputfilename;
$type = (strpos($tmpoutputfilename, '.m4a') !== false) ? 'audio' : 'video';
$inputfileplaceholder_preg = preg_quote(FILTER_HTML5AVTOMP4_INPUTFILE_PLACEHOLDER, '/');
$outputfileplaceholder_preg = preg_quote(FILTER_HTML5AVTOMP4_OUTPUTFILE_PLACEHOLDER, '/');
$ffmpegoptions =
preg_replace('/^(.*)' . $inputfileplaceholder_preg . '(.*)' . $outputfileplaceholder_preg . '(.*)$/',
'$1 ' . escapeshellarg($tmpinputfilepath) . ' $2 ' . escapeshellarg($tmpoutputfilepath) . ' $3',
get_config('filter_html5avtomp4', $type . 'ffmpegsettings'));
$command = escapeshellcmd(trim($pathtoffmpeg) . ' ' . $ffmpegoptions);
if ($displaytrace) {
mtrace($command);
}
$output = null;
$return = null;
exec($command, $output, $return);
if ($output) {
mtrace($output);
}
if ($displaytrace) {
mtrace('...returned ' . $return);
}
unlink($tmpinputfilepath); // not needed anymore
if (!file_exists($tmpoutputfilepath) || !is_readable($tmpoutputfilepath)) {
$job->status = FILTER_HTML5AVTOMP4_JOBSTATUS_FAILED;
$DB->update_record('filter_html5avtomp4_jobs', $job);
if ($displaytrace) {
mtrace('output file not found');
}
return;
}
$fs = get_file_storage();
$inputfile_properties = $DB->get_record('files', ['id' => $inputfile->get_id()]);
$outputfile_properties = [
'contextid' => $inputfile_properties->contextid,
'component' => $inputfile_properties->component,
'filearea' => $inputfile_properties->filearea,
'itemid' => $inputfile_properties->itemid,
'filepath' => $inputfile_properties->filepath,
'filename' => $tmpoutputfilename,
'userid' => $inputfile_properties->userid,
'author' => $inputfile_properties->author,
'license' => $inputfile_properties->license,
'timecreated' => time(),
'timemodified' => time()
];
try {
$outputfile = $fs->create_file_from_pathname($outputfile_properties, $tmpoutputfilepath);
}
catch (Exception $exception) {
$job->status = FILTER_HTML5AVTOMP4_JOBSTATUS_FAILED;
$DB->update_record('filter_html5avtomp4_jobs', $job);
if ($displaytrace) {
mtrace('file could not be saved: ' . $exception->getMessage());
}
return;
}
unlink($tmpoutputfilepath); // not needed anymore
if (get_config('filter_html5avtomp4', 'deleteoriginalfiles') == true) {
$inputfile->delete();
}
$job->status = FILTER_HTML5AVTOMP4_JOBSTATUS_DONE;
$DB->update_record('filter_html5avtomp4_jobs', $job);
if ($displaytrace) {
mtrace('created file id ' . $outputfile->get_id());
}
}
}