Convert 720p Mp4 to 480p with php ffmpeg - php

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.

Related

PHP/FFMPEG - why does my video conversion result in empty file?

I'm trying to convert a video that lives on my server from a .webm to a .mp4.
shell_exec('ffmpeg -i file.webm output.mp4');
However, this results in an empty mp4 (0 bytes - or, sometimes, and very weirdly, 28 bytes.) The input file exists, and is 45MB.
This answer recommended explicitly copying the input codec, but same result.
shell_exec('ffmpeg -i file.webm -vcodec copy -acodec-copy output.mp4');
What am I doing wrong?
[ ===== EDIT ===== ]
After trying a few more things, inspired by the comments, I'm still having no joy converting to MP4.
It seems I'm running FFMPEG v.2.8.15. This seems quite a lot lower than the current release of 4~, however I only installed this a week or so ago so I'm not sure why this is the case and I don't know how to update it (I'm on WHM Cpanel.)
Anyway, my problem is NOT to do with pathing, because the same command works fine if I change .mp4 to .webm - I get a successfully converted .webm file.
But when I run:
$foo = shell_exec('ffmpeg -i file.webm -vcodec copy -acodec copy output3.mp4 -report');
...I get this FFMPEG log output.
I've also tried:
shell_exec('ffmpeg -fflags +genpts -i file.webm -r 24 KKK.mp4 -report');
...from this answer, and
shell_exec('ffmpeg -i file.webm -c:v copy III.mp4');
...from this article.
Both result in the same problem, i.e. an 0-bytes .mp4 file.
Problem
You're trying to mux VP9 video and Opus audio into the MP4 container. Currently, muxing Opus into the MP4 container is considered experimental and requires -strict experimental (or -strict -2), but your ffmpeg is too old to support that. Download a new version.
Solutions
Do not mux typical WebM formats (VP9 or VP8 + Opus or Vorbis) into MP4. You can re-encode it to H.264 (or H.265) with AAC audio if you want a more common set of formats for MP4:
ffmpeg -i input -c:v libx264 -c:a aac -movflags +faststart output.mp4
Or upgrade your ffmpeg and add the -strict experimental (or -strict -2) output option to your command if you know you want VP9 + Opus in MP4:
ffmpeg -i input -c copy -strict experimental -movflags +faststart output.mp4
Or don't re-mux into MP4 in the first place. HTML5 video supports VP9 + Opus in WebM so just use the original file if it is supported by your target browser(s).

PHP and FFMPEG advice/solution for a unknown issue

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.

How can I limit ffmpeg memory usage

I am using ffmpeg to convert a 1080p video to MP4 using this command in PHP.
$command = FFMPEG.' -i ' . $src . ' -sameq -strict -2 -vcodec libx264 -ar 22050 -y ' . $dest.'.mp4 2>&1';
exec($command,$output,$status);
The problem is that the process uses a lot of memory ~1600MB, which is not allowed by my server so ffmpeg gets terminated. Can I limit the memory usage of ffmpeg to about 600-700MB?
Any help is greatly appreciated...
It's not about PHP. It's about how to limit FFMPEG memory usage.
Short answer no!
Look here: http://hwdmediashare.co.uk/forum/27-development-customisation/54700-limit-ffmpeg-memory-usage
It would be the video codec which is mainly responsible for the high memory use.
So it's the encoder whose memory use needs to be addressed, not FFmpeg directly. I'm not sure how to fix x264's memory use, but I tried the newer x265 and in my case it's only using 1.6 GB, while libx264 was failing by asking more than the 2 GB memory limit (per process, on 32-bit systems).
So, what worked for me was to use:
ffmpeg -i input -pix_fmt yuv420p -c:v hevc -x265-params crf=23 out.mp4
(Omitting parameters to take care of the audio.)
But the general approach is to try other encoders. I was going to try mpeg4 and vp9 if x265 didn't work, maybe others. If none of that works, then further options include looking at the encoders' settings (though nothing obvious and directly relevant to memory use shows up):
ffmpeg -h encoder=mpeg4
Update: actually, it turned out that YouTube doesn't accept HEVC (aka H.265) yet (and it only let me know after the upload finished). So, like I suggested above, I went for VP9, doing a pilot run with the first 50 frames this time. I used settings similar to a guide I found (the Constant quality settings, though I should have used more of their suggested parameters):
ffmpeg.exe -i <input> -pix_fmt yuv420p -c:v libvpx-vp9 -pass 1 -b:v 0 -crf 20 -f webm pass1.webm
ffmpeg.exe -i <input> -pix_fmt yuv420p -c:v libvpx-vp9 -pass 2 -b:v 0 -crf 20 -f webm pass2.webm
(Note that pass1.webm will be almost empty.)
Also note that two passes are preferred whenever possible. It's better on all fronts, including faster encoding overall.
With these settings, a 73-second clip at 4K resolution took about 16 hours to encode — that's using one core, as I forgot to specify -threads. While slow, FFmpeg's memory use only went up to about 0.6 GB. The resulting file was 300 MB, and I can't see any loss of quality compared to uncompressed frames (so -crf 20 might have been a bit too low).
The truth is video encoding a CPU and memory intensive job. If you want to do it you need to give the requisite memory for it.
Since you want to convert a 1080p .mov to a .mp4 of same quality I am assuming you jsut want to change the format of the file and not reencode.
ffmpeg -i input.mov -acodec copy -vcodec copy out.mp4
will do it in a jiffy and without any of the memory overheads
If you want your audio codec only changed don't give the acodec part.
If the input video codec does not work for you [so copy is not an option] try doing mpeg4 encoding for vcodec. Cheaper to encode there but I cannot assure you it will fit in your memory requirements always.

