Skip to content
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

Failed to construct 'URL': Invalid URL #800

Open
nickycdk opened this issue Nov 28, 2024 · 0 comments
Open

Failed to construct 'URL': Invalid URL #800

nickycdk opened this issue Nov 28, 2024 · 0 comments

Comments

@nickycdk
Copy link

I am trying to compress and resize a video in my vue application, but have run into a few headaches, where I am getting errors I cant quite any documentation on (Error compress video: TypeError: Failed to construct 'URL': Invalid URL).


import { FFmpeg } from '@ffmpeg/ffmpeg';
import { fetchFile } from '@ffmpeg/util';

const _ffmpeg = ref(new FFmpeg());

const compressVideo = async (file: File) => {
  const fileName = file.name;
  const fileData = await fetchFile(file);

  await _ffmpeg.value.load();

  _ffmpeg.value.writeFile(fileName, fileData);

  try {
    await _ffmpeg.value.exec([
      '-i',
      fileName,
      '-vf',
      'scale=720:-1',
      '-b:v',
      '1000k',
      '-c:v',
      'libx264',
      '-crf',
      '23',
      '-preset',
      'medium',
      'output.mp4',
    ]);

    // Read the compressed file
    const data = await _ffmpeg.value.readFile('output.mp4');

    // Create a Blob from the compressed file data
    const compressedBlob = new Blob([data], { type: 'video/mp4' });

    // Optional: Create a downloadable link
    const url = URL.createObjectURL(compressedBlob);
    const link = document.createElement('a');
    link.href = url;
    link.download = 'compressed_' + fileName;
    link.click();

    // Clean up
    URL.revokeObjectURL(url);

    console.log('Video compressed successfully');
  } catch (error) {
    console.error('Compression failed:', error);
  }
};

I am using it like so when the user selects a video file using an input file element:


const target = e.target as HTMLInputElement;
const newFile = target.files[0];
    try {
      const test = await compressVideo(file);
      console.log('the resized file', newFile)
    } catch (error) {
      console.log('Error compress video:', error);
    }

The error I am getting is: Error compress video: TypeError: Failed to construct 'URL': Invalid URL

What am I doing wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant