|
@@ -0,0 +1,240 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+class fichier
|
|
|
|
|
+{
|
|
|
|
|
+ static public function add(array $file, array $form)
|
|
|
|
|
+ {
|
|
|
|
|
+ if ($file['error'] === UPLOAD_ERR_OK && !empty($file["tmp_name"])) {
|
|
|
|
|
+ $md5 = md5_file($file["tmp_name"]);
|
|
|
|
|
+ $hash = self::generateHash($file['name']);
|
|
|
|
|
+ $durationInSeconds = fichier::getVideoDuration($file["tmp_name"]);
|
|
|
|
|
+
|
|
|
|
|
+ if (copy($file["tmp_name"], "../video/" . $md5 . ".mp4")) {
|
|
|
|
|
+ db::query("INSERT INTO videos (title, titleTmp, type, size, duration, md5, hash, active, dateEvent, user) VALUES (:title, :titleTmp, :type, :size, :duration, :md5, :hash, :active, :dateEvent, :user)");
|
|
|
|
|
+ db::bind(':title', htmlspecialchars($form['videoName']));
|
|
|
|
|
+ db::bind(':titleTmp', htmlspecialchars($file['name']));
|
|
|
|
|
+ db::bind(':type', htmlspecialchars($file['type']));
|
|
|
|
|
+ db::bind(':size', (int)$file['size']);
|
|
|
|
|
+ db::bind(':duration', $durationInSeconds);
|
|
|
|
|
+ db::bind(':md5', $md5);
|
|
|
|
|
+ db::bind(':hash', $hash);
|
|
|
|
|
+ db::bind(':active', (int)$form['videoStatus']);
|
|
|
|
|
+ db::bind(':dateEvent', $form["dateEvent"]);
|
|
|
|
|
+ db::bind(':user', htmlspecialchars($_SESSION["USER"]["name"]));
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ db::execute();
|
|
|
|
|
+ return $hash;
|
|
|
|
|
+ } catch (Exception $ex) {
|
|
|
|
|
+ error_log($ex->getMessage()); // Log l'erreur pour un examen ultérieur
|
|
|
|
|
+ return FALSE;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return FALSE;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ static public function update(array $form)
|
|
|
|
|
+ {
|
|
|
|
|
+ db::query(" UPDATE videos SET
|
|
|
|
|
+ title = :title,
|
|
|
|
|
+ dateEvent = :dateEvent,
|
|
|
|
|
+ active = :active
|
|
|
|
|
+ WHERE id = :id");
|
|
|
|
|
+ db::bind(':title', $form["videoName"]);
|
|
|
|
|
+ db::bind(':dateEvent', $form["dateEvent"]);
|
|
|
|
|
+ db::bind(':active', $form["videoStatus"]);
|
|
|
|
|
+ db::bind(':id', $form["id"]);
|
|
|
|
|
+ db::execute();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static public function updateTime($_id, $_duration)
|
|
|
|
|
+ {
|
|
|
|
|
+ db::query(" UPDATE videos SET
|
|
|
|
|
+ duration = :duration
|
|
|
|
|
+ WHERE id = :id");
|
|
|
|
|
+ db::bind(':duration', $_duration);
|
|
|
|
|
+ db::bind(':id', $_id);
|
|
|
|
|
+ db::execute();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function delete(string $_md5 = NULL, string $_folderFiles = "../video/")
|
|
|
|
|
+ {
|
|
|
|
|
+ if (isset($_md5) and $_md5 != NULL and file_exists($_folderFiles . $_md5)) {
|
|
|
|
|
+ if (unlink($_folderFiles . $_md5 . ".mp4")) {
|
|
|
|
|
+ unlink($_folderFiles . $_md5 . ".jpg");
|
|
|
|
|
+ db::query("DELETE FROM videos WHERE md5 = :md5");
|
|
|
|
|
+ db::bind(':md5', $_md5);
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ db::execute();
|
|
|
|
|
+ return TRUE;
|
|
|
|
|
+ } catch (Exception $ex) {
|
|
|
|
|
+ return FALSE;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return FALSE;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return FALSE;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static public function getById(float $id)
|
|
|
|
|
+ {
|
|
|
|
|
+ db::query("SELECT "
|
|
|
|
|
+ . "*"
|
|
|
|
|
+ . "FROM videos "
|
|
|
|
|
+ . "WHERE id = :id");
|
|
|
|
|
+ db::bind(':id', $id);
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ $return = db::single();
|
|
|
|
|
+ return $return;
|
|
|
|
|
+ } catch (Exception $ex) {
|
|
|
|
|
+ return FALSE;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static public function getByHash(string $hash)
|
|
|
|
|
+ {
|
|
|
|
|
+ db::query("SELECT "
|
|
|
|
|
+ . "*"
|
|
|
|
|
+ . "FROM videos "
|
|
|
|
|
+ . "WHERE hash = :hash");
|
|
|
|
|
+ db::bind(':hash', $hash);
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ $return = db::single();
|
|
|
|
|
+ return $return;
|
|
|
|
|
+ } catch (Exception $ex) {
|
|
|
|
|
+ return FALSE;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static public function getAll()
|
|
|
|
|
+ {
|
|
|
|
|
+ db::query("SELECT "
|
|
|
|
|
+ . "*"
|
|
|
|
|
+ . "FROM videos ORDER BY id DESC");
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ $return = db::resultset();
|
|
|
|
|
+ return $return;
|
|
|
|
|
+ } catch (Exception $ex) {
|
|
|
|
|
+ return FALSE;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static private function generateHash($string)
|
|
|
|
|
+ {
|
|
|
|
|
+ $timestamp = time();
|
|
|
|
|
+ $combined = $string . $timestamp;
|
|
|
|
|
+ $hashValue = hash('sha256', $combined);
|
|
|
|
|
+ return $hashValue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static public function getVideoDuration($filePath)
|
|
|
|
|
+ {
|
|
|
|
|
+ $command = "ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 " . escapeshellarg($filePath);
|
|
|
|
|
+ $duration = shell_exec($command);
|
|
|
|
|
+ if ($duration) {
|
|
|
|
|
+ return trim($duration);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static public function generatePreview($_md5)
|
|
|
|
|
+ {
|
|
|
|
|
+ $videoPreview = "../video/" . $_md5 . ".jpg";
|
|
|
|
|
+ if (file_exists($videoPreview)) {
|
|
|
|
|
+ return ['success' => TRUE, 'file' => 'Le fichier existe déjà'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $time = "00:00:00";
|
|
|
|
|
+ $videoFile = "../video/" . $_md5 . ".mp4";
|
|
|
|
|
+
|
|
|
|
|
+ if (!file_exists($videoFile)) {
|
|
|
|
|
+ return ['success' => FALSE, 'error' => 'Le fichier vidéo n\'existe pas.'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Générer un nom unique pour le fichier de sortie
|
|
|
|
|
+ $outputFile = tempnam(sys_get_temp_dir(), 'preview_') . '.jpg';
|
|
|
|
|
+
|
|
|
|
|
+ // Commande FFmpeg pour faire une capture d'image
|
|
|
|
|
+ $command = "ffmpeg -i " . escapeshellarg($videoFile)
|
|
|
|
|
+ . " -frames:v 1 -q:v 5 " . escapeshellarg($outputFile);
|
|
|
|
|
+
|
|
|
|
|
+ // Exécuter la commande FFmpeg
|
|
|
|
|
+ exec($command, $output, $return_var);
|
|
|
|
|
+
|
|
|
|
|
+ // Vérifier si la commande a réussi
|
|
|
|
|
+ if ($return_var === 0) {
|
|
|
|
|
+ $destinationFile = "../video/" . $_md5 . ".jpg";
|
|
|
|
|
+ if (copy($outputFile, $destinationFile)) {
|
|
|
|
|
+ unlink($outputFile); // Supprimer le fichier temporaire
|
|
|
|
|
+ return ['success' => TRUE, 'file' => $destinationFile];
|
|
|
|
|
+ } else {
|
|
|
|
|
+ unlink($outputFile); // Supprimer le fichier temporaire en cas d'échec
|
|
|
|
|
+ return ['success' => FALSE, 'error' => 'Erreur lors de la copie du fichier généré.'];
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ unlink($outputFile); // Supprimer le fichier temporaire en cas d'échec
|
|
|
|
|
+ return ['success' => FALSE, 'error' => 'Erreur lors de l\'exécution de la commande FFmpeg.'];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ static public function cut()
|
|
|
|
|
+ {
|
|
|
|
|
+ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
|
|
|
+ // Validation basique des temps de début et de fin
|
|
|
|
|
+ $startTime = $_POST['startTime'];
|
|
|
|
|
+ $endTime = $_POST['endTime'];
|
|
|
|
|
+
|
|
|
|
|
+ // Vérifier que les temps sont au bon format (HH:MM:SS)
|
|
|
|
|
+ if (!preg_match('/^\d{2}:\d{2}:\d{2}$/', $startTime) || !preg_match('/^\d{2}:\d{2}:\d{2}$/', $endTime)) {
|
|
|
|
|
+ return ['success' => FALSE, 'error' => 'Format incorrect pour startTime ou endTime.'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Chemin sécurisé du fichier vidéo
|
|
|
|
|
+ $videoFile = "../video/" . basename($_POST['md5']) . ".mp4";
|
|
|
|
|
+
|
|
|
|
|
+ if (!file_exists($videoFile)) {
|
|
|
|
|
+ return ['success' => FALSE, 'error' => 'Le fichier vidéo n\'existe pas.'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Générer un nom unique pour le fichier de sortie
|
|
|
|
|
+ $outputFile = tempnam(sys_get_temp_dir(), 'cut_') . '.mp4';
|
|
|
|
|
+
|
|
|
|
|
+ // Commande FFmpeg pour découper la vidéo
|
|
|
|
|
+ $command = "ffmpeg -i " . escapeshellarg($videoFile)
|
|
|
|
|
+ . " -ss " . escapeshellarg($startTime)
|
|
|
|
|
+ . " -to " . escapeshellarg($endTime)
|
|
|
|
|
+ . " -force_key_frames 'expr:gte(t,n_forced*1)' -c copy -movflags +faststart "
|
|
|
|
|
+ . escapeshellarg($outputFile);
|
|
|
|
|
+
|
|
|
|
|
+ // Exécuter la commande FFmpeg
|
|
|
|
|
+ exec($command, $output, $return_var);
|
|
|
|
|
+
|
|
|
|
|
+ // Vérifier si la commande a réussi
|
|
|
|
|
+ if ($return_var === 0) {
|
|
|
|
|
+ $destinationFile = "../video/_" . basename($_POST['md5']. ".mp4");
|
|
|
|
|
+ if (copy($outputFile, $destinationFile)) {
|
|
|
|
|
+ unlink($outputFile); // Supprimer le fichier temporaire
|
|
|
|
|
+ return ['success' => TRUE, 'file' => $destinationFile];
|
|
|
|
|
+ } else {
|
|
|
|
|
+ unlink($outputFile); // Supprimer le fichier temporaire en cas d'échec
|
|
|
|
|
+ return ['success' => FALSE, 'error' => 'Erreur lors de la copie du fichier généré.'];
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ unlink($outputFile); // Supprimer le fichier temporaire en cas d'échec
|
|
|
|
|
+ return ['success' => FALSE, 'error' => 'Erreur lors de l\'exécution de la commande FFmpeg.'];
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return ['success' => FALSE, 'error' => 'Requête non autorisée.'];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|