How to create videos from images with php?

Let's say I have 10 images and I want to combine those images in a video like a slideshow.
For example I want to show each image for 5 seconds and then continue with next image for another 5 seconds.
If it's possible, it will be perfect to include music and some descriptive text too.
Is there a sample code for this may be with ffmpeg library ?
My first thought was to shell out to the ffmpeg command with something like this.
Creating a Video from Images
ffmpeg can be used to stitch several images together into a video.
There are many options, but the following example should be enough to
get started. It takes all images that have filenames of
XXXXX.morph.jpg, where X is numerical, and creates a video called
"output.mp4". The qscale option specifies the picture quality (1 is
the highest, and 32 is the lowest), and the "-r" option is used to
specify the number of frames per second.
ffmpeg -r 25 -qscale 2 -i %05d.morph.jpg output.mp4
(The website that this blurb was taken from is gone. Link
has been removed.)
Where 25 means 25 images per second. You could set this to 1 for a slight (1 sec) delay or use decimals, IE: 0.5 for a 2 second delay.
You can then combine a video and audio stream with something like this.
ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -b:a 128k final.mp4
Of course choose your appropriate codecs. If you want an mp4 use libx264 for video and aac (built into ffmpeg and no longer "experimental") for audio.
Just remember that if you choose to use a method like this that ffmpeg output goes, by default, to stderr for when you try to read it. It can be redirected to stdout if you prefer.
The first thing that came to mind for me was imagemagick. I've used it with PHP for a lot of image manipulation and I know it supports reading a decent amount of video formats and according to that link it supports writing to some too.
yes, ffmpeg is the right solution for you. i just recently made something similar - a video site with animated thumbnails. i used ffmpeg to put together images in an aminated gif. however, the output can be whatever you need... unfortunately, in my searches into this topic i have not found any sample code that would combine all the points you are after, so i suppose you will have to try manually with ffmpeg... in my project i used php video toolkit http://sourceforge.net/projects/phpvideotoolkit/ in some parts to make it a bit easier...
You can use blend effect with ffmpeg:
ffmpeg -framerate 20 \
-loop 1 -t 0.5 -i 1.jpg \
-loop 1 -t 0.5 -i 2.jpg \
-loop 1 -t 0.5 -i 3.jpg \
-loop 1 -t 0.5 -i 4.jpg \
-c:v libx264 \
-filter_complex " \
[1:v][0:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b1v]; \
[2:v][1:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b2v]; \
[3:v][2:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b3v]; \
[0:v][b1v][1:v][b2v][2:v][b3v][3:v]concat=n=7:v=1:a=0,format=yuv420p[v]" -map "[v]" out.mp4
You should check bellow link for more effect of ffmpeg :D
https://github.com/letungit90/ffmpeg_memo

how to convert series of jpegs to flv using imagemagik and php?

I have series of jpegs , i want to make flv or mpg from all the images . How can i do it with using imagemagik and php .
exec(convert image1.jpg image2.jpg one.flv) make blank flv
Well I would jump stright into using ffmpeg. You can also do it using ImageMagick; however the docs state you need ffmpeg installed, so why have the middleman?
I haven't tested this, fair warning.
/* cmd img series codec bitrate framerate optional -s WidthxHeight and output filename */
exec(ffmpeg -f image2 -i image%d.jpg -vcodec mpeg4 -b 800k -r 12 video.avi);
/* For Mpeg4 *
/*For FLV */
exec(ffmpeg -f image2 -i image%d.jpg -vcodec flv -b 800k -r 12 video.flv);
If you want to use the outdated mpeg2 or mpeg1 formats you can do that as well.
I would suggest connecting via ssh and testing these commands, and hopefully you have ffmpeg installed.
a ffmpeg -formats will show you which formats are supported:
See the docs:
http://ffmpeg.org/ffmpeg.html#Video-and-Audio-file-format-conversion
and this great answer which I stole various things from:
Image sequence to video quality

Categories