PHP-FFMpeg Installation and Basic Usage on windows issue - php

following the README I installed PHP FFmpeg through Composer and I downloaded binaries from https://ffmpeg.zeranoe.com/builds/
now I'm trying to run the "Basic Usage" example:
<?php
require("vendor/autoload.php");
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('video.mpg');
$video
->filters()
->resize(new FFMpeg\Coordinate\Dimension(320, 240))
->synchronize();
$video
->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
->save('frame.jpg');
$video
->save(new FFMpeg\Format\Video\X264(), 'export-x264.mp4')
->save(new FFMpeg\Format\Video\WMV(), 'export-wmv.wmv')
->save(new FFMpeg\Format\Video\WebM(), 'export-webm.webm');
?>
but i receive "Executable not found" errors because I didn't understand where to put them
I also tried specifying binaries ffmpeg.exe and ffprobe.exe with:
$ffmpeg = FFMpeg\FFMpeg::create(array(
'ffmpeg.binaries' => '/pathtobin/ffmpeg',
'ffprobe.binaries' => '/pathtobin/ffprobe',
'timeout' => 3600, // The timeout for the underlying process
'ffmpeg.threads' => 12 // The number of threads that FFMpeg should use
), $logger);
but still same error... am I making a mountain out of a molehill? any help is appreciated... Thanks
EDIT 1
I added ffmpeg\bin folder to system path and I can run executables from cmd from anywhere, but now I'm getting this error: "Executable not found, proposed : avprobe, ffprobe"...
instead, if I give binary paths explicitly, I get 'ffprobe failed to execute command "C:/FFmpeg/bin/ffprobe.exe" "-help" "-loglevel" "quiet"'
what am I doing wrong?
EDIT 2
web server config
Windows 10 Pro
IIS 10
PHP 7.0.9
site root ---> C:\inetpub\wwwroot\site\
content:
- index.php
- input.mp4
- vendor/ (PHP-FFMpeg library folder)
FFmpeg binaries ---> C:\inetpub\wwwroot\FFmpeg\
content:
- ffmpeg.exe
- ffplay.exe
- ffprobe.exe
index.php
<?php
ini_set('display_errors', 'On'); error_reporting(E_ALL);
require("vendor/autoload.php");
$ffmpeg = FFMpeg\FFMpeg::create(array(
'ffmpeg.binaries' => 'C:/inetpub/wwwroot/FFmpeg/ffmpeg.exe',
'ffprobe.binaries' => 'C:/inetpub/wwwroot/FFmpeg/ffprobe.exe',
'timeout' => 3600, // The timeout for the underlying process
'ffmpeg.threads' => 12, // The number of threads that FFMpeg should use
));
$video = $ffmpeg->open('input.mp4');
?>
result
Fatal error: Uncaught Alchemy\BinaryDriver\Exception\ExecutionFailureException: ffprobe failed to execute command "C:/inetpub/wwwroot/FFmpeg/ffprobe.exe" "-help" "-loglevel" "quiet" in C:\inetpub\wwwroot\site\vendor\alchemy\binary-driver\src\Alchemy\BinaryDriver\ProcessRunner.php:100 Stack trace: #0 C:\inetpub\wwwroot\site\vendor\alchemy\binary-driver\src\Alchemy\BinaryDriver\ProcessRunner.php(72): Alchemy\BinaryDriver\ProcessRunner->doExecutionFailure('"C:/inetpub/www...') #1 C:\inetpub\wwwroot\site\vendor\alchemy\binary-driver\src\Alchemy\BinaryDriver\AbstractBinary.php(209): Alchemy\BinaryDriver\ProcessRunner->run(Object(Symfony\Component\Process\Process), Object(SplObjectStorage), false) #2 C:\inetpub\wwwroot\site\vendor\alchemy\binary-driver\src\Alchemy\BinaryDriver\AbstractBinary.php(137): Alchemy\BinaryDriver\AbstractBinary->run(Object(Symfony\Component\Process\Process), false, NULL) #3 C:\inetpub\wwwroot\site\vendor\php-ffmpeg\php-ffmpeg\src\FFMpeg\FFProbe\OptionsTester.php(61): Alchemy\Binary in C:\inetpub\wwwroot\site\vendor\php-ffmpeg\php-ffmpeg\src\FFMpeg\FFProbe\OptionsTester.php on line 63

