I am working on a php code as shown below where I am converting mp4 files into mp3 using system command ffmpeg (in the case statement below).
<?php
$mp4_files = preg_grep('~\.(mp4)$~', scandir($src_dir));
foreach ($mp4_files as $f)
{
$parts = pathinfo($f);
switch ($parts['extension'])
{
case 'mp4' :
$filePath = $src_dir . DS . $f;
system('ffmpeg -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination_dir . DS . $parts['filename'] . '.mp3', $result); // Through this command conversion happens.
}
}
$mp3_files = preg_grep('/^([^.])/', scandir($destination_dir));
?>
After conversion, mp3 files goes into destination_dir. If new mp4 file arrives in $src_dir, the conversion usually happen on refresh of a page.
Once the conversion is complete, I am parsing everything into table as shown below:
<table>
<tr>
<th style="width:8%; text-align:center;">House Number</th>
<th style="width:8%; text-align:center;">MP4 Name</th>
<th style="width:8%; text-align:center;" >Action/Status</th>
</tr>
<?php
$mp4_files = array_values($mp4_files);
$mp3_files = array_values($mp3_files);
foreach ($programs as $key => $program) {
$file = $mp4_files[$key];
$file2 = $mp3_files[$key]; // file2 is in mp3 folder
?>
<tr>
<td style="width:5%; text-align:center;"><span style="border: 1px solid black; padding:5px;"><?php echo basename($file, ".mp4"); ?></span></td> <!-- House Number -->
<td style="width:5%; text-align:center;"><span style="border: 1px solid black; padding:5px;"><?php echo basename($file); ?></span></td> <!-- MP4 Name -->
<td style="width:5%; text-align:center;"><button style="width:90px;" type="button" class="btn btn-outline-primary">Go</button</td> <!-- Go Button -->
</tr>
<?php } ?>
</table>
Problem Statement:
I am wondering what changes I should make in the php code above that on click of a Go button, conversion of individual mp4 into mp3 happen.
On clicking of Go button, individual mp3 file (from an mp4) belonging to an individual row should go inside destination directory ($destination_dir).
The best way is to use XMLHttpRequest with better example here AJAX - Server Response
Create a javascript function like this :
Add somewhere something to handle the response of the server as you want; example:
Modify the button to call the javascript function
onclick="go('<?php echo $key; ?>', this); return false;"
:Take the time to learn how works Ajax call, it's really important to communicate with the server if you don't use form
You can use JQuery but it's better without ;)
Edit
Using form, you can do this:
Edit :
Replace
$key
by the House Numberbasename($file, ".mp4")
and the
page.php
oryour_encoder.php
as you want for an Ajax call :If you use
form
, you have to:check if
$_POST['key']
existsdo the encoding if key exists
send your new html table.
Edit :
I know this adapted from your preview question but this code is "uncorrect", it works, no problem, but it can be optimized like this :
from...
... to