PHP FFMPEG Example to Convert MP4 Video to MP3 Audio File Using HTML5 Form in Browser
Dec 21, 2025
Buy the full source code of the application here:
https://procodestore.com/index.php/product/php-ffmpeg-example-to-convert-mp4-video-to-mp3-audio-file-using-html5-form-in-browser/
Show More Show Less View Video Transcript
0:05
Uh hello friends, today in this tutorial
0:07
I will basically show you how we can
0:10
convert the MP4 video into MP3 and we
0:13
will be using PHP for that PHP script in
0:16
the browser and we will be using the
0:18
library FFMPPG which is an open-source
0:20
library for working with the video files
0:22
and audio files. We will try to
0:24
basically build this online converter
0:26
which converts your video files MP4 to
0:30
MP3. You can basically take any file
0:32
AVI, FLC, any extension of video file.
0:36
We will convert this into MP3 file. So
0:38
this is a video coming up. This is a
0:40
live stream and uh we will code it
0:43
together step by step explaining you
0:46
each instruction in detail. So let's go
0:48
to the demo of this application. So
0:51
guys, this is a basically the demo of
0:52
the application. We have video to audio
0:55
converter right here. And on your
0:57
screen, you can see that we have this
0:59
interface out there. We have the choose
1:02
file button where you will select your
1:03
MP4 file. And then
1:06
we have basically a convert to MP3
1:10
player button. So now if I select my
1:12
video file guys, so this is the video
1:14
file I'm selecting guys. If I open this
1:18
uh hello friends, today in this tutorial
1:19
I will be showing you that the problem
1:21
will be fixed. You just need to un so
1:23
you can see that the video is playing
1:25
and uh we now need to extract all the
1:28
audio from this video. So this is the
1:30
MP3. We need to extract the audio. So
1:34
what I will do, I will select this video
1:36
and you can see 1.MPP4 uh video file has
1:39
been selected. Now basically I will
1:42
click this button. As I click this
1:44
button guys, the ffmppg command will run
1:46
in the background and automatically you
1:48
will see my MP3 file has been downloaded
1:51
and now I can
1:53
>> Hello friends, today in this tutorial I
1:54
will be showing you that and here you
1:57
need facing this problem. So very simple
2:00
guys, we have extracted uh the sound
2:03
which was there in the video. So this is
2:06
a really simple PHP script guys. You can
2:09
uh execute in the browser. You just need
2:11
a VPS hosting for that if you want to
2:13
deploy this application [snorts] and uh
2:16
you need this software called as ffmpg.
2:19
So this is basically the software that
2:21
we are using guys
2:23
to make this application. So ffmpg if
2:25
you don't know ffmppg it's a open-source
2:28
project way back started it was round
2:31
about in the '90s. So it is used by many
2:34
famous companies such as YouTube for
2:37
their video processing and Facebook as
2:40
well. So we will use this library. This
2:43
is where we can download this library.
2:45
Simply click the download button. And
2:47
this is a simple command guys if you
2:49
want to convert a MP4 to AVI. So you can
2:53
convert video from one format to another
2:55
or audio one format to another. We will
2:58
use a command in PHP execute it in the
3:00
command line. So once your ffmpg is
3:04
installed guys you just need to write
3:05
ffmppg. I have already installed it. So
3:08
the command is very simple ffmpg- i and
3:12
here we need to give the input file. So
3:14
video.mpp4
3:15
and then the output file. Let's suppose
3:17
output.mpp3.
3:19
That's all. So this is the actual
3:21
command guys which runs in the browser.
3:24
So where basically it takes this video
3:26
file and it converts it into
3:28
output.mpp3. It extracts the audio. So
3:31
this is a command that we will use it.
3:34
And how we can basically run the PHP
3:37
script. For that you need this exam
3:39
control panel. This is a special kind of
3:41
software which runs your PHP script in
3:44
the browser. So just install this Apache
3:46
control panel and uh start your Apache
3:50
server
3:51
and after that guys we will basically
3:53
write our PHP script. So let's start
3:55
building this PHP script.
3:58
So first of all guys just make this
4:00
index dot PHP file right here in the
4:03
root directory and now we will write our
4:05
PHP script guys. So for that we will
4:07
first of all have
4:09
a simple form
4:13
and this form basically will contain a
4:17
title which is MP4 to MP3 converter.
4:22
Close the title. So you have seen many
4:26
websites guys in the internet which
4:29
allows you to convert the MP4 to MP3. So
4:32
this is exactly I'm teaching you in the
4:34
video how to do this in the browser
4:37
itself. If you need the full script guys
4:39
the link is given in the description of
4:41
the video. You can check out the full
4:42
source code
4:45
and then we have the form here
4:51
and here guys what I will do guys I will
4:54
write my HTML code in a separate file.
4:56
So what I will do index html and I will
4:59
call this as process dotphp.
5:03
So just create this process dot php and
5:06
I'm doing it because for the formatting
5:08
because you can write a basic HTML file
5:11
right here and right here we will
5:14
basically have a form which will make a
5:17
post request to method will be post and
5:20
encoding type multiart form data. So
5:23
these three parameters are required. So
5:25
this will redirect the PHP script to
5:27
this convert dot.php. We are making the
5:29
post request like this. So after this
5:33
guys we will have a label and this label
5:35
will contain we will simply say to the
5:38
user that you need to select a video
5:40
file
5:42
MP4. That's all. After that we will have
5:46
input type file and name is equal to
5:50
video name parameter and we will only
5:53
accept MP4 files and this is required.
5:56
You can even accept AVI files FLC
5:59
totally up to you but in this video we
6:02
will only take MP4
6:05
button type is submit and here we will
6:08
simply say that convert to MP3 that's
6:10
all. So if you now ex refresh your
6:13
browser guys, you will have this
6:15
interface convert to MP3. If you click
6:19
it, we need to select this video file.
6:22
So here you will see not found. We
6:24
haven't basically created convert dot
6:26
PHP. So just create this convert.
6:30
Sorry, this needs to be process dot PHP.
6:32
So just create process dot PHP. So up
6:35
till now guys we have created our form
6:38
and inside the form
6:40
we created a simple form. Now we need to
6:43
write the PHP code.
6:47
So now we will write the process.php
6:50
script guys. So here we will have the
6:52
PHP tag
6:54
and right inside this we will have a if
6:56
condition. So we will simply check in
6:58
the server. So this basically tells
7:02
request method. So we are checking that
7:04
if it's a post request or a get get
7:06
request. So we can use this if condition
7:09
for that. So it simply uh see in this
7:12
PHP script whether it's a post request
7:15
or it's a get request. So we have put
7:17
this post here. That's why this will
7:19
evaluate to true. In this we will again
7:22
compare if the files are passed or not.
7:26
We can do this using the files array.
7:29
And uh we need to give the name
7:30
attribute. Here we have given this name
7:32
attribute right here. So this name
7:35
attribute needs to be the same. So after
7:38
this guys we have this uh temporary
7:40
location which is there temp name. This
7:43
simply means we are simply checking
7:47
if the video is there or not.
7:50
Just sorry I think I put
7:54
so here we will again say
7:57
dollar files
8:01
video
8:03
error. So if it is equal to zero triple
8:06
equal to is the condition. So this these
8:09
two conditions are if if these two
8:11
condition evaluate to true
8:14
in this case
8:17
just remove this
8:22
I think uh sorry this needs to close
8:26
right here. Yeah. So in this if
8:29
condition guys first of all this means
8:31
that if the video file is present or not
8:33
we have if we have selected a valid file
8:36
or not. The second condition is that if
8:38
the video doesn't contain any sort of
8:40
validation error. If both these
8:43
condition are met in that case we will
8:45
get a true condition. So in that we will
8:47
basically get the temporary path of the
8:50
file. So this temporary path guys it is
8:53
stored inside the files array. So we can
8:56
access the temporary location of the
8:58
file using temp name. So this will be
9:01
the temporary location. We can echo out
9:03
this uh in the browser itself. If you
9:06
see guys, if we select our file and uh
9:11
convert this, you will see that this is
9:13
a temporary location guys. In every PHP
9:15
script, whenever we try to upload a
9:18
file, first of all, the file is uploaded
9:19
at a temporary location. So in this
9:22
case, the temporary location is this
9:24
temporary location. We don't need to
9:26
upload the file. We can access the file
9:27
using the temporary location like this.
9:30
So this is the advantage. And now using
9:32
this temporary location guys, what we
9:34
can do? We can simply say that uh
9:38
[snorts] let me just turn off the text
9:40
of volume. So we can simply say output
9:42
file path. So where this file will be
9:45
stored. So it will be stored in the uh
9:47
root directory. I will give the name as
9:49
output.mpp3. So this will be stored in
9:51
the root directory itself. And uh after
9:54
that guys we will now be executing the
9:57
ffmppg command. So this execute method
10:01
guys it is available in PHP by default
10:04
which allows you to execute any uh
10:07
command in the
10:10
command line using PHP.
10:14
So for for that you need to have fmpg
10:16
installed. We will write here ffmpg dash
10:19
i and then we will provide uh
10:22
[snorts] so
10:24
this needs to be double quotes sorry
10:27
ffmppg dash i and then
10:34
video video
10:38
temp path
10:41
and then dash cv h264. Let me explain
10:46
you guys what does it is doing. So this
10:49
basically it's a encoding that we are
10:51
doing right here. This is making the
10:54
faster. As you see guys ffmpg takes a
10:57
lot of time in a processing videos. But
10:59
if you have a decent amount of GPU GPU
11:02
stands for graphical processing unit.
11:04
It's a subset of CPU as well. If you
11:09
play games you have a GPU. So in my case
11:11
I have a GPU which is running. If I open
11:14
task manager, you can even go to task
11:16
manager and just see your uh if you have
11:21
a dedicated GPU, you can use it. So
11:24
basically I have this Nvidia GeForce
11:26
GTX. So in order to use this dedicated
11:30
GPU inside our FFMPG command, we just
11:33
need to give this - CV this option. So
11:36
this will use the dedicated GPU. So this
11:38
will make the processing little bit
11:40
faster.
11:42
After that we will say VN and we will
11:45
apply the codec which is this is not
11:48
necessary but uh we are applying the
11:50
codec which is MP3 codec. After that we
11:53
are giving the file name output file
11:55
path
11:57
output file path
12:01
to greater than and add one that's all.
12:06
And uh then it takes the
12:09
output guys where the it you need to
12:12
save it and then it takes the return
12:14
code that's all.
12:18
So [snorts]
12:20
that's all guys it takes and now we can
12:23
basically compare if the return code is
12:26
equal to zero in that case there is no
12:29
kind of error take place. So in that
12:32
case in the else if any sort of error
12:35
take place then we can echo out a
12:36
message that your command is not perfect
12:39
conversion failed. So if the command is
12:41
successful guys then we will download
12:43
the file as an attachment download the
12:46
file. So let me first of all look if the
12:49
command is successful if it is
12:50
generating the output.mpp3 file or not.
12:53
If I basically go to my application and
12:55
just choose the video file click convert
12:59
to MP3. So now guys it will take some
13:01
time and now if you check your
13:04
output.mpp3 file is generated and it is
13:06
stored inside your root directory. So
13:08
this is you can simply play this file.
13:12
So if I play this file you will Hello
13:14
friends with the fixed properties and
13:15
search for a. So now we need to simply
13:18
download this file in the browser using
13:20
PHP. So we need to simply set some
13:22
headers for this. So we can set the
13:24
header content type
13:27
application / octate
13:32
stream.
13:34
So this is basically the header that we
13:36
need to set guys in order to download
13:38
the files in the browser using PHP. The
13:42
second header we need to set is the
13:44
content disposition
13:48
and this will be as attachment because
13:51
we need to download it as an attachment.
13:53
So file name will be equal to slash
14:01
we need to concatenate the file name
14:03
which is we'll use the base name
14:06
function and we will pass the file name
14:08
which is output file path
14:11
and then we can concatenate once again
14:13
this
14:16
slash that's
14:25
This needs to be double quotes. Sorry.
14:27
Put a semicolon. That's all. So after
14:30
that guys, uh we need to read the file.
14:33
There is a read file method in PHP. So
14:36
we need to read the file from the path
14:38
which is output file path. And then we
14:41
need to delete this file. So we have a
14:43
unlink method inside PHP guys which will
14:45
automatically delete the file after the
14:47
file has been downloaded. That's all. So
14:50
now we need to exit out from the
14:52
program. So we have the exit command.
14:54
That's all. This is the overall PHP
14:57
script guys. So if you refresh the
14:59
browser,
15:01
select my video file and click convert
15:03
to MP3. So it is saying conversion fade.
15:07
So you can see uh I think some kind of a
15:10
problem is there.
15:17
I think this problem is coming because
15:20
of this code. So the link is given guys
15:23
in the description. I can just write
15:25
this code once again.
15:37
Conversion failed. Uh this code is
15:40
correct. I think we need to delet uh
15:42
this uh this file needs to be deleted
15:45
first of all. That's the problem.
15:48
Yeah. So, choose file and uh convert to
15:52
MP3. So, it is saying process.php is not
15:55
found.
15:58
Oh, sorry. We deleted that file. So,
16:00
this needs to be deleted. [music]
16:09
So now guys it basically is converting
16:12
here and automatically you will see your
16:14
file is downloaded output.mpp3. So you
16:16
can now play this file. Hello friends,
16:18
today in this tutorial. So one basically
16:20
flaw which is there inside the script
16:22
guys. You can basically make this uh
16:24
path dynamic in nature. Let's suppose uh
16:28
you have some kind of uh
16:31
we can search here how we can uh use uh
16:35
date dot now in PHP. So basically have
16:42
uh dynamic names coming. So what we can
16:45
say here
16:48
if you want to get
16:52
the current date
17:01
uh random string in PHP. So
17:06
generating a random string. So for this
17:08
what we can say
17:12
we can basically
17:20
yeah sorry
17:27
so this is uh you can basically do like
17:29
this guys. So
17:32
you can generate a random string right
17:33
here
17:37
and then we can concatenate
17:40
this random string in the file name. So
17:43
right here you can do string dot
17:48
and then the actual file name is let's
17:52
suppose output.mpp3.
17:54
So we can concatenate these two
17:56
variables like this.
18:00
So now every time uh if you see
18:07
it is saying
18:12
let me just concatenate like this
18:21
or we can do like this also file name. I
18:24
missed the semicolon. So what it will do
18:26
guys it will automatically regenerate
18:28
the file name as dynamic. So every time
18:31
the file name will be dynamic. So now if
18:34
you basically go to the application
18:38
click convert to mp3.
18:45
So file is downloaded. You will see the
18:47
file name is dynamic in nature. So now
18:50
this there will be no problem. You can
18:52
select this file. Click convert to mp3.
18:55
So I'm using GPU guys. So that's why my
18:58
conversion is a little bit faster. Here
19:00
you can see that. So this is basically
19:02
the PHP script guys how we can
19:05
dynamically convert MP4 to MP3 video
19:07
file. Hopefully you will like this
19:09
video. If you like this video then
19:11
please hit that like button, subscribe
19:13
the channel as well. All the source code
19:15
is given in the description of the video
19:17
and I will be seeing you in the next
19:20
live stream.
