quasarstream / PHP-FFmpeg-video-streaming

📼 Package media content for online streaming(DASH and HLS) using FFmpeg
https://www.quasarstream.com/op/php/ffmpeg-streaming?u=php-ff
MIT License
516 stars 118 forks source link

PHP FPM with Nginx, unwritable file #104

Closed Nolanrulesroblox closed 3 years ago

Nolanrulesroblox commented 3 years ago

Describe the bug

2021/08/03 21:47:35 [error] 19363#19363: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Streaming\Exception\RuntimeException: A temporary file could not be created. in /home/***/Desktop/HLS2/vendor/aminyazdanpanah/php-ffmpeg-video-streaming/src/File.php:91
Stack trace:
#0 /home/***/Desktop/HLS2/vendor/aminyazdanpanah/php-ffmpeg-video-streaming/src/HLSKeyInfo.php(55): Streaming\File::tmp()
#1 /home/***/Desktop/HLS2/vendor/aminyazdanpanah/php-ffmpeg-video-streaming/src/HLSKeyInfo.php(65): Streaming\HLSKeyInfo->__construct()
#2 /home/***/Desktop/HLS2/vendor/aminyazdanpanah/php-ffmpeg-video-streaming/src/HLS.php(133): Streaming\HLSKeyInfo::create()
#3 /home/***/Desktop/HLS2/encode.php(42): Streaming\HLS->encryption()
#4 {main}
  thrown in /home/***/Desktop/HLS2/vendor/aminyazdanpanah/php-ffmpeg-video-streaming/src/File.php on line 91" while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /encode.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.4-fpm.sock:", host: "localhost"

To Reproduce Opened the encode.php encode.php:

<?php
$time_pre = microtime(true);
$vid = isset($_GET['url']);
if (empty($vid)) {
    $vid = '/home/***/Desktop/HLS2/1.mkv';
}
require 'vendor/autoload.php'; // path to the autoload file
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Streaming\FFMpeg\Exception\ExceptionInterface;
use Streaming\FFMpeg;
use FFMpeg\FFProbe;
use FFMpeg\Coordinate\Dimension;
use FFMpeg\Format\Video\X264;
$config = [
    'ffmpeg.binaries'  => '/usr/bin/ffmpeg',
    'ffprobe.binaries' => '/usr/bin/ffprobe',
    'timeout'          => 3600, // The timeout for the underlying process
    'ffmpeg.threads'   => 12,   // The number of threads that FFmpeg should use
];
//$save_to = './2/key';
$time = md5(time());
$url_2 = '/home/***/Desktop/HLS2/2/'.$time.'/';
if (!file_exists($url_2)) {
    mkdir($url_2, 0777, true);
}
$url_base = 'http://localhost:80';
echo $url_2;
fopen($url_2."testfile.txt", "w");
$url = $url_base.'/2/'.$time.'/'.$time.'.key';
$save_to = $url_2.$time.'.key';
$log = new Logger('FFmpeg_Streaming');
$log->pushHandler(new StreamHandler('/home/***/Desktop/HLS/loog/ffmpeg-streaming.log')); // path to log file
$ffmpeg = Streaming\FFMpeg::create($config, $log);
$video = $ffmpeg->open($vid);
$video->hls()
    ->encryption($save_to, $url, 90)
    ->x264()
    ->autoGenerateRepresentations([1080, 720])
    ->save('/home/***/Desktop/HLS2/2/'.$time.'/part');
$time_post = microtime(true);
$exec_time = $time_post - $time_pre;
echo $exec_time.' Seconds to run';
?>

Expected behavior Render video, then just load (on load, another script takes the m3u8)

Screenshots Desktop/Server (please complete the following information):

Additional context This worked fine on the PHP-CLI local server, maybe FPM locked the files?

I have tried sudo chmod 777 -R * on the folder, still same issue

angeljunior commented 3 years ago

The user running PHP does not have permission to write to the tmp folder.