Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command line interface to upload videos #29

Merged
merged 3 commits into from
Jun 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion objects/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,41 @@ function cleanString($text) {
'/ /' => ' ', // nonbreaking space (equiv. to 0x160)
);
return preg_replace(array_keys($utf8), array_values($utf8), $text);
}
}

/**
* @brief return true if running in CLI, false otherwise
*
* @return boolean
*/
function isCommandLineInterface() {
return (php_sapi_name() === 'cli');
}

/**
* @brief show status message as text (CLI) or JSON-encoded array (web)
*
* @param array $statusarray associative array with type/message pairs
* @return string
*/
function status($statusarray) {
if (isCommandLineInterface()) {
foreach ($statusarray as $status => $message) {
echo $status . ":" . $message . "\n";
}
} else {
echo json_encode(array_map(
function($text) { return nl2br($text); }
, $statusarray));
}
}

/**
* @brief show status message and die
*
* @param array $statusarray associative array with type/message pairs
*/
function croak($statusarray) {
status($statusarray);
die;
}
2 changes: 1 addition & 1 deletion objects/video.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function save($updateVideoGroups = false) {
$id = $this->id;
}
if ($updateVideoGroups) {
require_once './userGroups.php';
require_once $global['systemRootPath'] . 'objects/userGroups.php';
// update the user groups
UserGroups::updateVideoGroups($id, $this->videoGroups);
}
Expand Down
273 changes: 193 additions & 80 deletions view/mini-upload-form/upload.php
Original file line number Diff line number Diff line change
@@ -1,80 +1,193 @@
<?php
if(empty($global['systemRootPath'])){
$configFile = '../../videos/configuration.php';
if (!file_exists($configFile)) {
$configFile = '../videos/configuration.php';
}

require_once $configFile;
}
if (!User::canUpload()) {
die('{"status":"error", "msg":"Only logged users can upload"}');
}
//echo "Success: login OK\n";

header('Content-Type: application/json');

// A list of permitted file extensions
$allowed = array('mp4', 'avi', 'mov', 'mkv', 'flv', 'mp3', 'wav', 'm4v', 'webm');

if (isset($_FILES['upl']) && $_FILES['upl']['error'] == 0) {

//echo "Success: \$_FILES OK\n";
$extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);

if (!in_array(strtolower($extension), $allowed)) {
echo '{"status":"error", "msg":"File extension error [' . $_FILES['upl']['name'] . '], we allow only (' . implode(",", $allowed) . ')"}';
exit;
}

//echo "Success: file extension OK\n";

//chack if is an audio
$type = "";
if (strcasecmp($extension, 'mp3') == 0 || strcasecmp($extension, 'wav') == 0) {
$type = 'audio';
}
//var_dump($extension, $type);exit;

require_once $global['systemRootPath'] . 'objects/video.php';

//echo "Starting Get Duration\n";
$duration = Video::getDurationFromFile($_FILES['upl']['tmp_name']);

$path_parts = pathinfo($_FILES['upl']['name']);
$mainName = preg_replace("/[^A-Za-z0-9]/", "", $path_parts['filename']);
$filename = uniqid($mainName . "_", true);

$video = new Video(preg_replace("/_+/", " ", $_FILES['upl']['name']), $filename, @$_FILES['upl']['videoId']);
$video->setDuration($duration);
if ($type == 'audio') {
$video->setType($type);
} else {
$video->setType("video");
}
$video->setStatus('e');
$id = $video->save();
/**
* This is when is using in a non uploaded movie
*/
if (!empty($_FILES['upl']['dontMoveUploadedFile'])) {
if (!rename($_FILES['upl']['tmp_name'], "{$global['systemRootPath']}videos/original_" . $filename)) {
die("Error on rename file(" . $_FILES['upl']['tmp_name'] . ", " . "{$global['systemRootPath']}videos/original_" . $filename . ")");
}
} else if (!move_uploaded_file($_FILES['upl']['tmp_name'], "{$global['systemRootPath']}videos/original_" . $filename)) {
die("Error on move_uploaded_file(" . $_FILES['upl']['tmp_name'] . ", " . "{$global['systemRootPath']}videos/original_" . $filename . ")");
}

