PHP Extract audio from video - php

I need a way to extract the audio from some video (in PHP). I have the video streaming in from YouTube, so I would really like it if it were on the fly streaming, not I have to save it to a temp directory and process it there (though that is acceptable.) Thanks, Isaac Waller
Edit: to be more specific, I have a MP4 and I want it to be a MP3.

You're going to want to use something like ffmpeg and call it using php's exec command. If you look around in the docs, I'm sure you can figure out what flag to use to only get the audio.
I've used this app before on a project for live transcoding of video, works like a charm. Just make sure your server has it correctly installed.

Mplayer should do this for you, and there are libraries and codecs that you can call (PHP supports C libraries) which will strip the video from the AV stream on the fly.
Given that you're targeting youttube your job is a bit easier because they use a very small subset of file encodings.
If you take the time to learn the format, you can very easily remove the video stream on the fly and return only the audio stream.
If you give a little more information, such as what you're encoding it to, or where it's going to end up we may be able to help more specifically.
-Adam

Related

Audio watermarking with Ruby, PHP or Python

I'm working on a project where I need to post-process a bunch of audio files in various formats.
Firstly, the files need to be converted to .WAV format.
Secondly, depending on their length, I need to insert a short audible watermark at certain intervals in each of the new .WAV files.
The first part is easy, using the LAME encoder cli.
The second part is where it get's difficult - I've tried a few method with both LAME and FFmpeg, but can't seem get it working.
The script is running as a cron job in the background, so full cli access is available.
If possible, it would be great if someone could point me to an example script/gem or class that does this in some related way.
This gets complicated. You need to actually mix the audio, which to my knowledge, isn't possible with FFMPEG. The other problem you're going to have is loss of quality if you take an MP3, convert it to WAV so you can work with it, and re-encode it back to MP3.
I think you can use Sox for this: http://sox.sourceforge.net/
Use FFMPEG first to decode the audio to WAV, adjusting sample rate and bit depth as necessary.
Then, call out to soxmix: http://linux.die.net/man/1/soxmix
If you're ready to take the Python route, I would suggest SciPy, which can read WAV files into NumPy arrays:
from scipy.io import wavfile
fs, data = wavfile.read(filename)
(the official documentation contains details).
Sounds can be conveniently manipulated through the array manipulation routines of NumPy.
scipy.io.wavfile can then write the file back to the WAV format.
SciPy and NumPy are general scientific data tools. More music-centric Python modules can be found on the official web site.

Video file splitting on the fly

I'm downloading videos from a 3rd party server (mostly flv, mp4) using php script, after download I'm cutting video at 1:30 mins - now it's ready to be played. Is it possible to download only part (1-2 mins) of the video instead of whole file and still being able to play it (the way it's now it's simply wasting bandwidth, CPU...)?
This is very hard to do, as it requires your script to understand MP4 or FLV. However, this may help you (please note that it still requires ffmpeg, FLVTools2 and Mencode to be installed).
Cutting a flv or mp4 involves some index updating, looking for a possible cutting point etc. Building that in php seems overkill. Look into some Tools that can download and cut in one step and call those from your php. FFMpeg and MPlayer may be able to do that.

RAW images and PHP

I would like to extract a thumbnail from a RAW image file, like Canon's .CR2 or Nikon's .NEF. I've understood that this can somehow be done with ImageMagick, but haven't grasped if it's possible through the PHP wrapper.
Are there any good solutions? Preferably using the built in thumbnail for speed.
Yep, iMagick (the php version of ImageMagick) can handle these extensions: http://www.imagemagick.org/script/formats.php
Here's a great set of tuts that got me going with Imagick. The owner responded to a few of my questions quickly, and despite a bit of a language barrier was able to easily get me through my hurdles
As an aside, I've begun using Gallery to do image admin. No need to worry about thumbnailing, uploading, etc....it's all automatic. Then on the front end I can do jquery magic (getting photos via php query from the gallery database tables) to make it look really good.
Likely, if PHP's imagemagick libraries are to support this, they would be drawing from some functionality exposed through imagemagick's 'identify' command line tool (as the tool would be itself exposing functionality in the imagemagick libraries). Looking at the documentation for this tool, it doesn't look good. If you tried running identify -verbose, theoretically, the thumbnail information would appear in there somewhere, perhaps as an encoded value. Try it yourself: if it does, maybe you could possibly further extract the information returned from identify, either through the imagemagick functions in PHP (though I don't see any past the Exif libraries which only work on JPEG), or by scraping the return of a PHP system call to the identify tool.
Either way, doesn't look likely.
Benjamin Horn has submitted a complete example about reading the requested data and even saving it locally for later use.
Check this out:
https://benjaminhorn.io/code/extracting-thumbnails-from-camera-raw-files-cr2-and-nef-with-php/

Use PHP or Flash to extract a frame from a video

The quick question: is there a pure PHP library that can extract a frame from the video to use as the thumbnail? I guess the answer is "no" but let me know if I am wrong :-) ffmpeg wrappers will not go, since I want this feature to work on most hostings without special PHP modules or binaries to launch.
There might also be some tool to do this on the client side with flash for example... This also acceptable. I'll just have to upload the flash-generated content to the server.
This script claims to be able to do it just by using GD

Get the dimensions of a H.264 MP4

Does anybody know a ready-made, reliable way to tell the dimensions (width x height) of a MP4 encoded using the H.264 codec without ffmpeg or similar extensions, in pure PHP?
Thanks for all the answers folks. The bounty is running out and I will not have time to check the offered solutions before it does. I will accept the solution that I feel has the greatest likelihood to work.
getID3 is pure php and extracts an amazing amount of information from media files of all sorts. It will depend on what encoded your file in the first place as to what metadata is available and how reliable it is. getID3 has a nice demo page with lots of different file types. I tried to post more links but as a newbie I only get one.
It sounds like http://code.google.com/p/php-mp4info/ might be your answer. It reads MP4's but it doesn't mention anything about H.264.
also, what OS are you using?
What comes to mind:
mediainfo a huge project with GUI, but also has a CLI
mp4info (part of the seemingly defunct mp4mpeg project) is almost perfect for this
ffmpeg although this is overkill for the task. then again, you very well may need it for other tasks
ffmpeg and php: http://www.lampdeveloper.co.uk/linux/detecting-a-videos-dimensions-using-php-and-ffmpeg.html
php-reader is a full implementation of the ISO 14496 done in pure PHP. You can use this library to read all of the boxes which the mp4 consist of, like the moov atom containing metadata about the file.
Native PHP does not support anything like this, ffmpeg is only one library that come on my mind.

Categories