fichier.class.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. class fichier
  3. {
  4. static public function add(array $file, array $form)
  5. {
  6. if ($file['error'] === UPLOAD_ERR_OK && !empty($file["tmp_name"])) {
  7. $md5 = md5_file($file["tmp_name"]);
  8. $hash = self::generateHash($file['name']);
  9. $durationInSeconds = fichier::getVideoDuration($file["tmp_name"]);
  10. if (copy($file["tmp_name"], "../video/" . $md5 . ".mp4")) {
  11. 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)");
  12. db::bind(':title', htmlspecialchars($form['videoName']));
  13. db::bind(':titleTmp', htmlspecialchars($file['name']));
  14. db::bind(':type', htmlspecialchars($file['type']));
  15. db::bind(':size', (int)$file['size']);
  16. db::bind(':duration', $durationInSeconds);
  17. db::bind(':md5', $md5);
  18. db::bind(':hash', $hash);
  19. db::bind(':active', (int)$form['videoStatus']);
  20. db::bind(':dateEvent', $form["dateEvent"]);
  21. db::bind(':user', htmlspecialchars($_SESSION["USER"]["name"]));
  22. try {
  23. db::execute();
  24. return $hash;
  25. } catch (Exception $ex) {
  26. error_log($ex->getMessage()); // Log l'erreur pour un examen ultérieur
  27. return FALSE;
  28. }
  29. }
  30. }
  31. return FALSE;
  32. }
  33. static public function update(array $form)
  34. {
  35. db::query(" UPDATE videos SET
  36. title = :title,
  37. dateEvent = :dateEvent,
  38. active = :active
  39. WHERE id = :id");
  40. db::bind(':title', $form["videoName"]);
  41. db::bind(':dateEvent', $form["dateEvent"]);
  42. db::bind(':active', $form["videoStatus"]);
  43. db::bind(':id', $form["id"]);
  44. db::execute();
  45. }
  46. static public function updateTime($_id, $_duration)
  47. {
  48. db::query(" UPDATE videos SET
  49. duration = :duration
  50. WHERE id = :id");
  51. db::bind(':duration', $_duration);
  52. db::bind(':id', $_id);
  53. db::execute();
  54. }
  55. public static function delete(string $_md5 = NULL, string $_folderFiles = "../video/")
  56. {
  57. if (isset($_md5) and $_md5 != NULL and file_exists($_folderFiles . $_md5)) {
  58. if (unlink($_folderFiles . $_md5 . ".mp4")) {
  59. unlink($_folderFiles . $_md5 . ".jpg");
  60. db::query("DELETE FROM videos WHERE md5 = :md5");
  61. db::bind(':md5', $_md5);
  62. try {
  63. db::execute();
  64. return TRUE;
  65. } catch (Exception $ex) {
  66. return FALSE;
  67. }
  68. } else {
  69. return FALSE;
  70. }
  71. } else {
  72. return FALSE;
  73. }
  74. }
  75. static public function getById(float $id)
  76. {
  77. db::query("SELECT "
  78. . "*"
  79. . "FROM videos "
  80. . "WHERE id = :id");
  81. db::bind(':id', $id);
  82. try {
  83. $return = db::single();
  84. return $return;
  85. } catch (Exception $ex) {
  86. return FALSE;
  87. }
  88. }
  89. static public function getByHash(string $hash)
  90. {
  91. db::query("SELECT "
  92. . "*"
  93. . "FROM videos "
  94. . "WHERE hash = :hash");
  95. db::bind(':hash', $hash);
  96. try {
  97. $return = db::single();
  98. return $return;
  99. } catch (Exception $ex) {
  100. return FALSE;
  101. }
  102. }
  103. static public function getAll()
  104. {
  105. db::query("SELECT "
  106. . "*"
  107. . "FROM videos ORDER BY id DESC");
  108. try {
  109. $return = db::resultset();
  110. return $return;
  111. } catch (Exception $ex) {
  112. return FALSE;
  113. }
  114. }
  115. static private function generateHash($string)
  116. {
  117. $timestamp = time();
  118. $combined = $string . $timestamp;
  119. $hashValue = hash('sha256', $combined);
  120. return $hashValue;
  121. }
  122. static public function getVideoDuration($filePath)
  123. {
  124. $command = "ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 " . escapeshellarg($filePath);
  125. $duration = shell_exec($command);
  126. if ($duration) {
  127. return trim($duration);
  128. } else {
  129. return NULL;
  130. }
  131. }
  132. static public function generatePreview($_md5)
  133. {
  134. $videoPreview = "../video/" . $_md5 . ".jpg";
  135. if (file_exists($videoPreview)) {
  136. return ['success' => TRUE, 'file' => 'Le fichier existe déjà'];
  137. }
  138. $time = "00:00:00";
  139. $videoFile = "../video/" . $_md5 . ".mp4";
  140. if (!file_exists($videoFile)) {
  141. return ['success' => FALSE, 'error' => 'Le fichier vidéo n\'existe pas.'];
  142. }
  143. // Générer un nom unique pour le fichier de sortie
  144. $outputFile = tempnam(sys_get_temp_dir(), 'preview_') . '.jpg';
  145. // Commande FFmpeg pour faire une capture d'image
  146. $command = "ffmpeg -i " . escapeshellarg($videoFile)
  147. . " -frames:v 1 -q:v 5 " . escapeshellarg($outputFile);
  148. // Exécuter la commande FFmpeg
  149. exec($command, $output, $return_var);
  150. // Vérifier si la commande a réussi
  151. if ($return_var === 0) {
  152. $destinationFile = "../video/" . $_md5 . ".jpg";
  153. if (copy($outputFile, $destinationFile)) {
  154. unlink($outputFile); // Supprimer le fichier temporaire
  155. return ['success' => TRUE, 'file' => $destinationFile];
  156. } else {
  157. unlink($outputFile); // Supprimer le fichier temporaire en cas d'échec
  158. return ['success' => FALSE, 'error' => 'Erreur lors de la copie du fichier généré.'];
  159. }
  160. } else {
  161. unlink($outputFile); // Supprimer le fichier temporaire en cas d'échec
  162. return ['success' => FALSE, 'error' => 'Erreur lors de l\'exécution de la commande FFmpeg.'];
  163. }
  164. }
  165. static public function cut()
  166. {
  167. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  168. // Validation basique des temps de début et de fin
  169. $startTime = $_POST['startTime'];
  170. $endTime = $_POST['endTime'];
  171. // Vérifier que les temps sont au bon format (HH:MM:SS)
  172. if (!preg_match('/^\d{2}:\d{2}:\d{2}$/', $startTime) || !preg_match('/^\d{2}:\d{2}:\d{2}$/', $endTime)) {
  173. return ['success' => FALSE, 'error' => 'Format incorrect pour startTime ou endTime.'];
  174. }
  175. // Chemin sécurisé du fichier vidéo
  176. $videoFile = "../video/" . basename($_POST['md5']) . ".mp4";
  177. if (!file_exists($videoFile)) {
  178. return ['success' => FALSE, 'error' => 'Le fichier vidéo n\'existe pas.'];
  179. }
  180. // Générer un nom unique pour le fichier de sortie
  181. $outputFile = tempnam(sys_get_temp_dir(), 'cut_') . '.mp4';
  182. // Commande FFmpeg pour découper la vidéo
  183. $command = "ffmpeg -i " . escapeshellarg($videoFile)
  184. . " -ss " . escapeshellarg($startTime)
  185. . " -to " . escapeshellarg($endTime)
  186. . " -force_key_frames 'expr:gte(t,n_forced*1)' -c copy -movflags +faststart "
  187. . escapeshellarg($outputFile);
  188. // Exécuter la commande FFmpeg
  189. exec($command, $output, $return_var);
  190. // Vérifier si la commande a réussi
  191. if ($return_var === 0) {
  192. $destinationFile = "../video/_" . basename($_POST['md5']. ".mp4");
  193. if (copy($outputFile, $destinationFile)) {
  194. unlink($outputFile); // Supprimer le fichier temporaire
  195. return ['success' => TRUE, 'file' => $destinationFile];
  196. } else {
  197. unlink($outputFile); // Supprimer le fichier temporaire en cas d'échec
  198. return ['success' => FALSE, 'error' => 'Erreur lors de la copie du fichier généré.'];
  199. }
  200. } else {
  201. unlink($outputFile); // Supprimer le fichier temporaire en cas d'échec
  202. return ['success' => FALSE, 'error' => 'Erreur lors de l\'exécution de la commande FFmpeg.'];
  203. }
  204. } else {
  205. return ['success' => FALSE, 'error' => 'Requête non autorisée.'];
  206. }
  207. }
  208. }