$cmd = "/usr/bin/php -f {$global['systemRootPath']}view/mini-upload-form/videoEncoder.php {$filename} {$id} {$type} > /dev/null 2>/dev/null &";
//echo "** executing command {$cmd}\n";
exec($cmd);

//exec("/usr/bin/php -f videoEncoder.php {$_FILES['upl']['tmp_name']} {$filename} 1> {$global['systemRootPath']}videos/{$filename}_progress.txt 2>&1", $output, $return_val);
//var_dump($output, $return_val);

echo '{"status":"success", "msg":"Your video (' . $filename . ') is encoding <br> ' . $cmd . '", "filename":"' . $filename . '", "duration":"' . $duration . '"}';
exit;
}

echo '{"status":"error", "msg":' . json_encode($_FILES) . ', "type":"$_FILES Error"}';
exit;
<?php
if(empty($global['systemRootPath'])){
$configFile = '../../videos/configuration.php';
if (!file_exists($configFile)) {
$configFile = '../videos/configuration.php';
}

require_once $configFile;
}

/*
* Run the conversion script in the background when called through the web interface (the default)
* For the CLI it is better to run it in the foreground to avoid swamping the system with conversion
* processes during batch conversions
*/
$background = "&";

require_once $global['systemRootPath'] . 'objects/functions.php';

