-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from hughfenghen/feat/mp4-previewer
Feat/mp4 previewer
- Loading branch information
Showing
9 changed files
with
576 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
nav: DEMO | ||
group: 解码 | ||
order: 4 | ||
--- | ||
|
||
# 视频预览 | ||
|
||
从 MP4 文件中提取指定时间的图像,点击 Slider 预览任意时间点的图像。 | ||
|
||
```tsx | ||
import React, { useState } from 'react'; | ||
import { Slider } from 'antd'; | ||
import { MP4Previewer } from '@webav/av-cliper'; | ||
import { assetsPrefix } from './utils'; | ||
|
||
const videoSrc = assetsPrefix(['video/webav1.mp4']); | ||
|
||
const previewer = new MP4Previewer((await fetch(videoSrc)).body!); | ||
const mp4Info = await previewer.getInfo(); | ||
const duration = Number((mp4Info.duration / mp4Info.timescale).toFixed(0)); | ||
|
||
export default function UI() { | ||
const [imgSrc, setImgSrc] = useState<string>(''); | ||
|
||
return ( | ||
<div> | ||
<div style={{ display: 'flex', alignItems: 'center' }}> | ||
<span> 时间:</span> | ||
<div style={{ flex: 1 }}> | ||
<Slider | ||
min={0} | ||
max={duration} | ||
step={0.1} | ||
onChange={async (val) => { | ||
setImgSrc(await previewer.getImage(val)); | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
{imgSrc && <img src={imgSrc} style={{ width: '100%' }} />} | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
:::info | ||
如果只是为了绘制图像,使用视频帧更合适,`await previewer.getVideoFrame(time)`。 | ||
|
||
**注意**,视频帧使用完需要立即调用 `videoFrame.close()` | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { MP4Previewer } from "../src/mp4-utils/mp4-previewer"; | ||
import { OPFSFileWrap } from "../src/mp4-utils/opfs-file-wrap"; | ||
|
||
const previewer = new MP4Previewer((await fetch('./video/webav1.mp4')).body!) | ||
const imgEl = document.querySelector('#img') as HTMLImageElement | ||
|
||
for (let i = 0; i < 10; i += 1) { | ||
const t = performance.now() | ||
const img = await previewer.getImage(i) | ||
console.log('cost:', performance.now() - t, img) | ||
if (img == null) break | ||
imgEl.src = img | ||
} | ||
|
||
|
||
// 测试 OPFSFileWrap | ||
const opfsFile = new OPFSFileWrap('1111') | ||
|
||
await opfsFile.write(new Uint8Array([1, 2, 3, 4, 5])) | ||
await opfsFile.write(new Uint8Array([6, 7, 8, 9, 0])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>MP4Previewer</title> | ||
<style> | ||
#img { | ||
width: 900px; | ||
height: 500px; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<img id="img"> | ||
<script src="./mp4-previewer.demo.ts" type="module"></script> | ||
</body> | ||
|
||
</html> |
4 changes: 2 additions & 2 deletions
4
...av-cliper/src/__tests__/mp4-utils.test.ts → ...src/mp4-utils/__tests__/mp4-utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.