Could someone please advise on what my options are when it comes to video type conversion in PHP. I have just discovered that our system uses something called ffmpeg. This isn't a problem but when a website is transferred it does create a problem as this absolute command breaks websites.
system ('/usr/bin/ffmpeg -i '.$video.' -y -f flv -qmin 5 -qmax 9 -ar 22050 '.DATA_DIR . $new_filename);
As you can see, a transferred website would require to have this path on their host and most don't.
So the question is this. I need to replace this. Is there some sort of PHP script or API that will make this work?
Is there any option other than pinging our own servers with the video and our video sending back the video in the new format?
Thanks.
Is there some sort of PHP script or API that will make this work?
No. This is well beyond the scope of PHP. FFMPeg is indeed the household name for video conversion - the best thing is probably to stick with that.
One workaround would be to set up a conversion service script on a server that supports ffmpeg, and all the other web sites sending the material to that server (if file sizes and traffic rates allow.)
There is a php ffmpeg library, but you can just install linux version of ffmpeg in your application and change this directory
No, there are no native PHP alternatives to ffmpeg for transcoding videos, so you must work around that somehow.
As mentioned before, there is no PHP extension that does video conversion (the ffmpeg-php extension can not convert videos) - you will have to call something not in PHP to get the video conversion proper done.
I see two possible problems on the "transferred websites":
If it is simply a path problem: look at this page for how to call ffmpeg - you should not have to include the "/usr/bin/" part in your command.
If the problem is that you cannot install ffmpeg on the transferred websites, you can do two things, depending on which drawback is more acceptable:
You may convert all videos to .flv beforehand, and serve them either from the transferred websites or from your own servers. Use that method for videos that will be watched often, or whose converted version will be watched often.
The transferred websites will point to the video flux from your own servers, that will handle the on-the-fly conversion. Do that for videos that will not be watched as often.
Feel free to install ffmpeg into your home directory on your hosting provider; many, if not most, hosts allow you to install programs in addition to scripts.
However, please do not place this code on a production system. Or, any computer you care about. If some smartass uploads a video named
Puppy;/bin/rm -rf /;.avi
then you can kiss all your data goodbye. If it is named:
Puppy;`nc -l 11111`;.avi
then they have a shell they can use for whatever they please.
Related
The thing is that the client wants to upload a pdf with images as a way of batch processing multiple images at once.
I already looked around and out of the box PHP can't read PDF's.
What are my alternatives?
I already know the host has not installed imageMagick or any pdf library and the exec function is disabled. That's basicly leaving me with nothing to work with, I guess?
Does anyone know if there is an online service that can do this, with an api of sorts?
thanks in adv
AFAIK, there is no PHP module to do it. There is a command line tool, pdfimages (part of xpdf). For reference, here's how that works:
pdfimages -j source.pdf image
Which will extract all images from source.pdf as image-000.jpg, image-001.jpg, etc. Note the output format is always Jpeg.
Possible Options
Being a command line tool, you need exec (or system, passthru, any of the command executing functions built into PHP). As your environment doesn't have that, I see four options:
Beg that exec be turned on for you (your hosting provider can limit what you can exec to a single command)
Change the design -- how about a ZIP upload?
Roll your own, using the source code of pdfimages as a model
Let pdfimages do the heavy lifting, by running it on a remote host you do control
Regarding #3, rolling your own, I don't think rolling your own, to solve a very narrow definition of requirements, would be too difficult. I seem to recall that the image boundaries in PDF are well defined: just read in the file to a boundary, cut to the end of the boundary, base64_decode, and write to a file -- repeat. However, that may be too much...
If rolling your own is too complicated, then option #4 is kind of like what Joel Spolsky describes for working with complicated Excel objects (see the numbered list under the bold heading "Let Office do the heavy work for you").
Find a cheap hosting environment (eg Amazon EC2) that let's you exec and curl
Install pdfimages
Write a PHP script that takes a URL to a PDF, curl opens that PDF, writes it to disk, passes it to pdfimages, then returns the URL to the resulting images.
An example exchange could look like this:
GET http://www.cheaphost.com/pdfimages.php?extract=http://www.limitedhost.com/path/to/uploaded.pdf
Content-type: text/html
<html>
<body>
<ul>
<li>http://www.cheaphost.com/pdfimages.php?retrieve=ab9895v/image-000.jpg</li>
<li>http://www.cheaphost.com/pdfimages.php?retrieve=ab9895v/image-001.jpg</li>
</ul>
</body>
</html>
So your single pdfimages.php script (running on the host with the exec functionality) can both extract images, and give you access to the extracted images. When extracting, it reads a PDF you tell it, runs pdfimages on it, and gives you back a list of URL to call to retrieve the extracted images. When retrieving, it just gives you back a straight image.
You would need to deal with cleanup, perhaps the thing to do would be to delete the image after retrieval. You would also need to handle security -- don't know what's in these images, but the content might need to be wrapped in SSL and other precautions taken.
You can use pdfimages and install it this way:
apt install poppler-utils
Then use it this way to get all the images as PNG files:
pdfimages -j mypdf.pdf image -png
Images will be placed in the same folder under image-000.png, image-001.png, etc.
There are many options available, including some to change the output format, more information here.
I hope this helps!
I have a web page, which (among other things) needs to extract a specific frame from a user-uploaded video. The user seeks to a particular part of a .mp4 in the player, then clicks a button, and an ajax call gets fired off to a php script which takes the .mp4, and the exact time from the video, and uses that to extract a "thumbnail" frame.
My current solution is using the php exec command:
exec("ffmpeg -i $videoPath -ss $timeOffset -vframes 1 $jpgOutputPath");
...which works just great, except it's as slow as molasses. My guess is that ffmpeg is a little too much for the job, and I might be able to do better by utilizing the underlying libraries or something... however I have zero idea how to do that.
Ideally I don't want to have to install anything that requires a real "installation process"... i.e., dropping an executable into the folder with my web app is fine, but I'd rather not have to actually run an installer. Also, the solution should be able to run on mac, linux and windows (though linux is the top priority).
What can I do to speed this process up?
Thanks.
Of course you could code up some C/C++ and link to -lav*, basically creating a simplified version of ffmpeg just for extracting frames, and maybe even do it as a php extension (also I wouldn't run it as the same user, let alone in the same process). But the result is very unlikely to be faster, because you would only avoid some forking and setup overhead, but your likely problem is actually the decoding, which would still be the same.
Instead, you should first look into using ffmpeg in fast seeking mode (or fast/accurate hybrid mode). Their wiki states about fast seeking:
The -ss parameter needs to be specified before -i:
ffmpeg -ss 00:03:00 -i Underworld.Awakening.avi -frames:v 1 out1.jpg
This example will produce one image frame (out1.jpg) somewhere around
the third minute from the beginning of the movie. The input will be
parsed using keyframes, which is very fast. The drawback is that it
will also finish the seeking at some keyframe, not necessarily located
at specified time (00:03:00), so the seeking will not be as accurate
as expected.
Fast seeking is less accurate, but a damn lot faster, as ffmpeg will not actually need to decode (most of) the movie during the seek, while fast/accurate hybrid mode is good compromise. Read the wiki page for all available options.
Edit 14/06/10:
As of FFmpeg 2.1, when transcoding with ffmpeg (i.e. not stream copying), -ss is now accurate even when used as an input option. Previous behavior can be restored with the -noaccurate_seek option.
(source)
So with 2.1+, "hybrid" seeking shouldn't be required anymore for accurate results when it comes to re-encodes (and saving to .jpeg is a re-encode). It is enough to do the usual fast seeking (-ss ... -i ...) instead of slow seeking (-i ... -ss ...).
In C/C++ using the ffmpeg libraries directly (as nmaier mentioned but didn't provide resources for) : I had this same problem and it took me a lot of work, but I have now implemented a frame extraction function in C++ directly using the ffmpeg libraries (within the QT Framework, but it should still help for plain C++).
See the function QImage Video::ffmpegLib_captureAt(const int percent, const int ofDuration) in my project Video simili duplicate cleaner Video.cpp file, it should be a self explanatory example : https://github.com/theophanemayaud/video-simili-duplicate-cleaner/blob/master/QtProject/app/video.cpp
Is there any opeensource library available for php that we can use to encode mp3 without paying fee. I will be using mp3 encoding on my website. If not can you tell me how much fee we have to pay for recording ten minutes sound clip.
Legally, no, mp3 is patent encumbered so you have to pay to use it. That's not to say that libraries don't exist, just look at the long list of products that exist to rip a CD into mp3 encoded files. However, if you are putting it on a public website, you really should do this the proper way.
You can use the ffmpeg library for encoding, from here. You need to install an extension (probably "ffmpeg") in your Apache, whether it be in your local machine or in the server. For the latter, you can ask the server administrator to do the installation, but provide specific information as to which version of the extension you want to get it installed.
Note: There are differences with this extension version & the PHP version.
Hope it helps.
Have a look at mencoder and ffmpeg . both can encode mp3, you can execute them
on command line from your php script.
But as #BMitch said, try to be sure about patent and fees issues.
Hello I am wondering what processes involves converting files online...What programming languages are required?Basically I am wondering how are the files on scribd,issu,slideshare converted...
Thank you..!
There is a media processing library called FFMpeg. It will read, write and convert pretty much any common media format. It can resize, crop, scale, resample, etc all the files it can load, meaning videos can be shrunk in size, or whatever you might want to do!
The great thing is, if you have PHP and FFMpeg installed on a server, you can use PHP's exec() command to convert/modify/save videos.
A little note: beware of using exec() with any commands that are influence by what is sent from the server, like frame sizes, etc - hackers can latch onto them and mess your server up!
To get round the problem of pethetic people (script kiddies, etc), try ffmpeg-php. I don't know how popular this is on web hosts, but I haven't seen it many places in the wild.
James
EDIT
Unfortunately not. FFMpeg is primarily for video/audio conversion. However, there is a program called pdf2swf that should do the trick. The man page for it is here.
The only concern about pdf2swf is whether your web host actually has it installed on their server. If you have a VPN or a dedicated server, that's no problem, but if you're on shared hosting, and have no access to the root filesystem, this is where issues arise - you can't install pdf2swf if you don't have it.
I am looking for a way to accomplish the following: A user programs some drum loops in flash and is somehow able to download or save an mp3 of the loop.
I thought that the pattern could be composted on the server side. Then a link to the file is sent to the user.
I had thought there might be something like imageGD or imageMagick for sound out there?
Also, if it is possible for flash to generate something that the user could save on the fly? That would work too, but I am unaware of any such functionality in flash.
I suppose something could be done in processing, but I am totally unfamiliar with that.
How might this sort of thing might be accomplished?
Take a look at SoX.
SoX is a cross-platform (Windows, Linux, MacOS X, etc.) command line utility that can convert various formats of computer audio files in to other formats. It can also apply various effects to these sound files, and, as an added bonus, SoX can play and record audio files on most platforms.
If you have control over your server environment, I suppose you could use ffmpeg to do the job.
In your PHP code:
exec(escapeshellcmd("/path/to/ffmpeg -i /path/to/audiofile1.mp3 -i /path/to/audiofile2.mp3 -itsoffset 10 -i /path/to/audiofile3.mp3 -itsoffset 20 -acodec mp3 /path/to/outputfile.mp3"),$output,$status);
Notice that -itsoffset is the offset in seconds you want the audio file to be placed in. So this is not ideal if you want very minute control over the timing, but I don't know if you need that.
Check out this MP3 class.
I'm using it for a project that was deployed just today.
It can read MP3's, extract a part of them, and merge files, among other things, except it doesn't overlap sounds.
Provided you already have those sound pieces (drums, guitar, etc) and that you don't need overlapping, it seems this is what you're looking for.