Add or extract audio from video – PHP & FFmpeg

In this article, we are going to teach you how you can add audio to a video. And also, how you can extract the audio from the video. We will be using PHP and FFmpeg for both purposes.

Add audio to video

Adding audio to a video will replace the video’s actual audio. You can use it to either replace the video’s audio. Or wanted to put some background music on any of your video. Each video has audio input streams which we can hear when we play the video. If you have ever watched any video that does not have any sound. It means the number of audio steams in that file is 0.

You can add any audio to your video by first removing the audio streams from video file. And then get the audio streams from audio file. And put them on the video file. We will be creating a separate file for output that will contain your video and new audio. Because we do not wanted to make any changes in the original file. You just needs to remember that the length of output file will be equal to the length of maximum media stream.

For example, if audio length is 10 seconds and video length is 15 seconds, the new output will be of 15 seconds. That is why it is a good practice to make the length of both files to be equal. If you have files of unequal lengths, you can always trim the video or audio to required length by following this tutorial. You can also try to keep audio streams from both (audio and video).

You may have seen it in some videos that has the actual sound of the video and they also have a very little, dim music in the background. These type of videos are best for explaining historical facts or world’s cultures etc.

Extract audio from video

Aside from adding music track, sometimes you want to extract an audio. You have a video song in your computer but cannot play that in music player. Because it is a video, you can convert that to audio and then listen that as audio file. As mentioned above, it will not affect your original video file. It will create a separate file with just audio in it. You are watching an action or thriller movie and you want to extract some of music track from it, you can first extract that specific part from video by following this tutorial and then convert that extracted video to audio.

Add music track to video

We will be using bootstrap darkly for layout and design, it will be included in the source files below. Below is the code that will create a simple form with 2 input fields. The first input field will be to select video where music track needs to be added. The second is the mp3 file which will be the music file that needs to be added in the background of selected video.

<link rel="stylesheet" href="bootstrap-darkly.min.css">

<div class="container" style="margin-top: 100px;">
    <div class="row">
        <div class="offset-md-4 col-md-4">
            <form method="POST" enctype="multipart/form-data">
                <div class="form-group">
                    <label>Select video</label>
                    <input type="file" name="video" class="form-control" required>
                </div>

                <div class="form-group">
                    <label>Select audio</label>
                    <input type="file" name="audio" class="form-control" required>
                </div>

                <input type="submit" class="btn btn-success" value="Add Audio">
            </form>
        </div>
    </div>
</div>

Now we need to process the form data and replace the video’s actual audio with a selected music track. Paste the following code at the top or bottom of your PHP file:

<?php

    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $video = $_FILES["video"]["tmp_name"];
        $audio = $_FILES["audio"]["tmp_name"];

        $command = "/usr/local/bin/ffmpeg -i " . $video . " -i " . $audio . " -c:v copy -map 0:v:0 -map 1:a:0 output.mp4";
        system($command);

        $command = "/usr/local/bin/ffmpeg -i " . $video . " original-audio.mp3";
        system($command);
    }

?>

Extracting audio

At this point, we have replaced the video’s actual audio with selected music. Now we wanted to extract the actual audio. This will also be useful if you wanted to convert any video song into mp3 file. This will not only save your memory but also helps in listening the song in your mp3 player or iPod, which otherwise you won’t be able to do. To extract audio from selected video you just needs to run another command right below your system function call.

$command = "/usr/local/bin/ffmpeg -i " . $video . " original-audio.mp3";
system($command);

This is the simplest command you will see related to FF mpeg, after telling the complete path of FF mpeg you just needs to tell the input video file and then the name of output audio file which we are naming as “original-audio.mp3”. This will create a separate mp3 file with the music of selected video. But if you want to convert the audio in some specific format, then you can use the following command:

$command = "/usr/local/bin/ffmpeg -i " . $video . " -b:a 192K -vn original-audio.mp3";
system($command);

This will convert the audio in 192 bitrate format. Change that value to any you want but the basic acceptable formats are 64K, 96K, 128K, 196K and 320K. 320K is the highest and best quality audio format today.

I have also attached all the source files below, make sure you download them and let us know if you face any issue.

[wpdm_package id=’154′]