src/Controller/LuckyController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\JsonResponse;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use App\WiziLib\AmazonWraper;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. class LuckyController
  9. {
  10.     /**
  11.      * @Route("/lucky/number")
  12.      */
  13.     public function number(): Response
  14.     {
  15.         $number random_int(0100);
  16.         var_dump(phpinfo());
  17.         die();
  18.         return new Response(
  19.             '<html><body>Lucky number: '.$number.'</body></html>'
  20.         );
  21.     }
  22.     /**
  23.      * @Route("/")
  24.      */
  25.     public function home(): Response
  26.     {
  27.         $number random_int(0100);
  28.         return new Response(
  29.             '<html><body>Lucky number: '.$number.'</body></html>'
  30.         );
  31.     }
  32.     /**
  33.      * form vidéo hls
  34.      *
  35.      * @param Request $request
  36.      * @param  $flat
  37.      * @return JsonResponse
  38.      * @Route("/flat-video-hls/{flat}", defaults={"_format" = "json"})
  39.      * @throws \Exception
  40.      */
  41.     public function postHLSsendAction(Request $request$flat)
  42.     {
  43.         set_time_limit(10000);
  44.         /*
  45.         width: 1920
  46.         height: 1080
  47.         */
  48.         
  49.         $data = ['reponse'=>'OK'];
  50.         $c 0;
  51.         $files $request->files->get('files');
  52.         $width $request->get('width');
  53.         $height $request->get('height');
  54.         $objectType 'Flat';
  55.         if ($request->get('object')) {
  56.             $objectType $request->get('object');
  57.         }
  58.         $n '';
  59.         $tab = [];
  60.         foreach ($files as $f) {
  61.             $c++;
  62.             $vv = [];
  63.             $tab[$c]['name'] = $f->getClientOriginalName();
  64.             $tab[$c]['extension'] = $f->guessExtension();
  65.             // return  new JsonResponse($tab) ;
  66.             /*
  67.              * en curl
  68.             $task  = new VideoTask();
  69.             $task->setType('video');
  70.             $task->setCreatedAt(new \DateTime());
  71.             $task->setExtension($f->guessExtension());
  72.             $task->setFilename(microtime(true));
  73.             $task->setTitle('');
  74.             $task->setObjectName('Flat');
  75.             $task->setObjectId($flat->getId());
  76.             $task->setDone(0);
  77.             $em->persist($task);
  78.             $em->flush();
  79.             */
  80.             $tasktabr $this->CallAPI("POST","https://app.wizi.eu/api/public/flat-video-task-from-graph-server/".$flat."/".$tab[$c]['extension'].'/'.$objectType);
  81.             $tasktab json_decode($tasktabr,true);
  82.             $task$tasktab['videotask'];
  83.             //return  new JsonResponse($task) ;
  84.             if(!is_dir('/home/www/video/public/videotask/'.$task['id'])){
  85.                 mkdir'/home/www/video/public/videotask/'.$task['id'],0777);
  86.                 $videodir '/home/www/video/public/videotask/'.$task['id'].'/';
  87.             }
  88.             $image file_get_contents($f);
  89.             if ($tab[$c]['extension'] == 'qt') {
  90.                 file_put_contents($videodir.$task['id'].'-'.$task['filename'].'.mov'$image);
  91.                 // public/update-video-task/{videotask}/{extension}
  92.                 $dsend = ['title' => $task['id'].'-'.$task['filename'].'.mov'];
  93.                 $vv['title'] = $task['id'].'-'.$task['filename'].'.mov';
  94.                 $vv['id'] = $task['id'];
  95.                 $dsend['extension'] = 'mov';
  96.                 $this->CallAPI("POST","https://app.wizi.eu/api/public/update-video-task/".$task['id']."/".$tab[$c]['extension'],$dsend);
  97.             }
  98.             else
  99.             {
  100.                 file_put_contents($videodir.$task['id'].'-'.$task['filename'].'.'.$task['extension'], $image);
  101.                 $dsend = ['title' => $task['id'].'-'.$task['filename'].'.'.$task['extension']];
  102.                 $dsend['extension'] = $task['extension'];
  103.                 $vv['title'] = $task['id'].'-'.$task['filename'].'.'.$task['extension'];
  104.                 $vv['id'] = $task['id'];
  105.                 $this->CallAPI("POST","https://app.wizi.eu/public/update-video-task/".$task['id']."/".$tab[$c]['extension'],$dsend);
  106.             }
  107.             $this->doVideoTask($vv,$width,$height);
  108.         }
  109.         $data['files'] = $tab ;
  110.         $data['count'] = $c;
  111.         $data['videotask'] = $task;
  112.         return new JsonResponse($data);
  113.         // return $data;
  114.     }
  115.     // Method: POST, PUT, GET etc
  116.     // Data: array("param" => "value") ==> index.php?param=value
  117.     public function CallAPI($method$url$data false)
  118.     {
  119.         $curl = \curl_init();
  120.         switch ($method)
  121.         {
  122.             case "POST":
  123.                 curl_setopt($curlCURLOPT_POST1);
  124.                 if ($data)
  125.                     curl_setopt($curlCURLOPT_POSTFIELDS$data);
  126.                 break;
  127.             case "PUT":
  128.                 curl_setopt($curlCURLOPT_PUT1);
  129.                 break;
  130.             default:
  131.                 if ($data)
  132.                     $url sprintf("%s?%s"$urlhttp_build_query($data));
  133.         }
  134.         // Optional Authentication:
  135.         curl_setopt($curlCURLOPT_HTTPAUTHCURLAUTH_BASIC);
  136.         curl_setopt($curlCURLOPT_USERPWD"username:password");
  137.         curl_setopt($curlCURLOPT_URL$url);
  138.         curl_setopt($curlCURLOPT_RETURNTRANSFER1);
  139.         $result curl_exec($curl);
  140.         curl_close($curl);
  141.         return $result;
  142.     }
  143.     public function doVideoTask($video,$width,$height){
  144.         $videoroot '/home/www/video/public/videotask/';
  145.         $videodir $videoroot.$video['id'];
  146.         $vidpath $videodir.'/'.$video['title'];
  147.         exec('chmod 777 -R '.$videodir.'/');
  148.         chown($videodir'root');
  149.         chown($vidpath'root');
  150.         $originaldim = []; // ,$width,$height
  151.         $originaldim['width'] = (int)$width;
  152.         $originaldim['height'] = (int)$height;
  153.         $mallw $originaldim['width'] /4;
  154.         $mallh $originaldim['height'] /4;
  155.         $medw $originaldim['width'] /2;
  156.         $medh $originaldim['height'] /2;
  157.         $datelog date("Y-m-d H:i:s").' newlogwizi';
  158.         file_put_contents('/home/www/video/var/log/ffmpeglogs'$datelog.PHP_EOLFILE_APPEND);
  159.         file_put_contents('/home/www/video/var/log/awslogs'$datelog.PHP_EOLFILE_APPEND);
  160.         file_put_contents('/home/www/video/var/log/flat-video-task-done-logs'$datelog.PHP_EOLFILE_APPEND);
  161.         
  162.         $contents =  "sudo ffmpeg -nostdin -y -i ".$videodir.'/'.$video['title']." \
  163.         -preset slow -g 48 -sc_threshold 0  \
  164.         -map 0:0 -map 0:1 -map 0:0 -map 0:1 -map 0:0 -map 0:1 \
  165.         -s:v:0 ".$mallw."x".$mallh." -c:v:0 h264 -b:v:0  1000k \
  166.         -s:v:1 ".$medw."x".$medh." -c:v:1 h264 -b:v:1 3200k  \
  167.         -s:v:2 ".$originaldim['width']."x".$originaldim['height']." -c:v:2 h264 -b:v:2 7400k  \
  168.         -c:a copy \
  169.         -var_stream_map \"v:0,a:0 v:1,a:1 v:2,a:2\" \
  170.         -master_pl_name master.m3u8 \
  171.         -f hls -hls_time 1 -hls_list_size 0 \
  172.         -hls_segment_filename \"".$videodir."/v%v/fileSequence%d.ts\" \
  173.         ".$videodir."/v%v/prog_index.m3u8 >> /home/www/video/var/log/ffmpeglogs 2>&1 \n
  174.         \n
  175.         aws s3 cp ".$videodir." s3://wizistagingvideo/".$video['id']."/ --recursive --acl public-read --cache-control max-age=3600 >> /home/www/video/var/log/awslogs 2>&1 \n
  176.         \n
  177.         curl -X POST https://app.wizi.eu/api/public/flat-video-task-done/".$video['id']." >> /home/www/video/var/log/flat-video-task-done-logs 2>&1 \n";
  178.         file_put_contents($videodir.'/command.sh'$contents);
  179.         chmod($videodir.'/command.sh'777);
  180.         exec('sh '.$videodir.'/command.sh > /dev/null &');
  181.         return 'ok';
  182.     }
  183.     public function get_vid_dimD($file)
  184.     {
  185.         //   cd /home/videotask/43
  186.         // /snap/bin/ffmpeg.ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of json 43-1597675743.687.mov
  187.         $command '/snap/bin/ffmpeg.ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of json ' escapeshellarg($file);// . ' 2>&1'
  188.         $dimensions = array();
  189.         exec($command,$output,$status);
  190.         $obj json_decodeimplode($output));
  191.         // var_dump($obj->streams[0]->width); //height
  192.         $command2 '/snap/bin/ffmpeg.ffprobe -loglevel error -show_entries stream_tags=rotate -of json ' escapeshellarg($file);// . ' 2>&1' ;
  193.         exec($command2,$output2,$status2);
  194.         $obj2 json_decodeimplode($output2));
  195.         if (isset($obj2->streams[0]->tags->rotate)) {
  196.             $rotate $obj2->streams[0]->tags->rotate;
  197.             if ($rotate == "90") {
  198.                 $dimensions['width'] = $obj->streams[0]->height;
  199.                 $dimensions['height'] = $obj->streams[0]->width ;
  200.                 //var_dump('condition normale verticale');
  201.             }
  202.             else
  203.             {
  204.                 $dimensions['width'] = $obj->streams[0]->width;
  205.                 $dimensions['height'] = $obj->streams[0]->height;
  206.                 //var_dump('condition normale horizontale');
  207.             }
  208.         } elseif (isset($obj2->streams[1]->tags->rotate)) {
  209.             $rotate $obj2->streams[1]->tags->rotate;
  210.             if ($rotate == "90") {
  211.                 $dimensions['width'] = 720;
  212.                 $dimensions['height'] = 1280 ;
  213.                 //var_dump('condition sans dimentions verticale');
  214.             }
  215.             else
  216.             {
  217.                 $dimensions['width'] = 1280;
  218.                 $dimensions['height'] = 720;
  219.                 //var_dump('condition sans dimentions horizontale');
  220.             }
  221.         } else {
  222.             $dimensions['width'] = 720;
  223.             $dimensions['height'] = 1280 ;
  224.             //var_dump('condition si rien de trouivé');
  225.         }
  226.         return $dimensions;
  227.     }
  228.     public function rrmdir($src) {
  229.         $dir opendir($src);
  230.         while(false !== ( $file readdir($dir)) ) {
  231.             if (( $file != '.' ) && ( $file != '..' )) {
  232.                 $full $src '/' $file;
  233.                 if ( is_dir($full) ) {
  234.                     $this->rrmdir($full);
  235.                 }
  236.                 else {
  237.                     unlink($full);
  238.                 }
  239.             }
  240.         }
  241.         closedir($dir);
  242.         rmdir($src);
  243.     }
  244. }