返回 shell_exec 作为字符串 PHP 用 soundtouch/soundstrech 检测 BPM
Returning shell_exec as string PHP detecting BPM with soundtouch/soundstrech
我正在开发一个 php 函数,用于将 .wav 上传到服务器 (以及转换为 mp3 和创建波形图像 png),并且在函数我希望它使用 soundtouch / soundstrech 来检测 B.P.M。 (每分钟节拍)。我知道这不是最准确的,但就我的目的而言,这将是我所需要的。
我能够得到 B.P.M。使用 soundtouch / soundstrech 的 .wav 文件和 test.php 文件中的 ffmpeg 使用 deven 的 php-bpm-detect wrapper 但当我尝试集成时它在我的 PHP 函数中它 return 是 B.P.M。作为零。
我想知道是否有一种更简单的方法可以从以下 shell exec 中以字符串形式获取 bpm,而无需使用单独的 php 库?
我想执行此操作并将其 return 作为字符串:
$song_bpm = shell_exec('soundstretch ' . $file_path . ' -bpm');
test.php(这有效并且 return 是正确的 bpm:)
<?php
require "class.bpm.php";
$wavfile = "38a2819c20.wav";
$bpm_detect = new bpm_detect($wavfile);
$test = $bpm_detect->detectBPM();
echo ' bpm of ' . $wavfile . ' is: ' . $test . ' ';
?>
PHP 功能:(returns bpm 为零)
function upload_a_sound($user_id, $file_temp, $file_extn, $name, $uploader, $keywords) {
$timecode = substr(md5(time()), 0, 10);
$mp3name = 'beats/' . $timecode . '.mp3';
$file_path = 'beats/' . $timecode . '.wav';
move_uploaded_file($file_temp, $file_path);
shell_exec('ffmpeg -i ' . $file_path . ' -vn -ar 44100 -ac 2 -ab 192k -f mp3 ' . $mp3name . '');
require ('classAudioFile.php'); // This creates a spectogram .png file of .wav
$AF = new AudioFile;
$AF->loadFile($file_path);
$AF->visual_width=200;
$AF->visual_height=200;
$AF->visual_graph_color="#c491db";
$AF->visual_background_color="#000000";
$AF->visual_grid=false;
$AF->visual_border=false;
$AF->visual_graph_mode=0;
$AF->getVisualization ('images/song/' . $timecode . '.png');
$imageloc = 'images/song/' . $timecode . '.png';
require ('class.bpm.php'); //Deseven's class to get bpm,
$bpm_detect = new bpm_detect($file_path);
$song_bpm = $bpm_detect->detectBPM(); //when used here this returns 0
mysql_query("INSERT INTO `content` VALUES ('', '', '$name', '$uploader', '$keywords', '$file_path', '$imageloc', '$mp3name', '$song_bpm')"); // I will update this to mysqli soon, for now it works
}
我还发现 this 有效,但当我将其集成到我的函数中时却无效:
// create new files, because we don't want to override the old files
$wavFile = $filename . ".wav";
$bpmFile = $filename . ".bpm";
//convert to wav file with ffmpeg
$exec = "ffmpeg -loglevel quiet -i \"" . $filename . "\" -ar 32000 -ac 1 \"" . $wavFile . "\"";
$output = shell_exec($exec);
// now execute soundstretch with the newly generated wav file, write the result into a file
$exec = "soundstretch \"" . $wavFile . "\" -bpm 2> " . $bpmFile;
shell_exec($exec);
// read and parse the file
$output = file_get_contents($bpmFile);
preg_match_all("!(?:^|(?<=\s))[0-9]*\.?[0-9](?=\s|$)!is", $output, $match);
// don't forget to delete the new generated files
unlink($wavFile);
unlink($bpmFile);
// here we have the bpm
echo $match[0][2];
我已经更新了 my class 所以它现在支持绝对和相对路径。
以及直接的解决方案:
exec('soundstretch "test.wav" -bpm 2>&1',$average_bpm);
foreach ($average_bpm as $line) {
if (strpos($line,"Detected BPM rate") !== false) {
$line = explode(" ",$line);
$average_bpm = round($line[3]);
break;
}
}
echo $average_bpm;
请记住,如果出现任何问题,$average_bpm
将包含错误。
我正在开发一个 php 函数,用于将 .wav 上传到服务器 (以及转换为 mp3 和创建波形图像 png),并且在函数我希望它使用 soundtouch / soundstrech 来检测 B.P.M。 (每分钟节拍)。我知道这不是最准确的,但就我的目的而言,这将是我所需要的。
我能够得到 B.P.M。使用 soundtouch / soundstrech 的 .wav 文件和 test.php 文件中的 ffmpeg 使用 deven 的 php-bpm-detect wrapper 但当我尝试集成时它在我的 PHP 函数中它 return 是 B.P.M。作为零。
我想知道是否有一种更简单的方法可以从以下 shell exec 中以字符串形式获取 bpm,而无需使用单独的 php 库?
我想执行此操作并将其 return 作为字符串:
$song_bpm = shell_exec('soundstretch ' . $file_path . ' -bpm');
test.php(这有效并且 return 是正确的 bpm:)
<?php
require "class.bpm.php";
$wavfile = "38a2819c20.wav";
$bpm_detect = new bpm_detect($wavfile);
$test = $bpm_detect->detectBPM();
echo ' bpm of ' . $wavfile . ' is: ' . $test . ' ';
?>
PHP 功能:(returns bpm 为零)
function upload_a_sound($user_id, $file_temp, $file_extn, $name, $uploader, $keywords) {
$timecode = substr(md5(time()), 0, 10);
$mp3name = 'beats/' . $timecode . '.mp3';
$file_path = 'beats/' . $timecode . '.wav';
move_uploaded_file($file_temp, $file_path);
shell_exec('ffmpeg -i ' . $file_path . ' -vn -ar 44100 -ac 2 -ab 192k -f mp3 ' . $mp3name . '');
require ('classAudioFile.php'); // This creates a spectogram .png file of .wav
$AF = new AudioFile;
$AF->loadFile($file_path);
$AF->visual_width=200;
$AF->visual_height=200;
$AF->visual_graph_color="#c491db";
$AF->visual_background_color="#000000";
$AF->visual_grid=false;
$AF->visual_border=false;
$AF->visual_graph_mode=0;
$AF->getVisualization ('images/song/' . $timecode . '.png');
$imageloc = 'images/song/' . $timecode . '.png';
require ('class.bpm.php'); //Deseven's class to get bpm,
$bpm_detect = new bpm_detect($file_path);
$song_bpm = $bpm_detect->detectBPM(); //when used here this returns 0
mysql_query("INSERT INTO `content` VALUES ('', '', '$name', '$uploader', '$keywords', '$file_path', '$imageloc', '$mp3name', '$song_bpm')"); // I will update this to mysqli soon, for now it works
}
我还发现 this 有效,但当我将其集成到我的函数中时却无效:
// create new files, because we don't want to override the old files
$wavFile = $filename . ".wav";
$bpmFile = $filename . ".bpm";
//convert to wav file with ffmpeg
$exec = "ffmpeg -loglevel quiet -i \"" . $filename . "\" -ar 32000 -ac 1 \"" . $wavFile . "\"";
$output = shell_exec($exec);
// now execute soundstretch with the newly generated wav file, write the result into a file
$exec = "soundstretch \"" . $wavFile . "\" -bpm 2> " . $bpmFile;
shell_exec($exec);
// read and parse the file
$output = file_get_contents($bpmFile);
preg_match_all("!(?:^|(?<=\s))[0-9]*\.?[0-9](?=\s|$)!is", $output, $match);
// don't forget to delete the new generated files
unlink($wavFile);
unlink($bpmFile);
// here we have the bpm
echo $match[0][2];
我已经更新了 my class 所以它现在支持绝对和相对路径。
以及直接的解决方案:
exec('soundstretch "test.wav" -bpm 2>&1',$average_bpm);
foreach ($average_bpm as $line) {
if (strpos($line,"Detected BPM rate") !== false) {
$line = explode(" ",$line);
$average_bpm = round($line[3]);
break;
}
}
echo $average_bpm;
请记住,如果出现任何问题,$average_bpm
将包含错误。