if (isCommandLineInterface()) {
$opts = "";
$opts .= "u:"; // user
$opts .= "f:"; // file
$opts .= "d:"; // description
$opts .= "g:"; // comma-separated groups (numerical IDs) for which this video should be accessible - video will be public if left empty
$opts .= "c"; // copy original file (instead of move/rename)
$opts .= "b"; // run conversion script in background
$opts .= "h"; // show help message and exit

$longopts = [];

$options = getopt($opts, $longopts);

if (array_key_exists('h', $options)) {
print <<<'EOT'

Use: php -f upload.php -u <user> -f <file> [-d <description>] [-g "group1[,group2]] [-c] [-h]

-u user valid username
-f file input filename
-d descr description
-g group(s) comma-separated numerical groups for which this item will be visible,
item will be public if this is left out
-c copy original to destination directory (file will be moved if this is left out)
-h this help message


EOT;
exit;
}

/*
* login user without password
*/
$user = new User(false, $options['u'],false);
$user->login(true);

/*
* populate $_FILES to emulate POSTed file
*/
$_FILES['upl']['name'] = basename($options['f']);
$_FILES['upl']['error'] = (is_readable($options['f'])) ? 0 : 1;
$_FILES['upl']['tmp_name'] = $options['f'];
$_FILES['upl']['size'] = filesize($options['f']);

if (!empty($options['d'])) {
$_FILES['upl']['description'] = $options['d'];
}

if (!empty($options['g'])) {
$_FILES['upl']['videoGroups'] = explode(',', $options['g']);
}

if (array_key_exists('c', $options)) {
$_FILES['upl']['copyOriginalFile'] = true;
}

if (!array_key_exists('b', $options)) {
$background="";
}

} else {
header('Content-Type: application/json');
}

if (!User::canUpload()) {
//die('{"status":"error", "msg":"Only logged users can upload"}');
croak(["status" => "error"
, "msg" => "Only logged users can upload"]);
}
//echo "Success: login OK\n";

//if (!isCommandLineInterface()) {
// header('Content-Type: application/json');
//}

// A list of permitted file extensions
$allowed = array('mp4', 'avi', 'mov', 'mkv', 'flv', 'mp3', 'wav', 'm4v', 'webm');

if (isset($_FILES['upl']) && $_FILES['upl']['error'] == 0) {

$updateVideoGroups = false;

//echo "Success: \$_FILES OK\n";
$extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);

if (!in_array(strtolower($extension), $allowed)) {
//echo '{"status":"error", "msg":"File extension error [' . $_FILES['upl']['name'] . '], we allow only (' . implode(",", $allowed) . ')"}';
status(["status" => "error"
, "msg" => "File extension error (" . $_FILES['upl']['name'] . "), we allow only (" . implode(",", $allowed) . ")"]);
exit;
}

//echo "Success: file extension OK\n";

//chack if is an audio
$type = "";
if (strcasecmp($extension, 'mp3') == 0 || strcasecmp($extension, 'wav') == 0) {
$type = 'audio';
}
//var_dump($extension, $type);exit;

require_once $global['systemRootPath'] . 'objects/video.php';

//echo "Starting Get Duration\n";
$duration = Video::getDurationFromFile($_FILES['upl']['tmp_name']);

$path_parts = pathinfo($_FILES['upl']['name']);
$mainName = preg_replace("/[^A-Za-z0-9]/", "", $path_parts['filename']);
$filename = uniqid($mainName . "_", true);

$video = new Video(preg_replace("/_+/", " ", $_FILES['upl']['name']), $filename, @$_FILES['upl']['videoId']);
$video->setDuration($duration);
if ($type == 'audio') {
$video->setType($type);
} else {
$video->setType("video");
}
$video->setStatus('e');

/*
* set visibility for private videos
*/
if (array_key_exists('videoGroups', $_FILES['upl'])) {
$video->setVideoGroups($_FILES['upl']['videoGroups']);
$updateVideoGroups = true;
}

/*
* set description (if given)
*/
if (!empty($_FILES['upl']['description'])) {
$video->setDescription($_FILES['upl']['description']);
}

$id = $video->save($updateVideoGroups);

/**
* Copy, rename or move original file
*
* copy: used from command line when -c option is included
* rename: used with files which were downloaded directly into the videos directory (from other media sites)
* move: default, used with uploaded files
*/
if (array_key_exists('copyOriginalFile', $_FILES['upl'])) {
if (!copy($_FILES['upl']['tmp_name'], "{$global['systemRootPath']}videos/original_" . $filename)) {
die("Error on copy(" . $_FILES['upl']['tmp_name'] . ", " . "{$global['systemRootPath']}videos/original_" . $filename . ")");
}
} else if (array_key_exists('dontMoveUploadedFile', $_FILES['upl'])) {
if (!rename($_FILES['upl']['tmp_name'], "{$global['systemRootPath']}videos/original_" . $filename)) {
die("Error on rename(" . $_FILES['upl']['tmp_name'] . ", " . "{$global['systemRootPath']}videos/original_" . $filename . ")");
}
} else if (!move_uploaded_file($_FILES['upl']['tmp_name'], "{$global['systemRootPath']}videos/original_" . $filename)) {
die("Error on move_uploaded_file(" . $_FILES['upl']['tmp_name'] . ", " . "{$global['systemRootPath']}videos/original_" . $filename . ")");
}

$cmd = "/usr/bin/php -f {$global['systemRootPath']}view/mini-upload-form/videoEncoder.php {$filename} {$id} {$type} > /dev/null 2>/dev/null {$background}";
//echo "** executing command {$cmd}\n";
exec($cmd);

//exec("/usr/bin/php -f videoEncoder.php {$_FILES['upl']['tmp_name']} {$filename} 1> {$global['systemRootPath']}videos/{$filename}_progress.txt 2>&1", $output, $return_val);
//var_dump($output, $return_val);

//echo '{"status":"success", "msg":"Your video (' . $filename . ') is encoding <br> ' . $cmd . '", "filename":"' . $filename . '", "duration":"' . $duration . '"}';
status(["status" => "success"
, "msg" => "Your video ($filename) is encoding \n $cmd"
, "filename" => "$filename", "duration" => "$duration"]);
exit;
}