The docs say:
This library requires a working FFMpeg install. You will need both FFMpeg and FFProbe binaries to use it. Be sure that these binaries can be located with system PATH to get the benefit of the binary detection, otherwise you should have to explicitly give the binaries path on load.
You will need to store these somewhere permanent and then add this path to your System or User PATH variable:
You would add ";c:\my\path\to\ffmpeg" to the end of the PATH value.

I had this problem too. Spent two days using procmon to determine why PHP wasn't starting ffprobe.exe on a new Windows 2016 install.
Turns out if your IIS AppPool identity doesn't have at least modify writes on the C:\Windows\temp folder, you will get this misleading error.
You can get the App Pool identity/context of your site using (not to be confused with the user context provided from get_current_user(). ):
echo "Get ENV: ".getenv("APP_POOL_ID");
Or
echo "Get SERVER: ".$_SERVER["APP_POOL_ID"];
From my analysis, this is because PHP-CGI.exe, CMD.exe & the ffmpeg exe's (ffmpeg.exe, ffprobe.exe, ffplay.exe) use flat files in the temp folder to exchange and parse information. When PHP-CGI.exe creates a temp file in this location all other processes can only read from the temp file, leaving it empty 0 bytes. Every page load, I had these empty temp files being written to C:\Windows\Temp and ffprobe.exe would never execute.
Hope this helps someone.

Related

Imagick function that works on one server and doesn't work on another

I have a problem moving my script from one server to one other.
The PHP script read a PDF file and create an image for each page of the pdf.
This script currently works in an environment that has these characteristics
PHP version: 7.4.8
Imagick version: 3.4.4
Now I have moved the script to an environment that has these characteristics:
PHP version: 7.4.28
Imagick version: 3.7.0
The code is:
$img = new Imagick($percorsoBaseDir); //LINE OF THE ERROR
$img->setResolution(600, 600);
$num_pages = $img->getNumberImages();
$img->setImageCompressionQuality(100);
$images = NULL;
for ($i; $i < $num_pages; $i++)
{
..... [My script] ....
}
And the error is:
AH01071: Got error 'PHP message: PHP Fatal error: Uncaught ImagickException: Failed to read the file in /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/custom_plugin/includes/woocommerce/Step_2.php:222\nStack trace:\n#0 /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/custom_plugin/includes/woocommerce/Step_2.php(222): Imagick->readImage()\n#1 /var/www/vhosts/mysite.com/httpdocs/wp-includes/shortcodes.php(356): step_2_function()\n#2 [internal function]: do_shortcode_tag()\n#3 /var/www/vhosts/mysite.com/httpdocs/wp-includes/shortcodes.php(228): preg_replace_callback()\n#4 /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/js_composer/include/helpers/helpers.php(247): do_shortcode()\n#5 /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/js_composer/include/templates/shortcodes/vc_column_text.php(31): wpb_js_remove_wpautop()\n#6 /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/js_composer/include/classes/shortcodes/core/class-wpbakeryshortcode.php(451): require('/var/www/...',
I also tried throwing:
getcwd() //to verify that the file exists
and:
is_readable ($ pathBaseDir) //to verify that the file exist and is readable
is_writable ($ pathBaseDir) //to verify that the file exist and is writable
and they both return TRUE.
I also checked that the file loaded correctly in the file system and that I could open it from the browser.
I can't understand what can be the error, has something similar happened to you? How could I solve?
Thank you!
I find the problem: I have installed the Ghost Script in my server with this command:
apt-get install ghostscript
And now it works!

php Tesseract Error! The command "tesseract" was not found

