iT邦幫忙

1

PHP-FFMpeg問題

各位好:
想請教我想用FFMpeg來對影片作轉檔跟截圖
我的電腦:win 10 64 位元

我有抓PHP_FFMpeg.dll,放在C:\xampp\php\ext
再到CMD 下指令:composer require php-ffmpeg/php-ffmpeg
但他一直出現:
https://ithelp.ithome.com.tw/upload/images/20201231/20102983GrzeeN2LdV.png
但是我找不到其他的dll檔了....
有大大能提供給我載點嗎?謝謝
以下是我抄官網了sample code:

<?php
require '../../vendor/autoload.php';
 
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('video.mpg');
$video
    ->filters()
    ->resize(new FFMpeg\Coordinate\Dimension(320, 240))
    ->synchronize();
$video
    ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
    ->save('frame.jpg');
 
 
?>

顯示錯誤:

Fatal error: Uncaught Alchemy\BinaryDriver\Exception\ExecutableNotFoundException: Executable not found, proposed : avprobe, ffprobe in C:\vendor\alchemy\binary-driver\src\Alchemy\BinaryDriver\AbstractBinary.php:159 Stack trace: #0 C:\vendor\php-ffmpeg\php-ffmpeg\src\FFMpeg\Driver\FFProbeDriver.php(48): Alchemy\BinaryDriver\AbstractBinary::load(Array, NULL, Object(Alchemy\BinaryDriver\Configuration)) #1 C:\vendor\php-ffmpeg\php-ffmpeg\src\FFMpeg\FFProbe.php(226): FFMpeg\Driver\FFProbeDriver::create(Object(Alchemy\BinaryDriver\Configuration), NULL) #2 C:\vendor\php-ffmpeg\php-ffmpeg\src\FFMpeg\FFMpeg.php(132): FFMpeg\FFProbe::create(Array, NULL, Object(Doctrine\Common\Cache\ArrayCache)) #3 C:\xampp\htdocs\video.php(4): FFMpeg\FFMpeg::create() #4 {main} Next FFMpeg\Exception\ExecutableNotFoundException: Unable to load FFProbe in C:\vendor\php-ffmpeg\php-ffmpeg\src\FFMpeg\Driver\FFProbeDriver.php:50 Stack trace: #0 C:\vendor\php-ffmpeg\php-ffmpeg\src\FFMpeg\FFProbe.php(226): FFMpeg\Driver\FFProbeDriver::create(Object(Alchemy in C:\vendor\php-ffmpeg\php-ffmpeg\src\FFMpeg\Driver\FFProbeDriver.php on line 50

想請教各位該怎麼設定才行
謝謝大家~

附上參考網站:
https://github.com/PHP-FFMpeg/PHP-FFMpeg

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
淺水員
iT邦大師 6 級 ‧ 2020-12-31 16:18:13
最佳解答

剛剛看了一下官網的說明,他只有要求電腦有 ffmpeg 的執行檔
所以 ffmpeg 並不需要作為 php extension

我測試官網的範例是可行的,以下修改自 Basic Usage

<?php
require 'vendor/autoload.php';

//指定 ffmpeg 執行檔路徑
$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries'=>'d:\ffmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'd:\ffmpeg\bin\ffprobe.exe',
]);
$video = $ffmpeg->open('SampleVideo_1280x720_5mb.mp4');
$video
    ->filters()
    ->resize(new FFMpeg\Coordinate\Dimension(320, 240))
    ->synchronize();
$video
    ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
    ->save('frame.jpg');
//匯出 WebM 格式
$video->save(new FFMpeg\Format\Video\WebM(), 'export-webm.webm');

關於 ffmpeg 的安裝

直接從官網抓編譯好的壓縮檔,解壓縮就可。
裡面可以找到 bin 資料夾,內有 ffmpeg.exe 跟 ffprobe.exe

PS. php.ini 那邊不需要用到 ffmpeg 的 extension,記得刪除相關設定。

關於匯出的格式

範例中共匯出 x264、wmv、webm 幾種格式
目前測試只有 webm 可行
另外兩種輸出的錯誤如下

格式 錯誤訊息 原因
x264 Unknown encoder 'libfaac' ffmpeg 官方編譯版本預設不包含 libfaac,這可以自行編譯 ffmpeg 解決
wmv too many channels: got 6, need 2 or fewer. Error initializing output stream 0:1 -- Error while opening encoder for output stream #0:1 - maybe incorrect parameters such as bit_rate, rate, width or height 待研究,有點像是套件內部給的參數錯誤

個人結論

這個套件內部其實只是用命令列的方式使用 ffmpeg
自己 diy 直接執行 ffmpeg 應該也可以
不過如果有用到使用者的參數要做好過濾

神威 iT邦研究生 4 級 ‧ 2020-12-31 17:01:52 檢舉

感謝大大
但目前我放了ffmpeg.exe和ffprobe.exe
也刪除PHP.ini設定,卻還是出現以下錯誤
努力中~~

