0

I'm building a voice recorder program using html5 I have managed so far to record the voice and save the audio file as .wav I also can convert that file to a selected file format using ffmpeg. what I want to do is to let the user choose the format before recording so that when saving the audio file will be converted directly using ffmpeg into the selected format.I have been looking for demos for weeks and can't find anything if someone can help me with a demo please. thank you in advance this is my javascript code for uploading the saved audio as .wav in a folder named upload, and the fmpeg conversion, i'm missing the call of the file saved in the folder please:

function handleWAV(blob) {

if (currentEditedSoundIndex !== -1) {
$('#inFile2 tr:nth-child(' + (currentEditedSoundIndex + 1) + ')').remove();

}

var url = URL.createObjectURL(blob);

var li = document.createElement('li');
var au = document.createElement('audio');
var hf = document.createElement('a');

au.controls = true;
au.src = url;
hf.href = url;
hf.download = 'audio_recording_' + new Date().getTime() + '.wav';
hf.innerHTML = hf.download;
li.appendChild(au);
li.appendChild(hf);
inFile2.appendChild(li);


fileName=hf.download;

var reader = new FileReader();
reader.onload = function(event){
    var fd = new FormData();
    var Name = encodeURIComponent('audio_recording_' + new Date().getTime() + '.wav');
    console.log("name = " + Name);
    fd.append('fname', Name);
    fd.append('data', event.target.result);
    $.ajax({
        type: 'POST',
        url: 'upload.php',
        data: fd,
        processData: false,
        contentType: false,
        success: function(data){
            //console.log(data);
        }
    });
};      
reader.readAsDataURL(blob);



 var fileBuffer;



 // create ffmpeg worker
 function getFFMPEGWorker() {


     var ffmpegWorker = new Worker('worker.js');

     ffmpegWorker.addEventListener('message', function(event) {
         var message = event.data;
         console.log(message.type);
         if (message.type === "ready" && window.File && window.FileList && window.FileReader) {
             // script loaded, hide loader

         } else if (message.type == "stdout") {
             console.log(message.data);
         } else if (message.type == "stderr") {
             console.log(message.data);
         } else if (message.type == "done") {
             var code = message.data.code;
             console.log(code);
             console.log(message.data);
             var outFileNames = Object.keys(message.data.outputFiles);

             console.log(outFileNames);
             if (code == 0 && outFileNames.length) {
                 var outFileName = outFileNames[0];
                 console.log(outFileName);
                 var outFileBuffer = message.data.outputFiles[outFileName];
                 console.log(outFileBuffer);
                 var src = url;
                 console.log(url);
                 $("#downloadLink2").attr('href', src);
                 $("#download2").show();
             } else {
                 $("#error").show();
             }

         }
     }, false);
     return ffmpegWorker;
 }

 // create ffmpeg worker
 var ffmpegWorker = getFFMPEGWorker();
 var ffmpegRunning = false;
 if (ffmpegRunning) {
     ffmpegWorker.terminate();
     ffmpegWorker = getFFMPEGWorker();
 }
 ffmpegRunning = true;



 // hide download div
 $("#download2").hide();

 // change download file name
 var fileNameExt = fileName.substr(fileName.lastIndexOf('.') + 1);

 var outFileName = fileName.substr(0, fileName.lastIndexOf('.')) + "." + getOutFormat();

 $("#downloadLink2").attr("download2", outFileName);
 $("#downloadLink2").text(outFileName);

 var arguments = [];
 arguments.push("-i");
 arguments.push(fileName);

 arguments.push("-b:a");
 arguments.push(getBitrate());

 switch (getOutFormat()) {
     case "mp3":
         arguments.push("-acodec");
         arguments.push("libmp3lame");
         arguments.push("out.mp3");
         break;

     case "wma":
         arguments.push("-acodec");
         arguments.push("wmav1");
         arguments.push("out.asf");
         break;

     case "pcm":
         arguments.push("-f");
         arguments.push("s16le");
         arguments.push("-acodec");
         arguments.push("pcm_s16le");
         arguments.push("out.pcm");
 }

 ffmpegWorker.postMessage({
     type: "command",
     arguments: arguments,
     files: [
         {
             "name": fileName,
             "buffer": fileBuffer
         }
     ]
 });


function getOutFormat() {
 return $('input[name=format]:checked').val();
}

function getBitrate() {
 return $('input[name=bitrate]:checked').val();
}

function readInputFile(file) {


 // load file content
 var reader = new FileReader();
 reader.onload = function(e) {

     fileName = file.name;
     console.log(fileName);
     fileBuffer = e.target.result;
 }
 reader.readAsArrayBuffer(file);

}



function handleFileSelect(event) {
 var files = event.target.files; // FileList object
 console.log(files);
 // files is a FileList of File objects. display first file name
 file = files[0];
 console.log(file);
 if (file) {

     readInputFile(file);
     console.log(file);

 }
 } 

 // setup input file listeners
 el=document.getElementById('inFile2');

 el.addEventListener('change',handleFileSelect, true);

 }

note that this code was taken from a demo where the user upload the file to be converted by browisng it, I just want to cancel the browsing process and upload the saved file directly

user3789242
  • 105
  • 2
  • 12

2 Answers2

0

You have to save your audio format first. Than allow user to select common formats for audio and apply it to ffmpeg CLI string:

// ffmpeg -i input.wav -f mp3 output.mp3
ffmpeg -i input.wav -f {$userFormat} output.{$userFormat}

More about arguments here

Justinas
  • 41,402
  • 5
  • 66
  • 96
  • I want the user to select the format first then record. what I'm trying to do is to save the audio file as .wav in the back code, upload it to a folder and call that audio and convert it using ffmpeg due to selection.but my problem i'm stuck on calling that file again to convert it,I know the path but don't know how to call it.http://stackoverflow.com/questions/24735941/select-a-file-without-browsing-using-javascript – user3789242 Jul 15 '14 at 06:24
  • @user3789242 so instead of `input.wav` insert path to that file: `/usr/bin/generatedAudio/inputSomeRandomNumbers.wav`. Your link leads to JS question and you are working with PHP (you can't call FFMpeg directly from js). – Justinas Jul 15 '14 at 07:02
0

I believe what you are trying to do can be achieved using an ajax request. First you have to create a input for the user, maybe a FORM and assign the audio and file format type in the form and submit it to the server and convert it.

Here is a link Uploading recorded blob via AJAX that can help you with the ajax request.

Community
  • 1
  • 1
DC-
  • 797
  • 9
  • 14
  • as I said earlier I managed to upload the save file as .wav in a folder (using ajax) what I want is how to retrieve that data in order to do the conversion – user3789242 Jul 15 '14 at 07:13
  • one way is to convert the audio in the server, when user clicks download, you can send the format to the server, convert and send the file back to user. The other way will be to record the audio with the desired format. [Here](http://nusofthq.com/blog/recording-mp3-using-only-html5-and-javascript-recordmp3-js/) is a link explains on how to record mp3 format. You can look for libraries that can help you to get more formats. Also you can convert the file using javascript here is a [demo](http://bgrins.github.io/videoconverter.js/) to do it. – DC- Jul 15 '14 at 08:30