PHP Script for FFMPEG conversion - php

Not been programming for long, so go easy.
Basically I'm attempting to write a PHP script that'll be able to automatically convert a folder full of .mp4's for me, and make them .flv's. This will be done for my entire TV collection (which is quite large) to allow for streaming over my local network within browsers.
I've currently got the following code:
// Save video files into an array
$vid = glob("../../*.mp4");
// Open text file in write mode
$fp=fopen("test.txt","w+");
// Write contents of array to file
foreach($vid as $key => $value){
fwrite($fp,$value."\n");
}
This is taking all of the mp4 files within the folder, and saving their filenames into a text file. The next step I need to take is to read each line, one by one, and perform an FFMPEG command on them until each line has been completed.
Any idea how I can achieve this? I know I'll have to use shell_exec somehow, but I've never used this before, and I'm a little bit stuck as to what to do next.
Thanks for any help

Here is the ffmpeg command line to convert all the mp4 files in a directory to flv files
for i in *.mp4; do ffmpeg -i $i -ar 44100 $i.flv; done
Here it is in php
// Change directory to the directory of the videos
chdir('../..');
// Run the command
exec('for i in *.mp4; do ffmpeg -i $i -ar 44100 $i.flv; done');

Related

How to mix/merge 2 mp3 files in PHP?

I need a tool to join 2 mp3 files in 1.
The first file ( background ) will be some sound ( music, submitted by user ),
second file will be a machine (google) speaking text, which was submitted ( by user ).
In output I need a single mp3 file, with background music and speaking robot text playing together.
I can't really find any PHP standalone solution like some library or something like, only shell commands atc.
So are there some libraries?
or there's some unique shell command which works on all OS to combine files?
Or how do I complete the task?
Based off of this question, you should be able to install FFMPEG onto your server (hopefully not a shared host?) and use
//Reduce background volume, assuming it's input2.mp3
shell_exec('ffmpeg -i input2.mp3 -af "volume=0.3" backround.wav');
//Merge the two
shell_exec('ffmpeg -y -i input1.mp3 -i background.wav -filter_complex amerge -c:a libmp3lame -q:a 4 output.mp3');
Then you can simply serve up the output.mp3 file. If this is being performed by PHP running under apache (or other web host, instead of CLI) you'll need to make sure your www-data user has read access to the input files, and write access to the output directory.
All your temporary output, like the background, should be saved as .wav, and only converted back to .mp3 for the final output. Recompressing the audio at each step may result in a very poor final output.
I am making assumptions about how FFMPEG works here, so you may need to consult the documentation for it in order to build a functioning or more efficient set of commands.
You can simply do this
file_put_contents('combined.mp3',
file_get_contents('file1.mp3') .
file_get_contents('file2.mp3'));

FFMPEG PHP - Converting gif to mpeg

I'd like to create a simple script that converts gifs to mpegs using FFMPEG PHP. The script works when using FFMPEG in terminal. When I use a similar script using FFMEG PHP and host it on my domain, the script converts the gif to an mpeg, but only converts one frame of it. This is strange because the FFMPEG line of code that is responsible for the conversion is essentially the same line used previously in terminal. I've made sure that my web host (cirtex) has FFMPEG installed in their server. Also, I made sure to edit my php.ini file for FFMPEG use.
The script I created consists of two parts - uploader.php and uploader_02.php.
uploader.php is a simple submit form, where the user uploads and submits a gif.
uploader_02.php receives the gif and copies it over to another directory on the server. Then, the script applies the FFMPEG conversion to the saved gif.
Here's are the lines of php that are responsible for the conversion:
<?php
// ffmpeg
$ffmpeg = "/usr/bin/ffmpeg";
$videoFile = "test_videos/" . $name;
$output = "test_videos/instagram.mpg";
$cmd = "$ffmpeg -i $videoFile -vb 5M -y $output";
exec($cmd);
?>
For some reason, only one frame of the gif is converted into mpeg format. Not sure what is causing this problem.
Any information regarding this issue will help out a lot.
According to people online the best way to accomplish this task is to convert the gif into a series of images using imagemagic then put it back together using FFMPEG
exec('convert test.gif test%05d.jpg');
exec('ffmpeg -r 5 -i test%05d.jpg -y -an test.mpg');
Here is anther question that relates to this one Animated gif to avi on linux
Timing between frames would be the issue that I could see arising from this though.

Parsing Thumbcache in PHP

