<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\WiziLib\AmazonWraper;
use Symfony\Component\Routing\Annotation\Route;
class LuckyController
{
/**
* @Route("/lucky/number")
*/
public function number(): Response
{
$number = random_int(0, 100);
var_dump(phpinfo());
die();
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'
);
}
/**
* @Route("/")
*/
public function home(): Response
{
$number = random_int(0, 100);
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'
);
}
/**
* form vidéo hls
*
* @param Request $request
* @param $flat
* @return JsonResponse
* @Route("/flat-video-hls/{flat}", defaults={"_format" = "json"})
* @throws \Exception
*/
public function postHLSsendAction(Request $request, $flat)
{
set_time_limit(10000);
/*
width: 1920
height: 1080
*/
$data = ['reponse'=>'OK'];
$c = 0;
$files = $request->files->get('files');
$width = $request->get('width');
$height = $request->get('height');
$objectType = 'Flat';
if ($request->get('object')) {
$objectType = $request->get('object');
}
$n = '';
$tab = [];
foreach ($files as $f) {
$c++;
$vv = [];
$tab[$c]['name'] = $f->getClientOriginalName();
$tab[$c]['extension'] = $f->guessExtension();
// return new JsonResponse($tab) ;
/*
* en curl
$task = new VideoTask();
$task->setType('video');
$task->setCreatedAt(new \DateTime());
$task->setExtension($f->guessExtension());
$task->setFilename(microtime(true));
$task->setTitle('');
$task->setObjectName('Flat');
$task->setObjectId($flat->getId());
$task->setDone(0);
$em->persist($task);
$em->flush();
*/
$tasktabr = $this->CallAPI("POST","https://app.wizi.eu/api/public/flat-video-task-from-graph-server/".$flat."/".$tab[$c]['extension'].'/'.$objectType);
$tasktab = json_decode($tasktabr,true);
$task= $tasktab['videotask'];
//return new JsonResponse($task) ;
if(!is_dir('/home/www/video/public/videotask/'.$task['id'])){
mkdir( '/home/www/video/public/videotask/'.$task['id'],0777);
$videodir = '/home/www/video/public/videotask/'.$task['id'].'/';
}
$image = file_get_contents($f);
if ($tab[$c]['extension'] == 'qt') {
file_put_contents($videodir.$task['id'].'-'.$task['filename'].'.mov', $image);
// public/update-video-task/{videotask}/{extension}
$dsend = ['title' => $task['id'].'-'.$task['filename'].'.mov'];
$vv['title'] = $task['id'].'-'.$task['filename'].'.mov';
$vv['id'] = $task['id'];
$dsend['extension'] = 'mov';
$this->CallAPI("POST","https://app.wizi.eu/api/public/update-video-task/".$task['id']."/".$tab[$c]['extension'],$dsend);
}
else
{
file_put_contents($videodir.$task['id'].'-'.$task['filename'].'.'.$task['extension'], $image);
$dsend = ['title' => $task['id'].'-'.$task['filename'].'.'.$task['extension']];
$dsend['extension'] = $task['extension'];
$vv['title'] = $task['id'].'-'.$task['filename'].'.'.$task['extension'];
$vv['id'] = $task['id'];
$this->CallAPI("POST","https://app.wizi.eu/public/update-video-task/".$task['id']."/".$tab[$c]['extension'],$dsend);
}
$this->doVideoTask($vv,$width,$height);
}
$data['files'] = $tab ;
$data['count'] = $c;
$data['videotask'] = $task;
return new JsonResponse($data);
// return $data;
}
// Method: POST, PUT, GET etc
// Data: array("param" => "value") ==> index.php?param=value
public function CallAPI($method, $url, $data = false)
{
$curl = \curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
public function doVideoTask($video,$width,$height){
$videoroot = '/home/www/video/public/videotask/';
$videodir = $videoroot.$video['id'];
$vidpath = $videodir.'/'.$video['title'];
exec('chmod 777 -R '.$videodir.'/');
chown($videodir, 'root');
chown($vidpath, 'root');
$originaldim = []; // ,$width,$height
$originaldim['width'] = (int)$width;
$originaldim['height'] = (int)$height;
$mallw = $originaldim['width'] /4;
$mallh = $originaldim['height'] /4;
$medw = $originaldim['width'] /2;
$medh = $originaldim['height'] /2;
$datelog = date("Y-m-d H:i:s").' newlogwizi';
file_put_contents('/home/www/video/var/log/ffmpeglogs', $datelog.PHP_EOL, FILE_APPEND);
file_put_contents('/home/www/video/var/log/awslogs', $datelog.PHP_EOL, FILE_APPEND);
file_put_contents('/home/www/video/var/log/flat-video-task-done-logs', $datelog.PHP_EOL, FILE_APPEND);
$contents = "sudo ffmpeg -nostdin -y -i ".$videodir.'/'.$video['title']." \
-preset slow -g 48 -sc_threshold 0 \
-map 0:0 -map 0:1 -map 0:0 -map 0:1 -map 0:0 -map 0:1 \
-s:v:0 ".$mallw."x".$mallh." -c:v:0 h264 -b:v:0 1000k \
-s:v:1 ".$medw."x".$medh." -c:v:1 h264 -b:v:1 3200k \
-s:v:2 ".$originaldim['width']."x".$originaldim['height']." -c:v:2 h264 -b:v:2 7400k \
-c:a copy \
-var_stream_map \"v:0,a:0 v:1,a:1 v:2,a:2\" \
-master_pl_name master.m3u8 \
-f hls -hls_time 1 -hls_list_size 0 \
-hls_segment_filename \"".$videodir."/v%v/fileSequence%d.ts\" \
".$videodir."/v%v/prog_index.m3u8 >> /home/www/video/var/log/ffmpeglogs 2>&1 \n
\n
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
\n
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";
file_put_contents($videodir.'/command.sh', $contents);
chmod($videodir.'/command.sh', 777);
exec('sh '.$videodir.'/command.sh > /dev/null &');
return 'ok';
}
public function get_vid_dimD($file)
{
// cd /home/videotask/43
// /snap/bin/ffmpeg.ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of json 43-1597675743.687.mov
$command = '/snap/bin/ffmpeg.ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of json ' . escapeshellarg($file);// . ' 2>&1'
$dimensions = array();
exec($command,$output,$status);
$obj = json_decode( implode($output));
// var_dump($obj->streams[0]->width); //height
$command2 = '/snap/bin/ffmpeg.ffprobe -loglevel error -show_entries stream_tags=rotate -of json ' . escapeshellarg($file);// . ' 2>&1' ;
exec($command2,$output2,$status2);
$obj2 = json_decode( implode($output2));
if (isset($obj2->streams[0]->tags->rotate)) {
$rotate = $obj2->streams[0]->tags->rotate;
if ($rotate == "90") {
$dimensions['width'] = $obj->streams[0]->height;
$dimensions['height'] = $obj->streams[0]->width ;
//var_dump('condition normale verticale');
}
else
{
$dimensions['width'] = $obj->streams[0]->width;
$dimensions['height'] = $obj->streams[0]->height;
//var_dump('condition normale horizontale');
}
} elseif (isset($obj2->streams[1]->tags->rotate)) {
$rotate = $obj2->streams[1]->tags->rotate;
if ($rotate == "90") {
$dimensions['width'] = 720;
$dimensions['height'] = 1280 ;
//var_dump('condition sans dimentions verticale');
}
else
{
$dimensions['width'] = 1280;
$dimensions['height'] = 720;
//var_dump('condition sans dimentions horizontale');
}
} else {
$dimensions['width'] = 720;
$dimensions['height'] = 1280 ;
//var_dump('condition si rien de trouivé');
}
return $dimensions;
}
public function rrmdir($src) {
$dir = opendir($src);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
$full = $src . '/' . $file;
if ( is_dir($full) ) {
$this->rrmdir($full);
}
else {
unlink($full);
}
}
}
closedir($dir);
rmdir($src);
}
}