//echo '{"status":"error", "msg":' . json_encode($_FILES) . ', "type":"$_FILES Error"}';
status(["status" => "error", "msg" => print_r($_FILES,true), "type" => '$_FILES Error']);
exit;
10 changes: 5 additions & 5 deletions view/mini-upload-form/videoEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
eval('$ffmpeg ="' . $value . '";');
$cmd = "rm -f {$global['systemRootPath']}videos/{$filename}.{$key} && rm -f {$global['systemRootPath']}videos/{$filename}_progress_{$key}.txt && {$ffmpeg}";
echo "** executing command {$cmd}\n";
exec($cmd . " 1> {$global['systemRootPath']}videos/{$filename}_progress_{$key}.txt 2>&1", $output, $return_val);
exec($cmd . " < /dev/null 1> {$global['systemRootPath']}videos/{$filename}_progress_{$key}.txt 2>&1", $output, $return_val);
if ($return_val !== 0) {
echo "\\n **AUDIO ERROR**\n", print_r($output, true);
error_log($cmd . "\n" . print_r($output, true));
Expand All @@ -56,7 +56,7 @@
//$ffmpeg = "ffmpeg -i {$pathFileName} -filter_complex \"[0:a]showwaves=s=858x480:mode=line,format=yuv420p[v]\" -map \"[v]\" -map 0:a -c:v libx264 -c:a copy {$destinationFile}";
$cmd = "rm -f $destinationFile && rm -f {$global['systemRootPath']}videos/{$filename}_progress_mp4.txt && {$ffmpeg}";
echo "** executing command {$cmd}\n";
exec($cmd . " 1> {$global['systemRootPath']}videos/{$filename}_progress_mp4.txt 2>&1", $output, $return_val);
exec($cmd . " < /dev/null 1> {$global['systemRootPath']}videos/{$filename}_progress_mp4.txt 2>&1", $output, $return_val);
if ($return_val !== 0) {
echo "\\n **Spectrum ERROR**\n", print_r($output, true);
error_log($cmd . "\n" . print_r($output, true));
Expand All @@ -68,7 +68,7 @@
eval('$ffmpeg ="' . $videoConverter['webm'] . '";');
$cmd = "rm -f $destinationFile && rm -f {$global['systemRootPath']}videos/{$filename}_progress_webm.txt && {$ffmpeg}";
echo "** executing command {$cmd}\n";
exec($cmd . " 1> {$global['systemRootPath']}videos/{$filename}_progress_webm.txt 2>&1", $output, $return_val);
exec($cmd . " < /dev/null 1> {$global['systemRootPath']}videos/{$filename}_progress_webm.txt 2>&1", $output, $return_val);
if ($return_val !== 0) {
echo "\\n **VIDEO ERROR**\n", print_r($output, true);
error_log($cmd . "\n" . print_r($output, true));
Expand Down Expand Up @@ -103,7 +103,7 @@
eval('$ffmpeg ="' . $value . '";');
$cmd = "rm -f {$global['systemRootPath']}videos/{$filename}.{$key} && rm -f {$global['systemRootPath']}videos/{$filename}_progress_{$key}.txt && {$ffmpeg}";
echo "** executing command {$cmd}\n";
exec($cmd . " 1> {$global['systemRootPath']}videos/{$filename}_progress_{$key}.txt 2>&1", $output, $return_val);
exec($cmd . " < /dev/null 1> {$global['systemRootPath']}videos/{$filename}_progress_{$key}.txt 2>&1", $output, $return_val);
if ($return_val !== 0) {
echo "\\n **VIDEO ERROR**\n", print_r($output, true);
error_log($cmd . "\n" . print_r($output, true));
Expand Down Expand Up @@ -136,7 +136,7 @@

$cmd = "rm -f {$global['systemRootPath']}videos/{$filename}.jpg && {$ffmpeg}";
echo "** executing command {$cmd}\n";
exec($cmd . " 2>&1", $output, $return_val);
exec($cmd . " < /dev/null 2>&1", $output, $return_val);
if ($return_val !== 0) {
echo "\\n**IMG ERROR**\n", print_r($output, true);
error_log($cmd . "\n" . print_r($output, true));
Expand Down