PHP ffmpeg having problem extracting Image as a thumbnail - php

I am using a below repo via composer everything works well but i have a problem extracting the image as a poster ( thumbnail )
https://github.com/aminyazdanpanah/PHP-FFmpeg-video-streaming#extracting-image
I used below code.
$video = $ffmpeg->open('./videos/example1.mp4");
$frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(51));
$frame->save('/var/media/poster.jpg');
But got an error below using mac os
Fatal error: Uncaught Alchemy\BinaryDriver\Exception\ExecutionFailureException: ffmpeg failed to execute command '/usr/local/bin/ffmpeg' '-y' '-ss' '00:00:51.00' '-i' './videos/example1.mp4' '-vframes' '1' '-f' 'image2' './videos/example1/poster.jpg': Error Output: ffmpeg version N-109428-g10a56363a7-tessus https://evermeet.cx/ffmpeg/
Just want to know what is missing, i have already run chmod -R 0777 . to have a sufficient permissions.

Related

/usr/local/bin/wkhtmltopdf: Permission denied in Linux

I read nearly all topics concerning wkhtmltopdf, but my problem still exists. Actually it's not a wkhtmltopdf problem, but a permission problem.
To sum it up: wkhtmltopdf works in the command line, but not with PHP exec().
But wget works fine with php exec():
I can execute wkhtmltopdf in the command line, works fine:
/usr/local/bin/wkhtmltopdf "test.html" "/var/www/html/test/test.pdf"
exec( "/usr/local/bin/wkhtmltopdf --enable-plugins --encoding UTF-8 --javascript-delay 2000 --load-error-handling ignore --disable-smart-shrinking --image-quality 100 --orientation Portrait --minimum-font-size 10 --header-spacing '0' --margin-top '15mm' --header-html '/tmp/1568618584_generated.html' --footer-left 'Accume Partners Risk Director' --footer-right 'Residual Risk Report -- Page [page] of [toPage]' --footer-font-size '8' --footer-line /tmp/1568618584_generated.html /tmp/R_RM_2110_Residual_Risk_Report.pdf 2>&1", $output, $pdfGenerateStatus);
print_r($output).'<br>';
echo $pdfGenerateStatus;
it's return Array
( [0] => sh: /usr/local/bin/wkhtmltopdf: Permission denied ) 126 error.
I am using wkhtmltopdf 0.12.4 (with patched qt).
Also,
I have set 777 permission to /usr/local/bin/wkhtmltopdf but did not work.

PHP-FFMpeg: How to properly concatenate two videos?

