PHP Extract audio from video


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.

By : dawnerd

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.

You need to do some Win32 interop to achieve that effect. Depending on whether you are using Winforms or WPF, the way you hook to the message processing differs (I don't remember Winforms, so I'll give all examples for WPF). But in both cases, you need to:

  1. Intercept the creation of the window and modify the window styles and extended styles. In WPF you need to inherit from HwndSource and modify the HwndSourceParameters in order to achieve this. You need WS_OVERLAPPEDWINDOW, WS_CLIPSIBLINGS and WS_VISIBLE for regular style and WS_EX_WINDOWEDGE and WS_EX_APPWINDOW extended styles.

  2. Add a message handler throught he HwndSource parameters HwndSourceHook.

  3. In the message proc added through the hook in step two, you need to process several messages:

    • WM_NCACTIVATE - to change the painting of the title when the app is activated or not
    • WM_NCCALCSIZE - to return to the OS that you don't have non-client areas
    • WM_NCPAINT - in general you need to invaldate the window rect only here, the WPF will take care of the actual painting)
    • WM_NCHITTEST - to process the moving of the window, minimizing and maximizing.
  4. Once you do the above, your client area where WPF is going to paint your visual tree is going to span the whole area of the window. You will need to add the "non-cliet" visuals so that your application looks like a regular application to the user.

  5. You might need several more messages:

    • WM_THEMECHANGED if you want to change your "non-client" area painting to be consistent with the OS theme
    • WM_DWMCOMPOSITIONCHANGED if you want to extend glass and get the standard OS NC-glass painting when glass is enabled and switch to your custom logic when glass is not.
  6. You might want to look at the Win32 Theme APIs if you want go get the standard Win32 assets for borders, caption, close, mininmize and maximize buttons to use in your 'non-client" area.

  7. If you want to extend Glass into your window, you can look at:

    • DwmExtendFrameIntoClientArea - to get the standard glass NC-area
    • DwmDefWindowProc - to get the desktop manager to paint Glass and the standard NC controls
    • DwmIsCompositionEnabled - to determine if Glass is enabled; you can use the above two APIs only when Glass is enabled. If Glass is not enabled, you need to do your own drawing of the NC area.

You can find the proper C# definitions of all messages, styles and corresponding Win32 APIs you need on P/Invoke.

You could also achieve similar effect by using standard WPF window with a WindowStyle=none. However, there will be some differences between the behavior of the desktop towards your app and other apps; most obvious of them is that you won't be able to stack or tile your window by right-clicking on the taskbar.

You can also look into some third-party components that enable some of this functionality. I have not used any (as you can see, I am not scared of Win32 interop :-)), so I can't recommend you any particular.

This video can help you solving your question :)
By: admin