Fatal error: Uncaught Alchemy\BinaryDriver\Exception\ExecutionFailureException: ffprobe failed to execute command C:\ffmpeg\bin\ffprobe.exe -help -loglevel quiet: Error Output: in C:\vendor\alchemy\binary-driver\src\Alchemy\BinaryDriver\ProcessRunner.php:95 Stack trace: #0 C:\vendor\alchemy\binary-driver\src\Alchemy\BinaryDriver\ProcessRunner.php(73): Alchemy\BinaryDriver\ProcessRunner->doExecutionFailure('C:\\ffmpeg\\bin\\f...', '') #1 C:\vendor\alchemy\binary-driver\src\Alchemy\BinaryDriver\AbstractBinary.php(207): Alchemy\BinaryDriver\ProcessRunner->run(Object(Symfony\Component\Process\Process), Object(SplObjectStorage), false) #2 C:\vendor\alchemy\binary-driver\src\Alchemy\BinaryDriver\AbstractBinary.php(136): Alchemy\BinaryDriver\AbstractBinary->run(Object(Symfony\Component\Process\Process), false, NULL) #3 C:\vendor\php-ffmpeg\php-ffmpeg\src\FFMpeg\FFProbe\OptionsTester.php(61): Alchemy\BinaryDriver\AbstractBinary->command(Array) #4 C:\vendor\php-ffmpeg\php-ffmpeg\src\FFMpeg\FFProbe\OptionsTester.php(43): FFMpeg in C:\vendor\php-ffmpeg\php-ffmpeg\src\FFMpeg\FFProbe\OptionsTester.php on line 63
淺水員 iT邦大師 6 級 ‧ 2020-12-31 17:11:36 檢舉

在命令列直接執行 C:\ffmpeg\bin\ffprobe.exe -help -loglevel quiet 看看會產生甚麼錯誤。

神威 iT邦研究生 4 級 ‧ 2021-01-04 15:13:35 檢舉

謝謝大大,在別台電腦就好了~

0

先用phpinfo看看是否真的有載入。
安裝套件完,要記得重啟。

目前看起來因該是沒啟動這個套件造成的。
有時要查一下php.ini是否的有掛入這個套件。

畢竟有時命令掛入的,會因為配置上的不同,而掛入錯不同的設定檔。

看更多先前的回應...收起先前的回應...
神威 iT邦研究生 4 級 ‧ 2020-12-31 09:29:35 檢舉

星空大大好:
phpinfo真的沒載入....
但我再php.ini中已加入

extension=php_ffmpeg.dll

而且Apache也重開好幾次了.....
想請教大大有沒有甚麼方法呢?謝謝

是有個不太建議的招。
正常會發生這樣的事,大多都是設定的指向路徑錯誤。
如你的ext指向路徑位置得先確定是指向去哪。

但其實還是有個爛招。window系統獨有的。
可以試著將dll copy到windows/system32這邊試試。
有時可以讀到。

不過我先說,這招算是很不好。最好還是確保你的ext位置在哪。用正規的方式來處理。

可以查一下你的php的log。有時可能並不是沒載入到。有機會可能是版本問題。但這要從log中查看才能知道原因。

淺水員 iT邦大師 6 級 ‧ 2020-12-31 10:53:42 檢舉

真的要放到 windows/system32 之前
可以先試試看放到 apache/bin

通靈亡 iT邦高手 1 級 ‧ 2020-12-31 11:15:11 檢舉

This library requires a working FFMpeg install. You will need both FFMpeg and FFProbe binaries to use it.
Be sure that these binaries can be located with system PATH to get the benefit of the binary detection

問一下,你有沒有到FFmpeg.org下載和安裝 FFMpeg & FFProbe
沒有的話先到官網下載windows版的ffmpeg,有可能是在系統抓不到這兩個binary造成錯誤:
https://ffmpeg.org/download.html

Git文件上寫的網站已經關閉了
https://www.reddit.com/r/youtubedl/comments/ikmtb5/ffmpegzeranoecom_official_ffmpeg_builds_for/

神威 iT邦研究生 4 級 ‧ 2020-12-31 13:11:15 檢舉

淺水員我二個地方都放還是不OK....通靈亡好的我再裝裝看

其實,我剛剛點開你第一張圖。突然發現到一個問題點。
你有沒有注意到,它的錯誤訊息是找不到 php_ffmpeg.dll.dll

多了一個dll。

搞不好這就是你的問題了。

0
咖咖拉
iT邦好手 1 級 ‧ 2020-12-31 10:03:58

看錯自刪

0
japhenchen
iT邦超人 1 級 ‧ 2020-12-31 10:31:24

我的做法是寫個python去跑ffmpeg下載串流資料並存檔,讓php去呼叫這個.py,執行完畢後再print值回傳給php,再回php把檔案丟給瀏覽器下載.....

至於php-ffmpeg,我沒裝也不想裝,不知有沒有在維護更新

$songname = exec("/home/xxxxxx/code/getsong.py '" . $songname . "' ");
if(file_exists($songname))
{
    $extension = "mp3";
    $mime_type = "audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3";
    ob_start();
    header('Content-Disposition: attachment ;filename="' . urlencode(getBrifname($songname)) . '.mp3"');
    header('Content-type: {$mime_type}');
    header('X-Pad: avoid browser bug');
    header('Cache-Control: no-cache');
    header('Expires: 0');
    header('Content-Length: ' . filesize($songname));
    readfile($songname);
    ob_end_flush();
}

檔案是下載放到linux上的tmpfs(可以看成是Linux上的記憶體虛擬硬碟),下載完再unlink掉就不見了,不直接寫入硬碟上

神威 iT邦研究生 4 級 ‧ 2020-12-31 13:12:11 檢舉

謝謝japhenchen大大,我再試試看~

我要發表回答

立即登入回答