THE SITUATION:
I need it to concatenate multiple videos into one single video.
I am using the library PHP-FFMpeg.
But I don't manage to make it working.
THE VIDEOS:
The videos are recordings made with the MediaRecorder Web API.
The video format is: video/webm;codecs=h264
The audio format is opus.
recorder = new MediaRecorder(this.stream, {
mimeType: 'video/webm;codecs=h264'
})
CONCAT USING PHP-FFMPEG (using saveFromSameCodecs):
This is how I try to concat them using saveFromSameCodecs:
(I have checked the paths and are correct)
$video = $ffmpeg->open( $path1 );
$video
->concat([$path1, $path2])
->saveFromSameCodecs($path_output, TRUE);
But it failed with the following error message:
ffmpeg failed to execute command '/usr/local/bin/ffmpeg' '-f' 'concat' '-safe' '0' '-i' '/private/var/folders/dw/919v2nds7s78pz_qhp7z9rcm0000gn/T/ffmpeg-concath1kHiX' '-c' 'copy' '/Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/storage/app/public/videos/output.mp4'
CONCAT USING FFMPEG COMMAND LINE:
On suggestion on #RolandStarke and #LordNeckbeard I have tried using the ffmpeg command line to get a better insight on what is going on.
If I use the following command line:
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
I get the following error, related to the audio opus codec.
If I use the following command line, converting the audio coded to acc:
ffmpeg -f concat -safe 0 -i mylist.txt -c:v copy -c:a aac output.mp4
The final video is properly created, as a concatenation of the other videos.
CONCAT USING PHP-FFMPEG: (using saveFromDifferentCodecs)
It seems the problem is ONLY related to the codec.
So I have tried using saveFromDifferentCodecs:
$format = new FFMpeg\Format\Video\X264('libfdk_aac', 'libx264');
$result = $video1
->concat([$path1, $path2])
->saveFromDifferentCodecs($format, $output_path);
But I still get an error:
ffmpeg failed to execute command '/usr/local/bin/ffmpeg' '-i' '/Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/storage/app/public/videos/test1.mp4' '-i' '/Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/storage/app/public/videos/test2.mp4' '-filter_complex' '[0:v:0] [0:a:0] [1:v:0] [1:a:0] concat=n=2:v=1:a=1 [v] [a]' '-map' '[v]' '-map' '[a]' '-b:a' '128k' '/Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/storage/app/public/videos/output.mp4'
CONCAT USING PHPFFMPEG (but with different videos):
If the problem is only related to the codec, then using two different videos with video codec: h264 and audio codec aac, it should work, but it doesn't:
ffmpeg failed to execute command '/usr/local/bin/ffmpeg' '-f' 'concat' '-safe' '0' '-i' '/private/var/folders/dw/919v2nds7s78pz_qhp7z9rcm0000gn/T/ffmpeg-concatoJGhLt' '-c' 'copy' '/Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/storage/app/public/videos/output.mp4'
But using the command line it works smoothly: ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
CONCAT USING shell_exec:
I have tried using shell_exec, with the first two videos (opus codec):
echo shell_exec("/usr/local/bin/ffmpeg -f concat -safe 0 -i /Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/mylist.txt -c:v copy -c:a aac /Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/output.mp4 2>&1");
And it works smoothly.
The final output is created, and with the acc audio codec.
TESTING THE LIBRARY:
To see if php-mpeg was actually working, I test it by making a basic resize of a video and it worked correctly.
RESUME:
Using ffmpeg command lines everything works fine
Using shell_exec everything works fine
Using php-ffmpeg I always get the error ffmpeg failed to execute command
QUESTION:
How can I concat videos using php-ffmpeg?
Is the issue caused by wrong encoding?
Thanks!
When encountering issues with php-ffmpeg the best approach is to copy the command from the error message and paste it in terminal. This will give you a better error message.
In your case the the error is
ffmpeg failed to execute command '/usr/local/bin/ffmpeg' '-f' 'concat' '-safe' '0' '-i' '/private/var/folders/dw/919v2nds7s78pz_qhp7z9rcm0000gn/T/ffmpeg-concath1kHiX' '-c' 'copy' '/Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/storage/app/public/videos/output.mp4
Debugging this is a bit harder as the temp file /private/var/[...]/ffmpeg-concath1kHiX is deleted when you try to execute the ffmpeg command in the terminal. To test it you can create the temp file yourself like:
$vidoes = [__DIR__ . '/small.mp4', __DIR__ . '/small.mp4'];
file_put_contents('videolist.txt', implode("\n", array_map(function ($path) {
return 'file ' . addslashes($path);
}, $vidoes)));
Now you can run the ffmpeg command in the terminal
ffmpeg -f concat -safe 0 -i videolist.txt -c copy /Users/[...]/videos/output.mp4
#[...]
#File '/Users/[...]/videos/output.mp4' already exists. Overwrite ? [y/N]
#Not overwriting - exiting
So you your case the error is that the output file already existed. The solution is to use an other non-existing output file or delete it before concatenating your videos.
Here an example how to concatenate files (small.mp4 taken from http://techslides.com/sample-webm-ogg-and-mp4-video-files-for-html5)
<?php
require 'vendor/autoload.php';
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open(__DIR__ . '/small.mp4');
$video
->concat([__DIR__ . '/small.mp4', __DIR__ . '/small.mp4'])
->saveFromSameCodecs(__DIR__ . '/out-'. time() . '.mp4', true);
The correct answer is the one from #RolandStarke.
Here I just want to put together a list of suggestions that may be useful to debug other similar problems in dealing with ffmpeg:
Make a reinstall of ffmpeg. It may be a missing option or a missing codec. Here is how to install (for mac), including all possible options (september 2018). It took me 30minutes so be patient.
brew install ffmpeg --with-chromaprint --with-fdk-aac --with-fontconfig --with-freetype --with-frei0r --with-game-music-emu --with-libass --with-libbluray --with-libbs2b --with-libcaca --with-libgsm --with-libmodplug --with-librsvg --with-libsoxr --with-libssh --with-libvidstab --with-libvorbis --with-libvpx --with-opencore-amr --with-openh264 --with-openjpeg --with-openssl --with-opus --with-rtmpdump --with-rubberband --with-sdl2 --with-snappy --with-speex --with-srt --with-tesseract --with-theora --with-tools --with-two-lame --with-wavpack --with-webp --with-x265 --with-xz --with-zeromqlibzeromq --with-zimg
To have the updated options list you can run: brew options ffmpeg or just google it.
Try to do the same thing you are trying to do, but using the ffmpeg command line only. It will more insight and a better error reporting. For example for concat:
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
Check that PHP-FFMpeg is properly working. Concat it's a bit more difficult operation. You can try with a simpler one and see if it's working and if the library is properly installed. For example you can try a simple conversion:
FFMpeg::fromDisk('local')
->open('public/videos/test.mp4')
->export()
->toDisk('local')
->inFormat(new FFMpeg\Format\Video\X264('libmp3lame', 'libx264'))
->save('public/videos/converted_test.mp4');
Double check every path. And in the case of concat, make sure the output file doesn't already exists.
It may be an encoding problem. Try other types of files with different codecs to see if that's actually the issue. In case you can convert beforehand to the proper codec.
Add line breaks in the source code. In my case I was getting the generic error: Unable to save concatenated video. Here is the source code:
try {
$this->driver->command($commands);
} catch (ExecutionFailureException $e) {
$this->cleanupTemporaryFile($outputPathfile);
$this->cleanupTemporaryFile($sourcesFile);
throw new RuntimeException('Unable to save concatenated video', $e->getCode(), $e);
}
But if I dump the execution and then die it, right before that block:
dd($this->driver->command($commands));
It was giving me a more detailed error message.
You can also dump the commands array: dd($commands);
Finally here is a list of similar issue with PHP-FFMpeg that you can check to see if the solution has already been given: https://github.com/pascalbaljetmedia/laravel-ffmpeg/wiki/FFmpeg-failed-to-execute-command
If you have other suggestions feel free to comment or edit the answer.

Imagick is unable to open DNG images

As you can see, the image _IGP4559.DNG exists but Imagick can't open it.
JPEG images works just fine.
readImage() returns the same error.
Here is the image: http://files.patrikelfstrom.se/_IGP4559.DNG
I run this in the terminal to rule out all other variables.
$ ll /var/www/_IGP4559.DNG
-rw-rw-rw- 1 monsun monsun 11841201 May 17 03:37 /var/www/_IGP4559.DNG
$ php -a
Interactive mode enabled
php > new Imagick('/var/www/_IGP4559.DNG');
PHP Warning: Uncaught exception 'ImagickException' with message 'unable to open image `/tmp/magick-29917oe6CM9MIkN8A.ppm': No such file or directory # error/blob.c/OpenBlob/2709' in php shell code:1
Stack trace:
#0 php shell code(1): Imagick->__construct('/var/www/_IGP45...')
#1 {main}
thrown in php shell code on line 1
ImageMagick uses other programs to read some image formats, including DNG.
What is likely happening is that the program that ImageMagick expects to be able to use to read the DNG either isn't present on your system or isn't working correctly.
The file that lists these other programs is called delegates.xml
On my system the entry for DNG decoding is:
<delegate decode="dng:decode" command=""ufraw-batch" --silent --create-id=also --out-type=png --out-depth=16 "--output=%u.png" "%i""/>
i.e. it's trying to use the program 'ufraw-batch' to do the decoding, and like you're seeing it fails, as that program isn't usable on my system.
To solve this, you either need to install ufraw, or install a similar package that can convert files from the DNG format to a standard one that ImageMagick can read directly.

php libreoffice shell_exec not working

I am trying to convert .docx file to .html using php shell_exec in CentOS 6.5
My php code:
$command = "libreoffice --headless -convert-to html resume.docx 2>&1";
$result = shell_exec($command);
echo $result;
When I run the index.php at http://localhost/converter/ it gives me:
javaldx: Could not find a Java Runtime Environment! Warning: failed to read path from javaldx /usr/lib64/libreoffice/program/soffice.bin X11 error: Can't open display: Set DISPLAY environment variable, use -display option or check permissions of your X-Server (See "man X" resp. "man xhost" for details)`
while in terminal it is working perfectly:
cd /var/www/html/converter/
libreoffice --healdess -convert-to html resume.docx
here it creates resume.html in my /var/www/html/converter/.
Hi i have the same problem, i want to convert PDF's from DOCS created with PHP, i'm using OpenSuse 12.3 with LibreOffice, tried many things, finally i detect that the error is in folder:
1.- First check that you don't have disabled shell_exec in php.ini, and open_basedir don't restrict your access folders.
2.- Run the command as a simple user in shell (terminal)
export HOME=/tmp && soffice --headless --convert-to pdf --outdir /srv/www/htdocs/ /srv/www/htdocs/Creecimientos/sic/app/webroot/usuarios/2/8_Pagare_CreePersonas.docx
3.- If it works, you only have to put the correct folders in your code, when i run this code in PHP, it show me a blank page, so i check the access_log of apache for any hint:
[Java framework] Error in function createSettingsDocument (elements.cxx).
javaldx failed!
Warning: failed to read path from javaldx
terminate called after throwing an instance of 'com::sun::star::uno::RuntimeException'
Note: my error was in using export HOME=/tmp, i checked that the folder in root system has 777 for tmp, but the problem was that apache don't acces to it, maybe search for a relative folder of the script, but after test many things i only put a folder with permissons for wwwrun HOME=/srv/www/htdocs/folder_with_777
This is my final code, that works..
<?php
function word2pdf()
{
echo "Procesando";
$result = shell_exec('export HOME=/srv/www/htdocs/Creecimientos/sic/ && soffice --headless --convert-to pdf --outdir /srv/www/htdocs/Creecimientos/sic/ /srv/www/htdocs/Creecimientos/sic/app/webroot/usuarios/2/8_Pagare_CreePersonas.docx');
echo $result;
}
word2pdf();
?>
In fact, it prints: convert srv/www/htdocs/Creecimientos/sic/app/webroot/usuarios/2/8_Pagare_CreePersonas.docx -> /srv/www/htdocs/Creecimientos/sic//8_Pagare_CreePersonas.pdf using writer_pdf_Export, after succes.
I made other changes before in desesperate mode, but none of them solved the problem, tried to change owner to soffice wich found it witch $ ls -l $(which libreoffice), tried with 777, etc..
/* This command will work on centos 6 /7 with installation of libreoffice headless package */
First install package on centos as :
yum install libreoffice-headless
/* following code work to extract text format from */
<?php
$result = exec("export HOME=/tmp/ && /usr/bin/libreoffice --headless --convert-to txt:Text --outdir /tmp filePath");
var_dump($result);
?>
Most likely the user that LibreOffice is ran as, does not have a writeable home directory so LibreOffice fails to create it's config directory and then it cannot create it's config files and then fails to load Java, because it cannot write the default config. A bit silly I know.
Try adding this parameter: -env:UserInstallation=file:///tmp/whateverhere
I don't have enough reputation to comment on TD_Nijboer's answer, but the answer to his specific problem appears to be that soffice needs to be able to read & write config information somewhere. The first place it tries to do this is the libreoffice directory in ~/.config ('~' means "the current user's home directory").
In Debian, by default, the www-data user has the home directory /var/www, and does not have write permission there.
If you make sure it has permission to either create ~/.config itself, or libreoffice within an existing ~/.config, I expect it will work.
2 things, 1st the command is soffice --headless,
2nd i have an similar javaldx error and it has to do with permission.
when executing as root it works fine, but php executes as www-data.
if anybody knows a good way to execute libreoffice from php please let me know.
as i'm getting an error code 77 saying:
[Java framework] Error in function createSettingsDocument (elements.cxx).
javaldx failed!
Warning: failed to read path from javaldx

FFmpeg - PHP Error Code 127

I am trying to execute FFmpeg from php. I have installed FFmpeg-php, and the extension is in the modules directory and it shows up in phpinfo. FFmpeg is working fine as I can run the command in a terminal and it outputs the video. However, when I try and run the command from php using the following script:
exec(ffmpeg -i input.avi output.avi);
But I get a '127' error code.
The extension is loaded in using:
$extension = "ffmpeg";
$extension_soname = $extension . "." . PHP_SHLIB_SUFFIX;
$extension_fullname = PHP_EXTENSION_DIR . "/" . $extension_soname;
// Locate Extension
define('FFMPEG_LIBRARY', '/usr/local/bin/ffmpeg');
// Load Extension
if (!extension_loaded($extension))
echo dl($extension_soname) or die("Can't load extension $extension_fullname\n");
I have also tried defining the aboslute extension location in the command:
exec(/usr/local/bin/ffmpeg-i input.avi output.avi);
Again, this works in the terminal but still returns the same erro code using the php exec().
Has anyone got any ideas?
Thank you.
If you got the module loaded, use the php-ffmpeg api instead of exec. Run exec only if the module wasn't loaded or else you've loaded the module for nothing (meaning you're not using the module at all).
The error you recive is from ffmpeg binary (not the extension, though you're not using the extension at all) , so search for ffmpeg status 127 error to resolve the problem related to ffmpeg.
I just had the same issue (ffmpeg not running through exec/system)
I sorted it by using /usr/bin/ffmpeg instead of just ffmpeg

Categories