@@ -16,6 +16,7 @@ import {
1616 FFFSType ,
1717 FFFSMountOptions ,
1818 FFFSPath ,
19+ FileReadData ,
1920} from "./types.js" ;
2021import { getMessageID } from "./utils.js" ;
2122import { ERROR_TERMINATED , ERROR_NOT_LOADED } from "./errors.js" ;
@@ -63,6 +64,10 @@ export class FFmpeg {
6364 case FFMessageType . UNMOUNT :
6465 case FFMessageType . EXEC :
6566 case FFMessageType . FFPROBE :
67+ case FFMessageType . OPEN :
68+ case FFMessageType . CLOSE :
69+ case FFMessageType . READ :
70+ case FFMessageType . WRITE :
6671 case FFMessageType . WRITE_FILE :
6772 case FFMessageType . READ_FILE :
6873 case FFMessageType . DELETE_FILE :
@@ -362,6 +367,120 @@ export class FFmpeg {
362367 ) as Promise < OK > ;
363368 } ;
364369
370+ /**
371+ * Opens a file with the specified path, flags, and mode.
372+ *
373+ * @returns A file descriptor number.
374+ * @category File System
375+ */
376+ public open = (
377+ /** The path to the file. */
378+ path : string ,
379+ /**
380+ * Mode for opening the file (e.g., 'r', 'w', 'a')
381+ * @see [FS read and write flags](https://emscripten.org/docs/api_reference/Filesystem-API.html#fs-read-and-write-flags)
382+ * */
383+ flags : string ,
384+ /**
385+ * Permissions for creating a new file.
386+ * @defaultValue 0666
387+ * */
388+ mode ?: number
389+ ) : Promise < number > => {
390+ return this . #send(
391+ {
392+ type : FFMessageType . OPEN ,
393+ data : { path, flags, mode } ,
394+ }
395+ ) as Promise < number > ;
396+ }
397+
398+ /**
399+ * Closes an open file descriptor.
400+ *
401+ * @returns Resolves when the file is successfully closed.
402+ * @category File System
403+ */
404+ public close = (
405+ /** The file descriptor to close. */
406+ fd : number
407+ ) : Promise < OK > => {
408+ return this . #send(
409+ {
410+ type : FFMessageType . CLOSE ,
411+ data : { fd } ,
412+ }
413+ ) as Promise < OK > ;
414+ }
415+
416+ /**
417+ * Reads data from an open file descriptor.
418+ * @example
419+ * ```ts
420+ * const ffmpeg = new FFmpeg();
421+ * await ffmpeg.load();
422+ * const fd = await ffmpeg.open("../video.avi");
423+ * const CHUNK_SIZE = 1024;
424+ * const { data, done } = await ffmpeg.read(fd, 0, CHUNK_SIZE)
425+ * await ffmpeg.close(fd);
426+ * ```
427+ * @category File System
428+ */
429+ public read = (
430+ /** The file descriptor to read from. */
431+ fd : number ,
432+ /** The offset in the buffer to start writing data. */
433+ offset : number ,
434+ /** The number of bytes to read. */
435+ length : number ,
436+ /** The offset within the stream to read. By default this is the stream’s current offset. */
437+ position ?: number
438+ ) : Promise < FileReadData > => {
439+ return this . #send(
440+ {
441+ type : FFMessageType . READ ,
442+ data : { fd, offset, length, position } ,
443+ }
444+ ) as Promise < FileReadData > ;
445+ }
446+
447+ /**
448+ * Writes data to an open file descriptor.
449+ *
450+ * @example
451+ * ```ts
452+ * const ffmpeg = new FFmpeg();
453+ * await ffmpeg.load();
454+ * const data = new Uint8Array(32);
455+ * const fd = await ffmpeg.open("../video.avi", "w+");
456+ * await ffmpeg.write(fd, data, 0, data.length, 0);
457+ * await ffmpeg.close(fd);
458+ * ```
459+ * @category File System
460+ */
461+ public write = (
462+ /** The file descriptor to write to. */
463+ fd : number ,
464+ /** The buffer containing the data to write. */
465+ buffer : Uint8Array ,
466+ /** The offset in the buffer to start writing from. */
467+ offset : number ,
468+ /** The number of bytes to write. */
469+ length : number ,
470+ /** The offset within the stream to write. By default this is the stream’s current offset. */
471+ position ?: number
472+ ) : Promise < OK > => {
473+ const trans : Transferable [ ] = [ buffer . buffer ] ;
474+
475+ return this . #send(
476+ {
477+ type : FFMessageType . WRITE ,
478+ data : { fd, buffer, offset, length, position } ,
479+ } ,
480+ trans
481+ ) as Promise < OK > ;
482+ }
483+
365484 /**
366485 * Read data from ffmpeg.wasm.
367486 *
0 commit comments