在我的javascript代码中使用ffmpeg
我必须在我的项目中使用ffmpeg来转换来自wav t mp3、pcm和vox的音频文件。我的项目是javascript/php构建的。有人能告诉我如何使用ffmpeg吗?我已经读了很多书,现在它取决于命令行。但是在哪里编写这些命令行呢?如果有人能给我演示如何使用它,我将不胜感激 我找到了这个函数,但不知道如何给它转换文件在我的javascript代码中使用ffmpeg,javascript,ffmpeg,Javascript,Ffmpeg,我必须在我的项目中使用ffmpeg来转换来自wav t mp3、pcm和vox的音频文件。我的项目是javascript/php构建的。有人能告诉我如何使用ffmpeg吗?我已经读了很多书,现在它取决于命令行。但是在哪里编写这些命令行呢?如果有人能给我演示如何使用它,我将不胜感激 我找到了这个函数,但不知道如何给它转换文件 function getFFMPEGWorker() { // regexps for extracting time from ffmpeg logs
function getFFMPEGWorker() {
// regexps for extracting time from ffmpeg logs
var durationRegexp = /Duration: (.*?), /
var timeRegexp = /time=(.*?) /;
var duration;
var ffmpegWorker = new Worker('worker.js');
var durationLine;
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
$('#loading').hide();
} else if (message.type == "stdout") {
console.log(message.data);
} else if (message.type == "stderr") {
console.log(message.data);
// try to extract duration
if (durationRegexp.exec(message.data)) {
duration = timeToSeconds(durationRegexp.exec(message.data)[1]);
}
// try to extract time
if (timeRegexp.exec(message.data)) {
var time = timeToSeconds(timeRegexp.exec(message.data)[1]);
if (duration) {
$("#progress").text("Progress: " + Math.floor(time / duration * 100) + "%");
$("#progress").show();
}
}
} else if (message.type == "done") {
var code = message.data.code;
console.log(message.data);
var outFileNames = Object.keys(message.data.outputFiles);
console.log(outFileNames);
if (code == 0 && outFileNames.length) {
var outFileName = outFileNames[0];
var outFileBuffer = message.data.outputFiles[outFileName];
var src = window.URL.createObjectURL(new Blob([outFileBuffer]));
$("#downloadLink").attr('href', src);
$("#download").show();
} else {
$("#error").show();
}
$("#converting").hide();
$("#progress").hide();
}
}, false);
return ffmpegWorker;
}
// create ffmpeg worker
var ffmpegWorker = getFFMPEGWorker();
var ffmpegRunning = false;
$('#convert').click(function() {
// terminate existing worker
if (ffmpegRunning) {
ffmpegWorker.terminate();
ffmpegWorker = getFFMPEGWorker();
}
ffmpegRunning = true;
// display converting animation
$("#converting").show();
$("#error").hide();
// hide download div
$("#download").hide();
// change download file name
var fileNameExt = fileName.substr(fileName.lastIndexOf('.') + 1);
var outFileName = fileName.substr(0, fileName.lastIndexOf('.')) + "." + getOutFormat();
$("#downloadLink").attr("download", outFileName);
$("#downloadLink").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
}
]
});
});
不能使用JavaScript直接控制ffmpeg。您必须对服务器进行ajax调用,使用该调用运行ffmpeg。您的意思是ffmpeg行命令应该在服务器端?好的,谢谢,这很有用,但通过我的搜索,我发现了上述函数。它有用吗?我如何给它文件进行转换?或者我最好使用ajax?