I always wondered if it was possible to parse the thumbs db files located in Windows 7 in:
C:\Users\%userdata%\AppData\Local\Microsoft\Windows\Explorer
In Windows XP they used to be located in each folder, but I assume I would have to traverse these to find the directory I want, etc. I'm aware there are ways to generate thumbnails using ffmpeg and such, but want to find a way in PHP to parse that db file since Windows has already generated thumbs for me. It's not in plain text (which I was hoping for).
you could use a parser like vinetto via php's exec
I gave up trying to do parse these out and instead used ffmpeg to generate thumbs like this. The system call takes the 10th frame of the all the mp4 videos in my recordings folder and saves it as a 320x240 image and as the filename.jpg. I had a bunch of videos so I had to increase the max execution time of PHP to handle it.
foreach (glob("F:\\Recordings\\*.mp4") as $filename) {
$path = pathinfo($filename);
system("c:\\ffmpeg\\bin\\ffmpeg.exe -itsoffset -10 -i \"$filename\" -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320x240 \"".$path['filename'].".jpg\"");
}

Create Video File using PHP

I have scenario for creating a video files using diff. assets like images, audio file.
What I want to do is, Find audio files from the particular folder and set it as background music, and fetch images from particular folder and show those images one by one.
So basically I have images and audio files and I want create a video file using those assets using PHP.
Can any one please suggest the start up point for this? Have done image capture from video and converting the video using Ffmpeg so I have think of Ffmpeg but, I think it will not allow to create a video.
ffmpeg will allow you to create videos from still images.
For example, to create a video with a 10fps frame rate, from images 001.jpg .... 999.jpg:
ffmpeg -r 10 -b 1800 -i %03d.jpg test1800.mp4
You can mux streams (audio and video) like (add relevant codec options for bitrate, etc)
ffmpeg -i video.mp4 -i audio.wav -map 1.1 -map 2.1 output.mp4
I'm not going to go into more detail, as ffmpeg is a pain (args change in incompatible ways between versions, and depending on how it was compiled), and finding a rate/compression/resolution setting that is good for you is trial-and-error.
Under Ubuntu you need PHP5, ffmpeg and access to ffmpeg binary, this can be easy achieved with:
apt-get install php5 ffmpeg
I'm using this php function to generate an mp4 video from one gif image and one mp3 file, both source image and output video files are 720x576 pixels:
function mix_video($audio_file, $img_file, $video_file) {
$mix = "ffmpeg -loop_input -i " . $img_file . " -i " . $audio_file . " -vcodec mpeg4 -s 720x576 -b 10k -r 1 -acodec copy -shortest " . $video_file;
exec($mix);
}
example use:
$audio_file = "/path/to/mp3-file";
$img_file = "/path/to/img-file";
$video_file = "/path/to/video-file";
mix_video($audio_file, $img_file, $video_file);
Phil's solution is the correct approach. Work out what needs to be done by documenting yourself on ffmpeg (it's messy, and I can't blame him for not wanting to do your homework), and then call it from within php.
Cursory googling reveals php wrappers (e.g. http://ffmpeg-php.sourceforge.net/). If none are recent/complete enough, stick to issuing the needed shell commands from php.
ImageMagick convert -delay 100 -quality 75 photo1.jpg photo2.jpg movie.mpg
Or
http://dvd-slideshow.sourceforge.net/wiki/Main_Page

mp3 to flv on the server side how to?

I googled it for a long time, all I found was the ffmpeg php api, and a site called mp32tube .. now I was to have the exact same functionality of mp32tube I want to give my users the ability to upload an mp3 add a picture, then compile an FLV on the server that contains the picture and the mp3...
the rest I can do, like uploading the video to youtube, it's simple with their own API ..
can anyone please guide me to something that automatically does this on my server? (a centos powered VPS)
thank you very much.
Rami
You should read FAQ first.
http://www.ffmpeg.org/faq.html#SEC14
then to add sound you may try;
ffmpeg -ar 22050 -ab 128k -i song.mp3 -i videoHaveNoSound.flv VideoWithSound.flv
Let me know the result.
in addition:
you findout a shorter way;
ffmpeg -r 12 -b 1800 -i img.jpg -i yourSound.mp3 -acodec copy outVideo.flv
There is no "automatic" way to do this. You'll have to have the user upload the 2 pieces of data, then run them through ffmpeg. There is tons of documentation for ffmpeg out there, just research the command you'll need to run, test it outside of php first, then once you get it working, implement it in your script. After its gone through ffmpeg, delete the files used for creation, and let them download the resulting flv.

Categories