Error with FFmpeg and FS in React: “ErrnoError: FS error”

I’m working on a React application where I’m using the @ffmpeg/ffmpeg library to compress images and videos. I’m facing an issue with the virtual file system (FS) when trying to read and write files using FFmpeg. I’m getting the following error:

ErrnoError: FS error

Here’s the relevant part of my code:

import React, { useState } from "react";
import { FFmpeg } from "@ffmpeg/ffmpeg";

const ffmpeg = new FFmpeg();

const fetchFile = async (filePath) => {
    const file = await ffmpeg.readFile(filePath);
    alert("hello");
    return new Uint8Array(file).buffer;
};


const Main = () => {
    const [file, setFile] = useState(null);
    const [compressedFile, setCompressedFile] = useState("");

    const loadFFmpeg = async () => {
        if (!ffmpeg.isLoaded) {
          await ffmpeg.load();
        }
      };      

    const getFile = (event) => {
        const selectedFile = event.target.files[0];
        
        if (selectedFile) {
            setFile(selectedFile);
        }
    };

    const compressImage = (selectedFile) => {
        const img = new Image();
        img.src = URL.createObjectURL(selectedFile);
        img.onload = () => {
            const canvas = document.createElement('canvas');
            const MAX_WIDTH = 300;
            const MAX_HEIGHT = 300;
            let width = img.width;
            let height = img.height;

            if (width > height) {
                if (width > MAX_WIDTH) {
                    height *= MAX_WIDTH / width;
                    width = MAX_WIDTH;
                }
            } else {
                if (height > MAX_HEIGHT) {
                    width *= MAX_HEIGHT / height;
                    height = MAX_HEIGHT;
                }
            }

            canvas.width = width;
            canvas.height = height;
            const ctx = canvas.getContext('2d');
            ctx.drawImage(img, 0, 0, width, height);
            const dataUrl = canvas.toDataURL('image/jpeg', 1.0);
            setCompressedFile(dataUrl);
        };
    };

    const compressVideo = async (selectedFile) => {
        try {
            await loadFFmpeg();
    
            const arrayBuffer = await selectedFile.arrayBuffer();
            const fileName = selectedFile.name;
    
            await ffmpeg.writeFile(fileName, new Uint8Array(arrayBuffer));
    
            await ffmpeg.exec(
                '-i',
                fileName,
                '-vf',
                'scale=640:-1',
                '-c:a',
                'aac',
                '-strict',
                '-2',
                'output.mp4'
            );
    
            const data = await fetchFile('output.mp4');
            const compressedVideoBlob = new Blob([data], { type: 'video/mp4' });
            const compressedVideoUrl = URL.createObjectURL(compressedVideoBlob);
            setCompressedFile(compressedVideoUrl);
    
            await ffmpeg.unlink(fileName);
            await ffmpeg.unlink('output.mp4');
    
            alert('Compression successful');
        } catch (error) {
            console.error('Error:', error);
            alert('Compression failed. Please check the console for more details.');
        }
    };
        

    const handleSubmit = async (e) => {
        e.preventDefault();

        if (file) {
            const fileType = file.name.split('.').pop().toLowerCase();

            if (fileType === 'png' || fileType === 'jpg' || fileType === 'jpeg') {
                compressImage(file);
            } else if (fileType === 'mp4' || fileType === 'h264') {
                compressVideo(file);
            } else {
                alert('Please select a valid file type (png, jpg, jpeg for images or mp4, h264 for videos).');
            }
        }
    };

    const handleDownload = () => {
        if (file) {
            const downloadLink = document.createElement('a');
            downloadLink.href = compressedFile;

            const fileExtension = file.name.split('.').pop().toLowerCase();

            downloadLink.download = `compressed_file.${fileExtension}`;
    
            document.body.appendChild(downloadLink);
            downloadLink.click();
            document.body.removeChild(downloadLink);
        }
    };

    return (
        <>
            <h1>Main Page</h1>
            <form onSubmit={handleSubmit}>
                <label>Upload</label>
                <input type='file' onChange={getFile} />
                <br /><br />
                <input type="submit" value="Compress" />
            </form>
            {compressedFile && (
                <>
                    <h2>Compressed File Preview</h2>
                    {file && file.name && ( 
                        file.name.split('.').pop().toLowerCase() === 'mp4' || file.name.split('.').pop().toLowerCase() === 'h264' ? (
                            <video width="300" controls>
                                <source src={compressedFile} type="video/mp4" />
                                Your browser does not support the video tag.
                            </video>
                        ) : (
                            <img src={compressedFile} alt="Compressed file preview" />
                        )
                    )}
                    <br /><br />
                    <button onClick={handleDownload}>Download Compressed File</button>
                </>
            )}
        </>
    );
};

export default Main;

I’m using ffmpeg.readFile and ffmpeg.writeFile to read and write files to FFmpeg’s virtual file system. I’ve also tried using ffmpeg.read and ffmpeg.write but still encounter the same issue.

Could someone please help me understand what might be causing this FS error and how to resolve it?

New contributor

Kanyarat Monklung is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật