Skip to content

Commit

Permalink
feat: use opfs-tools access filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
hughfenghen committed Feb 4, 2024
1 parent 6cd8b65 commit a0afa77
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 100 deletions.
1 change: 1 addition & 0 deletions packages/av-cliper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"dependencies": {
"@types/dom-webcodecs": "^0.1.7",
"@webav/mp4box.js": "0.5.3-fenghen",
"opfs-tools": "^0.1.1",
"wave-resampler": "^1.0.0"
}
}
12 changes: 8 additions & 4 deletions packages/av-cliper/src/mp4-utils/mp4-previewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MP4File, MP4Info, MP4Sample } from '@webav/mp4box.js'
import { autoReadStream } from '../av-utils'
import { Log } from '../log'
import { extractFileConfig, sample2ChunkOpts } from './mp4box-utils'
import { OPFSFileWrap } from './opfs-file-wrap'
import { file } from 'opfs-tools'
import { SampleTransform } from './sample-transform'

export class MP4Previewer {
Expand All @@ -14,7 +14,7 @@ export class MP4Previewer {
data: null
}> = []

#opfsFile = new OPFSFileWrap(Math.random().toString())
#opfsFile = file(Math.random().toString())

#wrapDecoder: ReturnType<typeof wrapVideoDecoder> | null = null

Expand All @@ -27,6 +27,7 @@ export class MP4Previewer {

async #init(stream: ReadableStream<Uint8Array>) {
let offset = 0
const writer = await this.#opfsFile.createWriter()
return new Promise<MP4Info>((resolve, reject) => {
let mp4Info: MP4Info | null = null
let mp4boxFile: MP4File | null = null
Expand Down Expand Up @@ -60,13 +61,14 @@ export class MP4Previewer {
data: null
})
offset += s.data.byteLength
await this.#opfsFile.write(s.data)
await writer.write(s.data)
}
}
mp4boxFile?.releaseUsedSamples(data.id, data.samples.length)
}
},
onDone: async () => {
await writer.close()
if (mp4Info == null) {
reject('Parse failed')
return
Expand Down Expand Up @@ -109,13 +111,15 @@ export class MP4Previewer {
}
}

const reader = await this.#opfsFile.createReader()
const chunks = await Promise.all(
this.#videoSamples.slice(start, end + 1)
.map(async s => new EncodedVideoChunk(sample2ChunkOpts({
...s,
data: await this.#opfsFile.read(s.offset, s.size)
data: await reader.read(s.offset, s.size)
})))
)
await reader.close()
if (chunks.length === 0) return Promise.resolve(null)

return new Promise<VideoFrame>((resolve) => {
Expand Down
92 changes: 0 additions & 92 deletions packages/av-cliper/src/mp4-utils/opfs-file-wrap.ts

This file was deleted.

14 changes: 10 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5471,13 +5471,14 @@ npm-normalize-package-bin@^1.0.0, npm-normalize-package-bin@^1.0.1:
semver "^5.6.0"
validate-npm-package-name "^3.0.0"

npm-packlist@1.1.12, npm-packlist@^1.4.4:
version "1.1.12"
resolved "https://registry.npmjs.org/npm-packlist/-/npm-packlist-1.1.12.tgz#22bde2ebc12e72ca482abd67afc51eb49377243a"
integrity sha512-WJKFOVMeAlsU/pjXuqVdzU0WfgtIBCupkEVwn+1Y0ERAbUfWw8R4GjgVbaKnUjRoD2FoQbHOCbOyT5Mbs9Lw4g==
npm-packlist@^1.4.4:
version "1.4.8"
resolved "https://registry.npmjs.org/npm-packlist/-/npm-packlist-1.4.8.tgz#56ee6cc135b9f98ad3d51c1c95da22bbb9b2ef3e"
integrity sha512-5+AZgwru5IevF5ZdnFglB5wNlHG1AOOuw28WhUq8/8emhBmLv6jX5by4WJCh7lW0uSYZYS6DXqIsyZVIXRZU9A==
dependencies:
ignore-walk "^3.0.1"
npm-bundled "^1.0.1"
npm-normalize-package-bin "^1.0.1"

npm-pick-manifest@^3.0.0:
version "3.0.2"
Expand Down Expand Up @@ -5632,6 +5633,11 @@ onetime@^2.0.0:
dependencies:
mimic-fn "^1.0.0"

opfs-tools@^0.1.1:
version "0.1.1"
resolved "https://registry.npmjs.org/opfs-tools/-/opfs-tools-0.1.1.tgz#25c42085cdc32fb9f83cdd602e9c41e0bf9aa0d0"
integrity sha512-F+UroKcPfU2/ZzT/4/hONIEbdFHkD/oWwpzH3rkaz+EiDm0W6b2vqNAp1LrWClGrERZ19X3ceRixYL0ZdXcpjA==

optionator@^0.8.1:
version "0.8.3"
resolved "https://registry.npmmirror.com/optionator/-/optionator-0.8.3.tgz"
Expand Down

0 comments on commit a0afa77

Please sign in to comment.