I have a script that traverses a directory looking for video files in it and then I use shell_exec
with ffmpeg
to convert it, the problem is that my site is falling during script execution, after starting even on the server, the site crashes and can not access until I shut down the script, I'm using Wamp installed on a Windows Server 2012.
I think maybe the problem is in memory_limit
in PHP.ini which is in 128m
, but I'm not sure, is there any solution to prevent my site from crashing during this process?
This is my short script:
if($handle = opendir($diretorio)){
while ($entry = readdir($handle)){
$ext = strtolower(pathinfo($entry, PATHINFO_EXTENSION));
if(in_array($ext, $types)){
$ep = explode('.', $entry);
$input = $diretorio.$entry;
$cmd = $ffmpeg.' -i '.$input.' -hide_banner 2>&1 &';
$exec = shell_exec($cmd);
$data = trim($exec);
$x = getStreams($data);
$video_key = array_search('Video', array_column($x, 'formato'));
$video_map = $x[$video_key]["tempo"];
foreach ($x as $index => $value){
#esse if se repete mais 4x procurando outros formatos e/ou idiomas
if($value["formato"] === "Audio" && ($value["idioma"] === "jpn" || $value["idioma"] === "eng")){
$audio_map = $value["tempo"];
$output_legendado = $dir_original_saida.$ep["0"].".mp4";
if (!is_dir($dir_original_saida)) {
mkdir($dir_original_saida, 0777, true);
}
if(!file_exists($output_legendado)){
$query = " -sn -map $video_map -map $audio_map -vcodec libx264 -acodec aac -ab 128k -y -movflags +faststart $output_legendado ";
}
}
}
$cmd_con = $ffmpeg." -i $input $query 2>&1 &";
$out = exec($cmd_con);
echo "<pre>";
print_r($out);
echo "</pre>";
}
}
closedir($handle);
}