Repository navigation
feat(ffmpeg): add open, read, write and close for chunked file access #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
87b306b
487d692
01a6a15
0a4ac8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,10 @@ import type { | |
| FFMessageEvent, | ||
| FFMessageLoadConfig, | ||
| FFMessageExecData, | ||
| FFMessageOpenData, | ||
| FFMessageReadData, | ||
| FFMessageWriteData, | ||
| FFMessageCloseData, | ||
| FFMessageWriteFileData, | ||
| FFMessageReadFileData, | ||
| FFMessageDeleteFileData, | ||
|
|
@@ -148,6 +152,32 @@ const writeFile = ({ path, data }: FFMessageWriteFileData): OK => { | |
| return true; | ||
| }; | ||
|
|
||
| const open = ({ path, flags }: FFMessageOpenData): number => | ||
| ffmpeg.FS.open(path, flags).fd; | ||
|
|
||
| const read = ({ fd, length, position }: FFMessageReadData): Uint8Array => { | ||
| const stream = ffmpeg.FS.getStreamChecked(fd); | ||
| // Allocate no more than the file holds, whatever length asks for. | ||
| const left = ffmpeg.FS.fstat(fd).size - (position ?? stream.position); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: The read implementation calculates remaining bytes using |
||
| const buffer = new Uint8Array(Math.max(0, Math.min(length, left))); | ||
| const count = ffmpeg.FS.read(stream, buffer, 0, buffer.length, position); | ||
| return count === buffer.length ? buffer : buffer.slice(0, count); | ||
| }; | ||
|
|
||
| const write = ({ fd, data, position }: FFMessageWriteData): number => | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: Similarly, write passes |
||
| ffmpeg.FS.write( | ||
| ffmpeg.FS.getStreamChecked(fd), | ||
| data, | ||
| 0, | ||
| data.length, | ||
| position | ||
| ); | ||
|
|
||
| const close = ({ fd }: FFMessageCloseData): OK => { | ||
| ffmpeg.FS.close(ffmpeg.FS.getStreamChecked(fd)); | ||
| return true; | ||
| }; | ||
|
|
||
| const readFile = ({ path, encoding }: FFMessageReadFileData): FileData => | ||
| ffmpeg.FS.readFile(path, { encoding }); | ||
|
|
||
|
|
@@ -218,6 +248,18 @@ self.onmessage = async ({ | |
| case FFMessageType.FFPROBE: | ||
| data = ffprobe(_data as FFMessageExecData); | ||
| break; | ||
| case FFMessageType.OPEN: | ||
| data = open(_data as FFMessageOpenData); | ||
| break; | ||
| case FFMessageType.READ: | ||
| data = read(_data as FFMessageReadData); | ||
| break; | ||
| case FFMessageType.WRITE: | ||
| data = write(_data as FFMessageWriteData); | ||
| break; | ||
| case FFMessageType.CLOSE: | ||
| data = close(_data as FFMessageCloseData); | ||
| break; | ||
| case FFMessageType.WRITE_FILE: | ||
| data = writeFile(_data as FFMessageWriteFileData); | ||
| break; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: Transferring
data.buffertransfers the entire underlying ArrayBuffer, not just the Uint8Array view. If callers pass a view into a larger buffer (e.g.,new Uint8Array(largeBuffer, offset, length)), the whole buffer becomes unusable. This matches writeFile() behavior but is worth documenting in the method's JSDoc for clarity.