I'm trying and failing to use tesseract php. I get this error:
Fatal error: Uncaught thiagoalessio\TesseractOCR\TesseractNotFoundException: Error! The command "tesseract" was not found. Make sure you have Tesseract OCR installed on your system: https://github.com/tesseract-ocr/tesseract
The current $PATH is C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\xampp\php;C:\ProgramData\ComposerSetup\bin;C:\Users\Peppe\AppData\Local\Microsoft\WindowsApps;C:\Users\Peppe\AppData\Roaming\Composer\vendor\bin in C:\Users\Peppe\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php:48 Stack trace: #0
C:\Users\Peppe\vendor\thiagoalessio\tesseract_ocr\src\TesseractOCR.php(26): thiagoalessio\TesseractOCR\FriendlyErrors::checkTesseractPresence('tesseract') #1 C:\xampp\htdocs\index.php(7): thiagoalessio\TesseractOCR\TesseractOCR->run() #2 {main} thrown in C:\Users\Peppe\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php on line 48
I'm using windows 10 with xampp installed in C:\xampp. php version 7.4
I installed tesseract.exe from https://github.com/UB-Mannheim/tesseract/wiki both x32 and x64
I used composer to install the https://github.com/thiagoalessio/tesseract-ocr-for-php and it gave no error.
<?php
require_once 'C:\Users\Peppe\vendor\autoload.php';
use thiagoalessio\TesseractOCR\TesseractOCR;
$ocr = new TesseractOCR("caption.jpg");
$content = $ocr->run();
echo $content;
?>
finally, the caption.jpg is in the htdocs folder, the main folder and same folder as index.php where the aforementioned code appears.
any solutions?
I found the answer:
the $PATH variable is taken from Windows. To set it right search for "system environment variables", click "environment variables", edit PATH and add the tesseract software folder, for example C:\Program Files\Tesseract-OCR
In my case I have to add new variable tesseract with full path C:\Program Files\Tesseract-OCR\tesseract.exe as showing in below screenshot
To install TESSERACT OCR and use it in PHP
You must perform the following steps:
(in my explanation, I assume you are on windows)
url src = https://tesseract-ocr.github.io/tessdoc/Installation.html
1- Download the TESSERACT software from URL:
https://github.com/UB-Mannheim/tesseract/wiki
2- From your composer, you must install:
url: https://packagist.org/packages/thiagoalessio/tesseract_ocr
composer require thiagoalessio/tesseract_ocr
3- Then, it is important to do things in the following order:
Your TESSERACT software (step 1) is installed in a directory
which will certainly be (it's up to you to check in your system)
folder = C:\Program Files\Tesseract-OCR
It is absolutely necessary to add this folder in the windows environment variables at the PATH level (user and system)
4- Then you restart your web environment (WAMP, LARAGON ...)
5- Verification that TESSERACT is operational.
Go to your CMD and write:
tesseract --version
Reply somethink like that :
tesseract v5.3.0.20221222
leptonica-1.78.0
libgif 5.1.4 : libjpeg 8d (libjpeg-turbo 1.5.3) : libpng 1.6.34 : libtiff 4.0.9 : zlib 1.2.11 : libwebp 0.6.1 : libopenjp2 2.3.0
Found AVX2
Found AVX
Found FMA
Found SSE4.1
Found libarchive 3.5.0 zlib/1.2.11 liblzma/5.2.3 bz2lib/1.0.6 liblz4/1.7.5 libzstd/1.4.5
Found libcurl/7.77.0-DEV Schannel zlib/1.2.11 zstd/1.4.5 libidn2/2.0.4 nghttp2/1.31.0
6- Operation test from a PHP script
uses an image on a white background with text
<?php
$app_class = 'thiagoalessio\TesseractOCR\TesseractOCR';
$file = 'C:/Users/admin/Desktop/im2.jpg';
$class = new ReflectionClass($app_class);
$infoImg = $class->newInstanceArgs();
$infoImg->lang('eng','jpn','spa','fr');
$infoImg->image($file);
$infoImg->withoutTempFiles();
$resultat = $infoImg->run();
var_dump($resultat);
?>

Attempted to load class "Imagick" from the global namespace in symfony3

So I'm currently using "Imagick" extension for a symfony project, but I get this error:
ClassNotFoundException: "Attempted to load class "Imagick" from the global namespace. Did you forget a "use" statement?"
Strangely, it's working fine in all native php script, but not working in symfony project!
I did some research and I found that I need to enable imagick for CLI also .. but I didn't find any method explain how to make it.
So, I verified Imagick installation by this code snippet:
<?php
header('Content-type: image/jpeg');
$image = new imagick("C:/wamp64/www/test/image.jpg");
$image->thumbnailImage(100,0);
echo $image;
and I can see the image loaded correctly.
Also, when I run this script from the browser:
<?php
if (extension_loaded('imagick')){
echo 'imagick is installed';
} else {
echo 'imagick not installed';
}
?>
I get :
imagick is installed
But when I execute this file from command-line interface, I get :
imagick not installed
In the Symfony project I get this error log:
Uncaught PHP Exception Symfony\Component\Debug\Exception\ClassNotFoundException: "Attempted to load class "Imagick" from the global namespace. Did you forget a "use" statement?"
Symfony function
private function createThumbnail($path, $dest, $width, $height)
{
$im = new \Imagick();
$im->pingImage($path);
$im->readImage($path);
$im->thumbnailImage($width, $height);
$white=new \Imagick();
$white->newImage($width, $height, "white");
$white->compositeImage($im, \Imagick::COMPOSITE_OVER, 0, 0);
$white->setImageFormat('jpg');
$white->writeImage($dest);
$im->destroy();
$white->destroy();
}
Environment
PHP Version => 7.1.9
System => Windows 10
Server => Wamp64
Symfony version => 3.1
Compiler => MSVC14 (Visual C++ 2015)
Architecture => x64
imagick module version => 3.4.3
ImageMagick version => ImageMagick 6.9.3-7 Q16 x64 2016-03-27
The complete ImageMagick/Symfony installation steps are the following (here for Windows):
Download the package from https://pecl.php.net/package/imagick/3.5.1/windows
Extract from php_imagick-….zip the php_imagick.dll file, and save it to the ext directory of your PHP installation
Extract the dependencies
• For PHP 5.x/7.x: Extract from ImageMagick-….zip the DLL files located in the bin folder that start with CORE_RL or IM_MOD_RL or FILTER, and save them to the PHP root directory (where you have php.exe), or to a directory in your PATH variable:
• 17 files CORE_RL*.dll
• 126 files IM_MOD_RL*.dll
• 1 files FILTER*.dll
• For PHP 8.x: Extract from php_imagick-….zip the DLL files that start with CORE_RL or IM_MOD_RL or FILTER, and save them to the PHP root directory (where you have php.exe), or to a directory in your PATH variable
Add this line to your php.ini file: extension=php_imagick.dll
Restart the Apache/NGINX Windows service (if applicable)
Check if the install is correct through phpinfo() for which a specific imagick block has been added
Don’t forget to add use Imagick; to your php file
I found the solution, I put the answer for someone who have the same issue:
you should add the following lines
;on Windows:
extension=php_imagick.dll
;on UNIX:
extension = imagick.so
to C:\wamp64\bin\php\phpx.x.x\php.ini
not to C:\wamp64\bin\apache\apachex.x.x\bin\php.ini
because the first php.ini is the file used by PHP CLI, so by the Symfony Local Web Server.
Happy coding!

fatal error: Uncaught exception 'Alchemy\BinaryDriver\Exception\ExecutableNotFoundException' with message 'Executable not found, proposed

I have installed https://github.com/PHP-FFMpeg/PHP-FFMpeg on linux
hosting path is
/public_html/videoconversion/
I got this error.
Fatal error: Uncaught exception 'Alchemy\BinaryDriver\Exception\ExecutableNotFoundException' with message
'Executable not found, proposed :
public_html/videoconversion/' in
/home/deveducate/public_html/videoconversion/vendor/alchemy/binary-driver/src/Alchemy/BinaryDriver/AbstractBinary.php:160 Stack trace:
#0 /home/deveducate/public_html/videoconversion/vendor/php-ffmpeg/php-ffmpeg/src/FFMpeg/Driver/FFProbeDriver.php(48):
Alchemy\BinaryDriver\AbstractBinary::load('public_html/vid...', NULL, Object(Alchemy\BinaryDriver\Configuration))
#1 /home/deveducate/public_html/videoconversion/vendor/php-ffmpeg/php-ffmpeg/src/FFMpeg/FFProbe.php(226): FFMpeg\Driver\FFProbeDriver::create(Array, NULL)
#2 /home/deveducate/public_html/videoconversion/vendor/php-ffmpeg/php-ffmpeg/src/FFMpeg/FFMpeg.php(117): FFMpeg\FFProbe::create(Array, NULL, NULL)
#3 /home/deveducate/public_html/videoconversion/convert_to_mp4.php(10): FFMpeg\FFMpeg::create(Array, NULL)
#4 {main} Next exception 'FFMpeg\Exception\ExecutableNotFoundException' with message
'U in /home/deveducate/public_html/videoconversion/vendor/php-ffmpeg/php-ffmpeg/src/FFMpeg/Driver/FFProbeDriver.php on line 50
file
require 'vendor/autoload.php';
$ffmpeg = FFMpeg\FFMpeg::create(array(
'ffmpeg.binaries' => '/opt/local/ffmpeg/bin/ffmpeg',
'ffprobe.binaries' => '/opt/local/ffmpeg/bin/ffprobe',
'timeout' => 3600, // The timeout for the underlying process
'ffmpeg.threads' => 12, // The number of threads that FFMpeg should
use
), $logger);
$video = $ffmpeg->open('video.mpg');
$video
->filters()
->resize(new FFMpeg\Coordinate\Dimension(320, 240))
->synchronize();
$video
->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
->save('frame.jpg');
$video
->save(new FFMpeg\Format\Video\X264(), 'export-x264.mp4')
May any one let me know where to update path.
Thanks
Per the documentation:
FFMpeg will autodetect ffmpeg and ffprobe binaries. If you want to give binary paths explicitly, you can pass an array as configuration.
Example:
$ffmpeg = FFMpeg\FFMpeg::create(array(
'ffmpeg.binaries' => '/opt/local/ffmpeg/bin/ffmpeg',
'ffprobe.binaries' => '/opt/local/ffmpeg/bin/ffprobe',
'timeout' => 3600, // The timeout for the underlying process
'ffmpeg.threads' => 12, // The number of threads that FFMpeg should use
), $logger);
In your code, it looks like you need to make the changes in this file:
/home/deveducate/public_html/videoconversion/convert_to_mp4.php
NOTE: It also says this in the docs, so you may be able to just add them to the system path:
This library requires a working FFMpeg install. You will need both FFMpeg and FFProbe binaries to use it. Be sure that these binaries can be located with system PATH to get the benefit of the binary detection, otherwise you should have to explicitly give the binaries path on load.
I had a similar issue and finally resolved it:
Make sure ffmpeg is installed (apt get install ffmpeg)
In addition, if you have a PHP openbasedir restriction, make sure to add ffmpeg and ffprobe to it:
open_basedir="/tmp:/usr/share/php::/usr/bin/ffmpeg:/usr/bin/ffprobe"

PHP AWS HTTP error: Unable to open using mode r+ fopen

I have been transferring my PHP server files from windows to ubuntu 14.04 and my script broke...
I'm getting the error when downloading from S3.
$result = $s3->getObject(array(
'Bucket' => AWS_S3_BUCKET,
'Key' => $imgName,
'SaveAs' => $targetPath
)); //the exception is thrown!
The exception message:
Error executing GetObject on http://s3.amazonaws.com/bucket/file AWS
HTTP error: Unable to open ../../downloads/file using mode r+:
fopen(../../downloads/file).
sudo chmod -R 777 wwwfolder didn't work
Any ideas?
** Update **
Found a Hax/Workaround.... adding touch($targetPath) before $s3->getObject method fixes the problem... but why? I tried on different ubuntu server (same version) and there it works (without touch; and i don't know how it was configured..).
What about now? Any ideas?

Categories