In this lecture, we are going to create a service for FFmpeg.
This step is optional, but if MPEG is a heavy package, the file size of this package is twenty five
megabytes to keep our app lean.
We're going to outsource the logic into a service in a future lecture.
We're going to learn how to lazy load a service to get started.
Let's create a service with the Seelye in the command line.
Run the following command Njie G Service Services slash FFmpeg.
Next, open this file in your editor.
The first step is to create a new instance of the FFmpeg class from the FFmpeg package, a factory function
is exported by this package to create the instance at the top of the file importing function called
create FFmpeg from the FFmpeg slash FFmpeg package.
Next, inside the class, create a public property called is ready with an initial value of false.
Initializing FFmpeg will take some time.
We have a component called upload for handling the upload process.
Users should not be able to upload files until the service is ready to generate screenshots.
We'll update the component by creating a public property.
Next, create a private property called FFmpeg.
We should initialize this property from the constructor function in this function, set the FFmpeg property
to the Create FFmpeg function.
The Create FFmpeg function will return a new instance of the FFmpeg class, one of my favorite features
of this package is being able to debug commands.
The F MPEG tool will output messages to the command line under the hood.
This package will capture them.
We have the option of reading those logs in the council of the developer tools inside this function
pass in an object with a property called log.
Its value will be true.
By churning, logging on will be able to debug FFmpeg during development.
Next, we need to start loading FFmpeg.
The Create FFmpeg function creates an instance, but does not load the WebAssembly file.
This action is performed separately.
The developers of this package recommend downloading the WebAssembly file as soon as possible.
Its large file size can cause a delay.
Let's define an asynchronous function called initialize.
Loading the file is an asynchronous task.
We should wait for the file to load without blocking the main thread before we start downloading the
WebAssembly file.
We should check if it has already been loaded.
We don't want to read download a large file inside the function.
Create a conditional statement to check if the is ready property is true.
If it is, we can assume FFmpeg has been loaded.
We're going to return the function.
The next step is to start loading FFmpeg, we can call the load function to start downloading FFmpeg
after the conditional statement.
We're going to call the FFmpeg load function with the await keyword.
Lastly, we're going to update the is ready property to true.
By updating this property to True, we can prevent FFmpeg from loading.
Our service is ready in the following lecture, we are going to inject this service into the upload
component.