FFmpeg is a free and open -source software consisting of a suite of libraries and programs for handling video, audio, and other multimedia files and streams. With FFmpeg, we can do a lot of things with media-related files such as converting a video format, adding subtitles, changing the quality of the video, streaming a video, etc. To use FFmpeg, we also need a good server that can run the video smoothly. However, maintaining a server can become very expensive if we are trying to upload quality videos. This is where WebAssembly comes into play.
WebAssembly is an open standard that allows websites to run very fast code. Initially, we can only write HTML, CSS, and JavaScript on our frontend. However, WebAssembly allows us to use languages other than JavaScript that runs faster on the frontend. In short, WebAssembly allows us to run high quality program other than JavaScript(which may lack power) on the browser.
When we are running FFmpeg to convert files, we are assuming the file will be converted on the users computer.
We will be using WebAssembly to run FFmpeg.
To install WebAssembly supported FFmpeg, we install the following npm package:
npm i @ffmpeg/ffmpeg @ffmpeg/core
After installing FFmpeg, we import the code on our JavaScript file.
import { createFFmpeg, fetchFile } from "@ffmpeg/ffmpeg;
Next, we implement FFmpeg on our function that handles the download process. Here, it is essential that we use a promise method to run the code as we are running a heavy code that we usually do not run on JavaScript.
const handleDownload = async () => {
const ffmpeg = createFFmpeg({ log: true });
await ffmpeg.load();
To create a file in the FFmpeg world, we use the writeFile method. Then, we need to give a name for the file(this can be any name). Finally, we need to give a binary data which is the data for the video file.
ffmpeg.FS("writeFile", "recording.webm", await fetchFile(videoFile));
To run the code, we implement the code on the FFmpeg documentation (ffmpeg.org/ffmpeg.html).
Order: input, name of file, frame rate, output
await ffmpeg.run("-i", "recording.webm", "-r", "60", "output.mp4");
To load our converted file, we use the readFile method and put the file we want to load. This file will be represented in an array of positive numbers.
const mp4File = ffmpeg.FS("readFile", "output.mp4");
Next, we need to created a blob using all the number data which represents the video file. To access, this array, we need to use the buffer property. Make sure to also add the type of file that will be converted.
const mp4Blob = new Blob([mp4File.buffer], { type: "video/mp4" });
We also need to make a URL for data that is received when we stopped recording a video. At this point, we will finally be able to get a blob URL that will signal that the file is converted from webm to mp4.
const mp4Url = URL.createObjectURL(mp4Blob);
Don't forget to change the file format of the anchors
a.href = mp4Url;
a.download = "MyRecording.mp4";
To capture a thumbnail, we need to run the ffmpege.run function again. Inside the function, we input the file we want to use, the point in which we want to capture our thumbnail, and choose the number of times we want to capture.
await ffmpeg.run(
"-i",
"recording.webm",
"-ss",
"00:00:01",
"-frames:v",
"1",
"thumbnail.jpg"
);
Similar to the mp4File, we create a file and blob for the thumbnail.
const thumbFile = ffmpeg.FS("readFile", "thumbnail.jpg");
const thumbBlob = new Blob([thumbFile.buffer], { type: "image/jpg" });
const thumbUrl = URL.createObjectURL(thumbBlob);
const thumbA = document.createElement("a");
thumbA.href = thumbUrl;
thumbA.download = "MyThumbnail.jpg";
document.body.appendChild(thumbA);
thumbA.click();
To increase speed, we can unlink the the download process.
ffmpeg.FS("unlink", "recording.webm");
ffmpeg.FS("unlink", "output.mp4");
ffmpeg.FS("unlink", "thumbnail.jpg");
URL.revokeObjectURL(mp4Url);
URL.revokeObjectURL(thumbUrl);
URL.revokeObjectURL(videoFile);
Excellent content, the country's an exceedingly cold web page you've got right, preserve acknowledge that there are succeed, would be lower back. 카지노사이트 먹튀
I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often. olxtoto
Pretty nice post. I just stumbled upon your weblog and wanted to say that I have really enjoyed browsing your blog posts. After all I’ll be subscribing to your feed and I hope you write again soon! jokercard ca
Di Slot Demo, Anda dapat menikmati permainan slot seru dan menarik tanpa risiko kehilangan uang. Mainkan berbagai permainan dengan peluang besar untuk meraih jackpot yang menggiurkan dan rasakan pengalaman bermain yang menghibur. slot demo
Many thanks for that publish as well as excellent ideas.. actually We additionally believe that effort is actually the most crucial facet of obtaining achievement. funny sayings panties
Kudos to your place together with terrific hints.. quite possibly That i at the same time are convinced labor is certainly a very powerful aspect of becoming results. biurko białe
With thanks for any posting plus superb recommendations.. sometimes I actually as well reckon that diligence is definitely a vey important ingredient of finding being successful. alexistogel login
Fine place, a an exceptionally amazing webpage that there is at this point, stick to beneficial financial job, shall be to come back. Pinny