# FFmpeg

{% hint style="warning" %}
注意：此功能需要在 Eagle 4.0 beta 7 或以上版本才能使用
{% endhint %}

## FFmpeg 相依性插件簡介

「FFmpeg 相依性插件」是一款針對瀏覽器插件開發者的開發工具包，它將 FFmpeg 強大的多媒體處理能力封裝為易用的相依性包。此工具包讓開發者能在自身的插件中輕鬆實現圖像、影片和音訊格式的編解碼，以及串流媒體處理和格式轉換等高階功能。透過整合「FFmpeg 相依性插件」，開發者可以無縫擴展其插件的多媒體處理能力，為使用者帶來更多富有創意和實用性的功能。

## 安裝 FFmpeg 相依性插件

1. 進入插件中心
2. 搜尋並找到 FFmpeg 插件
3. 點擊安裝 FFmpeg 插件

{% hint style="info" %}
請注意，當使用者安裝具有 FFmpeg 相依性的插件時，Eagle 會自動提示使用者安裝「FFmpeg 相依性插件」。因此，開發者無需專門撰寫程式碼讓使用者進行安裝，只需為可能出現的錯誤提供相應提示。
{% endhint %}

## 如何使用 FFmpeg 相依性插件

如果你希望在你的插件中使用 FFmpeg 相關功能，你需要在插件的 `manifest.json` 檔案中添加 `dependencies` 定義，讓 Eagle 系統知道這個插件需要額外的擴展功能，示例如下：

```json
{
    "id": "LBCZE8V6LPCKD",
    "version": "1.0.0",
    "platform": "all",
    "arch": "all",
    "name": "視窗插件",
    "logo": "/logo.png",
    "keywords": [],
    "dependencies": ["ffmpeg"],
    "devTools": false,
    "main":
    {
        "url": "index.html",
        "width": 640,
        "height": 480,
    }
}
```

### 視窗插件範例 <a href="#gylpl" id="gylpl"></a>

你可以使用 `eagle.extraModule.ffmpeg` 來呼叫 FFmpeg 相依性插件提供的功能，示例如下：

```javascript
eagle.onPluginCreate(async (plugin) => {

    // 檢查 FFmpeg 相依性插件是否已經安裝
    const isFFemptInstalled = await eagle.extraModule.ffmpeg.isInstalled();
    
    // 從開啟插件中心，彈出安裝 FFmpeg 相依性插件頁面。
    if (!isFFemptInstalled) {
        await eagle.extraModule.ffmpeg.install();
        return;
    }
    
    // 取得 FFmpeg 二進位檔案所在位置
    const ffmpegPaths= await eagle.extraModule.ffmpeg.getPaths();
    const ffmpegBinaryPath = ffmpegPaths.ffmpeg;
    const ffprobeBinaryPath = ffmpegPaths.ffprobe;
    
    // 使用 spwan 指令執行相關操作
    const spawn = require('child_process').spawn;
    const ffprobe = spawn(ffprobePath, [
	'-v', 'error',
	'-print_format', 'json',
	'-show_format',
	'-show_streams',
	"C:\\your_file.mp4"
    ]);
});
```

### 縮圖插件範例 <a href="#gylpl" id="gylpl"></a>

你可以透過 `extraModule` 參數取得 FFmpeg 相關功能，示例如下：

```javascript

module.exports = async ({ src, dest, item, plugin, extraModule }) => {
    return new Promise(async (resolve, reject) => {
        try {
        
            const ffmpegModule = extraModule.ffmpeg;
	
            // 檢查 FFmpeg 相依性插件是否已經安裝
            if (!ffmpegModule.isInstalled) {
		return reject(new Error(`ffmpeg is not installed.`));
	    }
	    
            // 取得 FFmpeg 二進位檔案所在位置
            const { ffmpeg, ffprobe } = ffmpegModule.paths;
            
            // 使用 spwan 指令執行相關操作
            const spawn = require('child_process').spawn;
            const ffprobe = spawn(ffprobePath, [
	        '-v', 'error',
        	'-print_format', 'json',
	        '-show_format',
	        '-show_streams',
	        "C:\\your_file.mp4"
            ]);
            
            return resolve(item);
        }
        catch (err) {
            return reject(err);
        }
    });
}
```
