PHP and FFMPEG advice/solution for a unknown issue - php

I am having a live video streaming website and I allow people to upload all the video files to the site till now everything is ok.
What I am doing is that I am using FFMPEG to convert the video to mp4 and also get a image out of the video after 20 seconds.
The code that I am using is that:
require 'vendor/autoload.php';
$getEXT_check=substr(#$_FILES['profileimage99']['name'],-3);
if($getEXT_check !='mp4' && $getEXT_check !='MP4'){
exec('ffmpeg -i '.$uploadfile.' -f mp4 -s 896x504 '.$new_flv.''); }
//execute ffmpeg and create thumb
exec('ffmpeg -i '.$uploadfile.' -ss 00:00:20 -vframes 1 '.$new_image_path);
$theduration=exec('ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 '.$uploadfile.' 2>&1');
$theduration_val=round($theduration/60, 2);
What issue I am having is that
sometimes the code dosent extract the image out of video
video conversation takes very long time for non mp4 video (I am not converting mp4 videos)
Some info what I have done on server. During the development of site I installed ffmpeg on my pc and downloaded the vendor directory by composer.json file. But I have not done these things on my server I have a dedicated server from bluehost and have ffmpeg installed at this location.
/usr/local/bin/ffmpeg
/usr/local/bin/mplayer
/usr/local/bin/mencoder
/usr/bin/flvtool2
/usr/local/bin/MP4Box
/usr/local/bin/yamdi
During uploading the site what I did is that I also uploaded the vendor directory and all the files and made the site live. I haven't used any of the path as given by my server admin after asking.
Please suggest me something where I am doing wrong.

Related

ffmpeg command executes via command line, but not via PHP script

I am working on a simple video upload and compression system and I am currently using the following process.
First Step
I use a multipart/form-data to upload the original video file, which I store in /var/www/site/videos_pre/video.mp4. My public folder is var/www/site/public_html/. I store an entry in my database with the video information.
Second step
I have a converter process which is very basic, but does the job (at least in CLI).
It has the following code:
public function converter($content_id)
{
$content = $this->videos_m->get($content_id);
$id = uniqid();
$name_mp4 = $content->name_slug.'_'.$id.'.mp4';
$name_webm = $content->name_slug.'_'.$id.'.webm';
$command_mp4 = 'ffmpeg -i ../videos_pre/'.$content->original_file.' -b:v 1500k -bufsize 1500k ./videos/'.$name_mp4;
system($command_mp4);
$command_webm = 'ffmpeg -i ./videos/'.$name_mp4.' -c:v vp9 -c:a libvorbis ./videos/'.$name_webm;
system($command_webm);
$update_video = new stdClass();
$update_video->archivo_mp4 = $name_mp4;
$update_video->archivo_webm = $name_webm;
$this->db->where('content_id', $content_id);
$this->db->update('Videos', $update_video);
}
Third step - where the problem occurs
I have tested this on Windows 10 and Ubuntu 18.04. The code works on Windows 10 in both php and cli. The code on Ubuntu only works with cli.
The resulting commands are something like this:
// First command to reduce the bitrate of the mp4
ffmpeg -i /var/www/site/videos_pre/video.mp4 -b:v 1500k -bufsize 1500k /var/www/site/public_html/videos/video_5e757d3e0d762.mp4
// Second command to convert the mp4 to a webm to get both types
ffmpeg -i /var/www/site/public_html/videos/video_5e757d3e0d762.mp4 -c:v vp9 -c:a libvorbis /var/www/site/public_html/videos/video_5e757d3e0d762.webm
If I execute those commands they work perfectly. If I run the php script, be it in the browser or via CLI, literally nothing happens. No error messages nothing.
I am not sure if it's a permission issue, if there's something specific for php to run this, or some module I've not activated. I have enough execution time, I have it at 600 seconds, but as I said it doesn't even take time, it just does nothing.
I can even echo the commands and they appear. So, basically the problem is I need to be able to run those commands from PHP. I did it on Windows, but Ubuntu 18.04 won't let me. Both OSs have ffmpeg recently installed and I've been able to convert on both of them.
I tried changing paths from absolute to relative, no difference at all.
So, I managed to solve it, and it turns out it was a permission problem.
I had my /var/www/site folder with permissions like this: myuser:www-data. After checking this answer on a similar issue, I changed the folder permission to www-data:root and it worked instantly.
I hope that permission change doesn't affect the rest of things, but for now everything's working fine.

Convert 720p Mp4 to 480p with php ffmpeg

I need to covert 720p or 1080p video to 480p mp4 video , i found below code
ffmpeg -i input -vf scale=-1:480 -vcodec mpeg4 -qscale 3 output.mp4
and please help me , i am unable to give it INPUT VIDEO ex.
$video='/path/to/mp4/video';
exec('ffmpeg -i $video -vf scale=-1:480 -vcodec mpeg4 -qscale 3 output.mp4');
why above code is not working
i used that code and it worked flawless. maybe you werent in the dir with the vid files? or something? you should replace YOURVID.mkv with your file OR /path/to/your/file.mkv as well as replace /path/to/output.mkv with the file name or a file path to the converted output 480p file. easiest thing to do though is be in the dir with the original 720p/1080p video and just use the filename.mkv then output it to that same dir by just using an output.file.name.mkv and not a path. then all files will be in that one directory. i may have over explained it. sorry. lol. and THANK YOU for posting this. it did, indeed, work great for me on my freebsd machine with ffpmeg installed.
ffmpeg -i YOURVID.mkv -vf scale=-1:480 -vcodec mpeg4 -qscale 3 /path/to/new/output.mp4
I have an old 32bit Intel Atom Acer netbook, its not much good for anything. I have batocera installed on it and i need 480p video to play on it.
P.S. with a 320GB HDD, the prev mentioned netbook, some retro game roms and some 480p video files makes an excellent mobile entertainment center.

ffmpeg - which video rate is correct?

I am using ubuntu 13.04
Version of ffmpeg is ffmpeg version N-61041-g52a2138
Through php, I am calling ffmpeg commands to extract images and do some modification in those images and again merge those images back to video.
While merging all images through ffmpeg command, I need to pass video bit rates.
Command is as below
public function mergeImagesToVideo($frame_no,$user_directory_name,$video_bit_rates,&$output,&$status){
/* merge all of frames to create one second video */
$user_frames_path=$GLOBALS["all_user_dir_path"].$user_directory_name."/";
$command= $GLOBALS['ffmpeg'].' -f image2 -r 30 -i '.
$user_frames_path.'f'.$frame_no.'_%d.png -r 30 -b:v '
.$video_bit_rates.'k '.$user_frames_path.'f'.$frame_no.'.mp4';
//echo $command;
exec($command,$output,$status);
return;
}
The call is made as below
$this->mergeImagesToVideo($start_second,$user_directory,$video_bit_rates,$output,$status);
if($status!=0){
echo "some thing went wrong in merging all images to create a video <br>";
}
Now When check bit rates through ffmpeg command output is like as below
ffmpeg -i f1.mp4
This command shows 7303 kb/s
While I right click the image and show properties, it shows 7295 kbps
Which is the correct one ?? Not getting correct line..
Thanks in advance.

using ffmpeg exe in php

and am wanting to use ffmpeg as command line direct on windows 7 x64, I tried adding the extension, but unfortunately it did not work for some unknown reason, but the executable worked perfectly, I'm using the exec command, the only problem is that does not work with direct lines, I have to create a file. bat to run the program. this is my doubt.
$a = exec('\b.bat');
if ($a)
{
echo "Success"."\n";
print $a;
}else {
echo "No good"."\n";
print $a;
}
b.bat
ffmpeg -i video.flv -an -ss 00:00:16 -an -r 1 -vframes 1 -y %a.jpg
already tried several alternatives but the only one that worked was with the. bat
$a = exec('\windows/system32/ffmpeg.exe ffmpeg -i video.flv -an -ss 00:00:16 -an -r 1 -vframes 1 -y %a.jpg');
Make sure you include the full path to your .bat file and also make sure to include the full path to ffmpeg.exe in your .bat file.
Well, I am also working on this ffmpeg file to convert my movies and stuff my idea is for people to watch movies for free anyway here is what I use to convert my movies, have a note that when the ffmpeg.exe is not in the same folder than the movies then you will need to give a full path or use of ../ to go up a folder and / to a folder for example:
$input_path= $_FILES["file"]["tmp_name"];
$output_result= "movies/" . basename($_FILES["file"]["name"]).".flv";
exec("FFMPEG.EXE -i '$input_path' -s 900x400 -r 29.97 -b 1024k -ar 22050 -ab 50k -ac 1 '$output_result'");
there I am using scale and rateframe as well as bitrate and sound quality so my input and output I gave them in a variable basically that command you can use as is just give a value to those variables $input and $output. Please note that I am barely working out the idea too so far this is what I have come out with please do not expect excellent results since we are using microsoft windows the results may vary from version to version and you may have to use the command in administrator mode I hope this helps you my email is support#web2021.com if anything let me know man.

Converting a video to flv format by using ffmpeg is working in local but not working in server

I want to convert a video to flv format. I am using ffmpeg to convert the video. I am using following code.
exec("C:/wamp/www/newtip/ffmpeg/ffmpeg -i C:/wamp/www/newtip/ffmpeg/videos/".$name." -ar 22050 -ab 32 -f flv -s 320x240 C:/wamp/www/newtip/ffmpeg/players/".$name_s.".flv");
It is working correctly in the local system. But in the server it is not working correctly.
In the server i changed the code as below
exec("http://www.mydomain.com/newtip/ffmpeg/ffmpeg -i http://www.mydomain.com/newtip/ffmpeg/videos/".$name." -ar 22050 -ab 32 -f flv -s 320x240 http://www.mydomain.com/newtip/ffmpeg/players/".$name_s.".flv");
In local I have given the Source path as C:/wamp/www/newtip/ But in server i have given the path as http://www.mydomain.com/newtip/ .I think in the server the path is wrong. Can anybody tell me how to give the path in the server?
Try to specify the correct directory and also specify the document root with:
$path = $_SERVER['DOCUMENT_ROOT'] . "your required folder path here";
Make sure that:
Folder has required permissions set, chmod to 755
exec is not disabled.
You are confusing paths and urls. In place of http://www.mydomain.com/newtip/ffmpeg/ffmpeg, use the full path (something like... /home/username/mydomain.com/newtip/ffmpeg/ffmpeg). As Sarfraz recommends, you can use $_SERVER['DOCUMENT_ROOT'] to find the root for your path.
exec() runs on PHP which runs on the server, so use the path as if you're truly running it on the server with the local paths.
i had exact the same problem, it turned out to be the codec.
-acodec mp3 is working on local but the server required -acodec libmp3lame

Categories