I'm trying to convert some videos using ffmpeg into different formats.
I have this class,
<?php
define('FFMPEG', 'C:\\cygwin\\usr\\local\\bin\\ffmpeg.exe');
class VideoEncoder {
public function multicodeFileIntoDir($file, $directory) {
$parts = explode('/', $file);
$filename = $parts[count($parts)-1];
$encode_android = FFMPEG.' -i "'.$file.'" -s 480x320 -vcodec mpeg4 -acodec aac -ac 1 -ar 16000 -r 13 -ab 32000 -aspect 3:2 "'.$directory.$filename.'.g1.mp4"';
$encode_windows = FFMPEG.' -y -i "'.$file.'" "'.$directory.$filename.'.avi"';
$out = shell_exec($encode_android);
$out = shell_exec($encode_windows);
}
}
On debugging the commands that would be run, they work as expected, and produce the correct file. The permissions on the directory I'm calling this code on has permissions as 777, $out is null, and the command has no effect.
I have error_reporting as E_ALL, and display_errors is TRUE.
An example of the command (the value of $encode_windows just before executing) is
C:\cygwin\usr\local\bin\ffmpeg.exe -y -i "C:\xampp\htdocs\cams/../front/vid20110623_122802.mp4" "C:\xampp\htdocs\cams/../front/vid20110623_122802.mp4.avi"
Edit: It seems like it is writing to STDERR, somehow the output of the command is in the apache error log. Still, the output seems ok..
Any ideas?
Thanks
Related
I am currently trying to convert various video files using FFmpeg with the help of my server (Windows Server 2016). PHP is supposed to initiate the process.
Whenever I start FFmpeg via CMD, everything works fine and the video file converts perfectly. But when I trigger the same process with PHP, FFmpeg aborts the conversion after a while and exits by itself.
I neither get an error in the log nor can I recognize system errors from Windows Server.
It doesn't matter if I run the command through shell_exec, exec, or system.
For converting I actually use this code:
ffmpeg -loglevel error -i $video -vf scale=1920:1080 -crf 10 -c:v libx264 -preset veryfast -threads 2 $output > NUL 2>&1 < NUL
This is my full php code:
ignore_user_abort(true);
set_time_limit(0);
error_reporting( E_ALL | E_STRICT );
$path = 'C:/inetpub/vhosts/confident-tharp.xx-xx-xxx-xxx.plesk.page/httpdocs';
$ffmpeg = $path . '/ffmpeg/ffmpeg.exe';
$video = $path . '/convert/myoldfile.mp4';
$output = $path . '/convertedfile.mp4';
$dimension = 'scale=1920:1080';
$command = "ffmpeg -loglevel error -i $video -vf scale=1920:1080 -crf 10 -c:v libx264 -preset veryfast -threads 2 $output > NUL 2>&1 < NUL";
shell_exec( $command );
I'm currently using FFmpeg 5, but there was no change with FFmpeg 4 either. The use of other codecs or file formats (e.g. .avi) also has no influence on the behavior of FFmpeg. Previously I tried to use the php-ffmpeg library. Again, I get the same "error".
Hope you guys can help.
I am trying to run this in cakephp
function test()
{
$mainDirectoryPath = WWW_ROOT . "media";
$filePath = "userId_10/vid_1444387525.mp4";
$newPath = "userId_10/Output.mp4";
$value = exec("ffmpeg -i $filePath -s 320x320 -vcodec mpeg4 -acodec aac -strict -2 -ac 2 -ar 44100 -ab 128k $newPath");
$this->jsonOutput($value);
}
i am getting nothing $value is empty,
however in shell if i run this as shell_exec() in a php file it works.
what is wrong above
UPDATE:
Ok now i have created a file with following code
<?php
$mainFile = $_REQUEST['mainFile'];
$newPath = $_REQUEST['newFile'];
echo shell_exec("ffmpeg -i $mainFile -s 320x320 -vcodec mpeg4 -acodec aac -strict -2 -ac 2 -ar 44100 -ab 128k $newPath");
#unlink($mainFile);
?>
If i run this with mainFile and newFile it works.
However when i run it in function as
$val = "php video.php?mainFile=$filePath&newFile=$newPath > /dev/null 2>&1 &";
exec($val);
Nothing happens.
Check php.ini file and disable_functions have not exec comamnd.
For security reason site admin disabled exec command in php.ini.
Remove exec from disable_functions in php.ini file.
Have been trying to execute FFMPEG using a script I have uploaded to my domain
<?php
$output = array();
$result = -1;
exec('../../../../../../usr/bin/ffmpeg -ab 320k -i source.wav dest320.mp3', $output, $result);
var_dump($output, $result);
?>
The example code says the program should not be returning -1 unless there is an error but I have pointed to the exact path that FFMPEG is stored in.....
When I call 'ffmpeg -ab 320k -i source.wav dest320.mp3' from CentOS it works...
Am lost and have spent hte last few hours trying to work it out.
Thanks
CP
Any time you have an exec that doesn't work in php, you should switch to passthru for debugging.
passthru('../../../../../../usr/bin/ffmpeg -ab 320k -i source.wav dest320.mp3 1 2>&1');
Appending 1 2>&1 to the end, pipes your stderr to stout, and returns any errors you have while running your exec.
It is probably safer to use passthru('/usr/bin/env ffmpeg -ab 320k -i source.wav dest320.mp3 1 2>&1'); which will get the correct path for ffmpeg without the need to specify and absolute/relative path.
Also worth noting that if you are using Ubuntu since 12.04, the ffmpeg command has been renamed to avconv.
i have a simple site on which people upload videos, so i want to generate a simple thumbnail from an uploaded video. i have tried every trick and way to do this from a number of websites but i am failing to make the command run without problems.
$video = $_FILES['vpopupdropin']["tmp_name"];
$ffmpeg = "C:\\Ffmpeg\\ffmpeg-20130605-git-3289670-win64-static\\bin";
$image = "manu.jpg";
$second = 12;
$size = "150x90";
$command = "$ffmpeg -i $video -an -ss $second -s $size -vcodec mjpeg $image";
echo $command;
shell_exec($command);
if(shell_exec($command)){
echo 'okay';
echo '<img src="'.$image.'"/ >';
}
else{
echo ' Problem';
}
i Echoed the the command from PHP and this is what i got:
C:\Ffmpeg\ffmpeg-20130605-git-3289670-win64-static\bin -i C:\xampp\tmp\php27F1.tmp -an -ss 12 -s 150x90 -vcodec mjpeg manu.jpg Problem
so i took the Command above and entered it in Cmd and got this error
[image2 # 00000000000000003d87580] Could not open file : manu.jpg
av_interleaved_write_frame(): Input/output error. the uploaded file transfers well to where iam saving it and plays well on the site meaning the file is not corrupt. but the thumbnail command seems to fail, i have even checked the other questions on this site but i seem to fail to get the right solution. the paths in the Command are correct and i have verified that at least
You did not give ffmpeg a name :-) So you tried to execute a \\bin folder !
$ffmpeg = "C:\\Ffmpeg\\ffmpeg-20130605-git-3289670-win64-static\\bin";
you forget ffmpeg.exe
$ffmpeg = "C:\\Ffmpeg\\ffmpeg-20130605-git-3289670-win64-static\\bin\\ffmpeg";
I do it for a .avi with following command
ffmpeg -i Echo2012.avi -r 1 -s 1024x576 -f image2 -vframes 1 foo-001.jpg
Don't execute your command twice !
$command = "$ffmpeg -i $video -an -ss $second -s $size -vcodec mjpeg $image";
echo $command;
shell_exec($command);
if(shell_exec($command)){
EDIT :
your command string :
ffmpeg -i upload.tmp -an -ss 12 -s 150x90 -vcodec mjpeg manu.jpg
-vcodec codec (output) : Set the video output codec. It's a switch for a output video. You want as output an image.
-an : You can disable Audio stream. You don't need Audio for an image.
-ss : position (input/output) When used as an input option (before -i), seeks in this input file to position.
my command string :
ffmpeg -i Echo2012.avi -r 1 -s 1024x576 -f image2 -vframes 1 foo-001.jpg
-r : fps (input/output,per-stream) . Set frame rate (Hz value, fraction or abbreviation).
As an input option, ignore any timestamps stored in the file and instead generate timestamps assuming constant frame rate fps.
-f image2 : Force output file format image2. The format is normally auto detected guessed from the file extension for output files.
-vframes number (output) : Set the number of video frames to record.
I am using a ffmpeg command to cut a video with exec function in php. But it is not creating a video file and doesn't show any errors. I used exec as below
exec("ffmpeg -i input.flv -ss 00:00:30.0 -t 00:00:10.0 -acodec copy -vcodec copy -async 1 output.flv");
Can I get any help to know what is the error happening here?
exec("ffmpeg -i input.flv -ss 00:00:30.0 -t 00:00:10.0 -acodec copy -vcodec copy -async 1 output.flv",$output);
I tried it also. But I didn't get any error message in variable $output
As a general rule, you first need to check the return value: non-zero values indicate there has been an error (given that the author of the program you are running adheres to the standard). This value is captured in exec()'s third argument.
In second place, many programs do not send error messages to the standard output. Instead, they use the standard error. In this case, it seems safe to just redirect the latter to the former:
exec("ffmpeg -i input.flv -ss 00:00:30.0 -t 00:00:10.0 -acodec copy -vcodec copy -async 1 output.flv 2>&1", $output, $return_value);