ffmpeg-browser-cdn-loader 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/PUBLISH.md +114 -0
- package/README.md +104 -0
- package/index.js +103 -0
- package/package.json +21 -0
package/PUBLISH.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# 发布到 npm 指南
|
|
2
|
+
|
|
3
|
+
## 前提条件
|
|
4
|
+
|
|
5
|
+
1. 拥有 npm 账号(在 https://www.npmjs.com/ 注册)
|
|
6
|
+
2. 已安装 Node.js 和 npm
|
|
7
|
+
|
|
8
|
+
## 发布步骤
|
|
9
|
+
|
|
10
|
+
### 1. 登录 npm
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm login
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
输入你的 npm 用户名、密码和邮箱。
|
|
17
|
+
|
|
18
|
+
### 2. 检查包名是否可用
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm view @ilovesong/ffmpeg-browser
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
如果显示 404,说明包名可用。如果已被占用,需要修改 `package.json` 中的 `name` 字段。
|
|
25
|
+
|
|
26
|
+
### 3. 进入包目录
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
cd ffmpeg-browser-loader
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### 4. 发布包
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm publish --access public
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
注意:由于使用了 scoped package(@ilovesong),需要添加 `--access public` 参数。
|
|
39
|
+
|
|
40
|
+
### 5. 验证发布
|
|
41
|
+
|
|
42
|
+
访问 https://www.npmjs.com/package/@ilovesong/ffmpeg-browser 查看你的包。
|
|
43
|
+
|
|
44
|
+
## 更新版本
|
|
45
|
+
|
|
46
|
+
当需要更新包时:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
# 修改代码后,更新版本号
|
|
50
|
+
npm version patch # 1.0.0 -> 1.0.1
|
|
51
|
+
# 或
|
|
52
|
+
npm version minor # 1.0.0 -> 1.1.0
|
|
53
|
+
# 或
|
|
54
|
+
npm version major # 1.0.0 -> 2.0.0
|
|
55
|
+
|
|
56
|
+
# 发布新版本
|
|
57
|
+
npm publish --access public
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## 在项目中使用
|
|
61
|
+
|
|
62
|
+
发布成功后,在你的主项目中:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
npm install @ilovesong/ffmpeg-browser
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
然后修改 `libs/localMp4Generator.js`:
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
import { getFFmpeg, getFetchFile, getToBlobURL } from '@ilovesong/ffmpeg-browser';
|
|
72
|
+
|
|
73
|
+
export async function initFFmpeg(onProgress) {
|
|
74
|
+
if (ffmpegInstance) return ffmpegInstance;
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
const FFmpeg = await getFFmpeg();
|
|
78
|
+
const fetchFile = await getFetchFile();
|
|
79
|
+
const toBlobURL = await getToBlobURL();
|
|
80
|
+
|
|
81
|
+
const ffmpeg = new FFmpeg();
|
|
82
|
+
// ... 其余代码保持不变
|
|
83
|
+
} catch (error) {
|
|
84
|
+
// ...
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## 注意事项
|
|
90
|
+
|
|
91
|
+
1. 包名必须唯一,如果 `@ilovesong/ffmpeg-browser` 已被占用,改用其他名字
|
|
92
|
+
2. 确保 package.json 中的 version、description 等信息正确
|
|
93
|
+
3. 首次发布 scoped package 必须使用 `--access public`
|
|
94
|
+
4. 发布后无法删除,只能废弃(deprecate)
|
|
95
|
+
|
|
96
|
+
## 故障排除
|
|
97
|
+
|
|
98
|
+
### 包名已被占用
|
|
99
|
+
|
|
100
|
+
修改 `package.json` 中的 name,例如:
|
|
101
|
+
- `@your-username/ffmpeg-browser`
|
|
102
|
+
- `@ilovesong/ffmpeg-loader`
|
|
103
|
+
- `@ilovesong/ffmpeg-cdn`
|
|
104
|
+
|
|
105
|
+
### 需要验证邮箱
|
|
106
|
+
|
|
107
|
+
npm 会发送验证邮件,点击链接验证后才能发布。
|
|
108
|
+
|
|
109
|
+
### 权限错误
|
|
110
|
+
|
|
111
|
+
确保已正确登录:
|
|
112
|
+
```bash
|
|
113
|
+
npm whoami
|
|
114
|
+
```
|
package/README.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# @ilovesong/ffmpeg-browser
|
|
2
|
+
|
|
3
|
+
A lightweight FFmpeg loader for browsers that loads FFmpeg from CDN at runtime, avoiding the need to bundle large WASM files in your application.
|
|
4
|
+
|
|
5
|
+
## Why?
|
|
6
|
+
|
|
7
|
+
The official `@ffmpeg/ffmpeg` package is large and can cause issues with deployment platforms like Cloudflare Pages. This package solves that by:
|
|
8
|
+
|
|
9
|
+
- Loading FFmpeg from CDN only when needed
|
|
10
|
+
- Zero bundle size impact
|
|
11
|
+
- Same API as the official package
|
|
12
|
+
- Works in all modern browsers
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @ilovesong/ffmpeg-browser
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
import { getFFmpeg, getFetchFile, getToBlobURL } from '@ilovesong/ffmpeg-browser';
|
|
24
|
+
|
|
25
|
+
async function processVideo() {
|
|
26
|
+
// Get FFmpeg class (loads from CDN on first call)
|
|
27
|
+
const FFmpeg = await getFFmpeg();
|
|
28
|
+
const fetchFile = await getFetchFile();
|
|
29
|
+
const toBlobURL = await getToBlobURL();
|
|
30
|
+
|
|
31
|
+
// Use FFmpeg as normal
|
|
32
|
+
const ffmpeg = new FFmpeg();
|
|
33
|
+
|
|
34
|
+
ffmpeg.on('log', ({ message }) => {
|
|
35
|
+
console.log(message);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Load FFmpeg core
|
|
39
|
+
const coreBase = 'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.10/dist/umd';
|
|
40
|
+
await ffmpeg.load({
|
|
41
|
+
coreURL: await toBlobURL(`${coreBase}/ffmpeg-core.js`, 'text/javascript'),
|
|
42
|
+
wasmURL: await toBlobURL(`${coreBase}/ffmpeg-core.wasm`, 'application/wasm'),
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Process video...
|
|
46
|
+
await ffmpeg.writeFile('input.mp4', await fetchFile(videoBlob));
|
|
47
|
+
await ffmpeg.exec(['-i', 'input.mp4', 'output.mp4']);
|
|
48
|
+
const data = await ffmpeg.readFile('output.mp4');
|
|
49
|
+
|
|
50
|
+
return new Blob([data], { type: 'video/mp4' });
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## API
|
|
55
|
+
|
|
56
|
+
### `getFFmpeg()`
|
|
57
|
+
|
|
58
|
+
Returns the FFmpeg class. Loads from CDN on first call.
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
const FFmpeg = await getFFmpeg();
|
|
62
|
+
const ffmpeg = new FFmpeg();
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### `getFetchFile()`
|
|
66
|
+
|
|
67
|
+
Returns the fetchFile utility function.
|
|
68
|
+
|
|
69
|
+
```javascript
|
|
70
|
+
const fetchFile = await getFetchFile();
|
|
71
|
+
const data = await fetchFile(url);
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### `getToBlobURL()`
|
|
75
|
+
|
|
76
|
+
Returns the toBlobURL utility function.
|
|
77
|
+
|
|
78
|
+
```javascript
|
|
79
|
+
const toBlobURL = await getToBlobURL();
|
|
80
|
+
const blobURL = await toBlobURL(url, mimeType);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## How it works
|
|
84
|
+
|
|
85
|
+
1. On first use, the package dynamically loads FFmpeg libraries from CDN
|
|
86
|
+
2. Libraries are cached by the browser
|
|
87
|
+
3. Subsequent calls use the cached version
|
|
88
|
+
4. Your application bundle stays small
|
|
89
|
+
|
|
90
|
+
## Browser Support
|
|
91
|
+
|
|
92
|
+
- Chrome/Edge 90+
|
|
93
|
+
- Firefox 89+
|
|
94
|
+
- Safari 15+
|
|
95
|
+
|
|
96
|
+
Requires WebAssembly and SharedArrayBuffer support.
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
MIT
|
|
101
|
+
|
|
102
|
+
## Credits
|
|
103
|
+
|
|
104
|
+
This package is a lightweight wrapper around [@ffmpeg/ffmpeg](https://github.com/ffmpegwasm/ffmpeg.wasm).
|
package/index.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @ilovesong/ffmpeg-browser
|
|
3
|
+
*
|
|
4
|
+
* A lightweight FFmpeg loader for browsers that loads FFmpeg from CDN at runtime.
|
|
5
|
+
* This avoids bundling large WASM files in your application.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
let FFmpegClass = null;
|
|
9
|
+
let fetchFileFunc = null;
|
|
10
|
+
let toBlobURLFunc = null;
|
|
11
|
+
|
|
12
|
+
const CDN_BASE = 'https://cdn.jsdelivr.net/npm';
|
|
13
|
+
const FFMPEG_VERSION = '0.12.10';
|
|
14
|
+
const UTIL_VERSION = '0.12.1';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Load a script dynamically
|
|
18
|
+
*/
|
|
19
|
+
function loadScript(src) {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
if (typeof window === 'undefined') {
|
|
22
|
+
reject(new Error('This package only works in browser environments'));
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const script = document.createElement('script');
|
|
27
|
+
script.src = src;
|
|
28
|
+
script.onload = resolve;
|
|
29
|
+
script.onerror = () => reject(new Error(`Failed to load script: ${src}`));
|
|
30
|
+
document.head.appendChild(script);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Initialize FFmpeg by loading from CDN
|
|
36
|
+
*/
|
|
37
|
+
async function initialize() {
|
|
38
|
+
if (FFmpegClass) {
|
|
39
|
+
return; // Already initialized
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (typeof window === 'undefined') {
|
|
43
|
+
throw new Error('This package only works in browser environments');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Load FFmpeg library
|
|
47
|
+
if (!window.FFmpegWASM) {
|
|
48
|
+
await loadScript(`${CDN_BASE}/@ffmpeg/ffmpeg@${FFMPEG_VERSION}/dist/umd/ffmpeg.js`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Load FFmpeg utilities
|
|
52
|
+
if (!window.FFmpegUtil) {
|
|
53
|
+
await loadScript(`${CDN_BASE}/@ffmpeg/util@${UTIL_VERSION}/dist/umd/index.js`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Wait a bit for scripts to fully initialize
|
|
57
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
58
|
+
|
|
59
|
+
if (!window.FFmpegWASM || !window.FFmpegUtil) {
|
|
60
|
+
throw new Error('Failed to load FFmpeg libraries from CDN');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
FFmpegClass = window.FFmpegWASM.FFmpeg || window.FFmpegWASM;
|
|
64
|
+
fetchFileFunc = window.FFmpegUtil.fetchFile;
|
|
65
|
+
toBlobURLFunc = window.FFmpegUtil.toBlobURL;
|
|
66
|
+
|
|
67
|
+
if (!FFmpegClass || !fetchFileFunc || !toBlobURLFunc) {
|
|
68
|
+
throw new Error('FFmpeg libraries loaded but exports not found');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Get FFmpeg class (loads from CDN if needed)
|
|
74
|
+
*/
|
|
75
|
+
export async function getFFmpeg() {
|
|
76
|
+
await initialize();
|
|
77
|
+
return FFmpegClass;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get fetchFile utility (loads from CDN if needed)
|
|
82
|
+
*/
|
|
83
|
+
export async function getFetchFile() {
|
|
84
|
+
await initialize();
|
|
85
|
+
return fetchFileFunc;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Get toBlobURL utility (loads from CDN if needed)
|
|
90
|
+
*/
|
|
91
|
+
export async function getToBlobURL() {
|
|
92
|
+
await initialize();
|
|
93
|
+
return toBlobURLFunc;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Export a default object with all utilities
|
|
98
|
+
*/
|
|
99
|
+
export default {
|
|
100
|
+
getFFmpeg,
|
|
101
|
+
getFetchFile,
|
|
102
|
+
getToBlobURL,
|
|
103
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ffmpeg-browser-cdn-loader",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Browser-only FFmpeg loader that loads from CDN at runtime",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"prepublishOnly": "echo 'Ready to publish'"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"ffmpeg",
|
|
12
|
+
"wasm",
|
|
13
|
+
"browser",
|
|
14
|
+
"video",
|
|
15
|
+
"audio"
|
|
16
|
+
],
|
|
17
|
+
"author": "ilovesong.ai",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"peerDependencies": {},
|
|
20
|
+
"dependencies": {}
|
|
21
|
+
}
|