![](/img/trans.png)
[英]How to trim a video by 4 fragments to 4 seconds using the PHP-FFMpeg?
[英]How to trim silence from audio with ffmpeg in php
我的主機沒有ffmpeg的silenceremove
過濾器,所以我試圖用silencedetect
刪除音頻文件的開始和結束silencedetect
然后剪切。 我正在嘗試用PHP編程。 此時,我甚至無法發現沉默。 我試過了:
define('UPLOAD_DIR', $_SERVER['DOCUMENT_ROOT'].'/audiofiles/');
$filein = UPLOAD_DIR . "music.ogg";
$fileout = UPLOAD_DIR . "music_no_silence.ogg";
$fileouttxt = UPLOAD_DIR . "music_log.txt";
$ffmpeg = "/usr/local/ffmpeg/bin/ffmpeg";
我首先嘗試將ffmpeg命令放入變量,然后嘗試訪問輸出但無法:
$output = shell_exec("$ffmpeg -i $filein -af silencedetect=n=-50dB:d=1");
echo $output;
然后我嘗試創建轉換的.txt日志,但.txt文件沒有顯示有關silencedetect的任何信息。 此外,我如何才能從.txt日志中以編程方式提取我需要的信息才能在php中使用它?
shell_exec("$ffmpeg -i $filein -af silencedetect=n=-50dB:d=1 $fileout 2> $fileouttxt");
我基本上試圖通過https://stackoverflow.com/a/25698675修改答案到php
編輯 :這是一種破解方式:
define('UPLOAD_DIR', $_SERVER['DOCUMENT_ROOT'].'/audiofiles/');
$filein = UPLOAD_DIR . "music.ogg";
$fileout1 = UPLOAD_DIR . "music_out1.ogg";
$fileout2 = UPLOAD_DIR . "music_out2.ogg";
$ffmpeg = "/usr/local/ffmpeg/bin/ffmpeg";
$command = "$ffmpeg -i $filein -filter_complex silencedetect=n=-10dB:d=0.5 -y $fileout1 2>&1";
$output = array();
exec($command,$output);
$searchword1 = 'silence_end';
$endmatches = array_filter($output, function($var) use ($searchword1) {return preg_match("/\b$searchword1\b/i", $var);});
$endmatches = array_values($endmatches);
$parts1 = explode(" ",$endmatches[0]);
$a = $parts1[4];
$a = $a-.5;
$searchword2 = 'silence_start';
$startmatches = array_filter($output, function($var) use ($searchword2) {return preg_match("/\b$searchword2\b/i", $var);});
$startmatches = array_values($startmatches);
$parts2 = explode(" ",end($startmatches));
$b = $parts2[4];
$b = $b+.5;
$c = $b-$a;
exec("$ffmpeg -i $fileout1 -ss $a -t $c -y $fileout2 && rm $fileout1");
PHP的shell_exec
返回寫入stdout
, ffmpeg
將消息寫入stderr
。 如果要使用shell_exec
,則必須使用2>&1
將stderr
輸出重定向到stdout
。
您還需要為ffmpeg
指定至少一個輸出文件,在本例中為/dev/null
。
set_time_limit(0);
$input_file = 'music.ogg';
$cmd = "ffmpeg -i {$input_file} -af silencedetect=n=-50dB:d=1 -f null /dev/null 2>&1";
$output = shell_exec($cmd);
if (preg_match_all("/^\[silencedetect\s[^]]+\]\s([^\n]+)/m", $output, $matches)) {
var_export($matches[1]);
}
另一種方法是使用proc_open
和stream_get_contents
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.