I'm working on an online experimenter which could record participants' audio from the browser. The audio data I get has an extension of .wemb, so I plan to use ffmpeg to convert it to .wav while I save the data.
I tried to use PHP's shell_exec but nothing happens when I run the scripts. Then I found that my echo and print_r also did not work. I'm new to PHP and javascript, so I''m really confused now.
Below are the relevant codes, I really appreciate it if you could help!
write_data.php:
<?php
$post_data = json_decode(file_get_contents('php://input'), true);
// the directory "data" must be writable by the server
$name = "../".$post_data['filename'];
$data = $post_data['filedata'];
// write the file to disk
file_put_contents($name, $data);
$INPUT = trim($name) . ".webm";
$OUTPUT = trim($name) . ".wav";
echo "start converting...";
// check if ffmprg is available
$ffmpeg = trim(shell_exec('which ffmpeg'));
print_r($ffmpeg);
// call ffmpeg
shell_exec("ffmpeg -i '$INPUT' -ac 1 -f wav '$OUTPUT' 2>&1 ");
?>
javascript:
saveData: function(fileName,format){
// save as json by default
if (!format){ format = 'json';}
// add extension to filename
fileName = `${fileName}.${format}`
// create saveData object using fetch
let saveData = [];
if (format == 'json') {
saveData = {
type: 'call-function',
async: true,
func: async function(done) {
let data = jsPsych.data.get().json();
const response = await fetch("../write_data.php", {
method: "POST",
headers: {
"content-type": "application/json"
},
body: JSON.stringify({ filename: fileName, filedata: data })
});
if (response.ok) {
const responseBody = await response.text();
done(responseBody);
}
}
}
} else {
saveData = {
type: 'call-function',
async: true,
func: async function(done) {
let data = jsPsych.data.get().csv();
const response = await fetch("../write_data.php", {
method: "POST",
headers: {
"content-type": "application/json"
},
body: JSON.stringify({ filename: fileName, filedata: data })
});
if (response.ok) {
const responseBody = await response.text();
done(responseBody);
}
}
}
}
return saveData;
},