In this episode, we dive into encoding uploaded videos to make sure they're playable in all browsers. Now that we're comfortable with the Laravel FFmpeg library, we'll create a new job—call it something like ConvertVideoFormat
or EncodeVideo
. The goal here is to automate video format conversion right after upload, so users don’t run into compatibility issues when sharing their links.
The process is pretty straightforward: we copy over a lot of the logic from our previous video preview generation, but this time, instead of grabbing a still frame, we're actually exporting the video to a new format using the FFmpeg PHP library. We use the x264 codec to create an .mp4 file, which is essentially the most widely supported format for web video.
After encoding, we save the new video to our storage with a new UUID in its filename, update the database record to point to this new file, and clean up by deleting the original, unencoded upload. The episode also touches on the pros and cons of not using Laravel queues for this—while skipping queues for now makes things quicker to set up, it can mean a delay for users uploading larger videos. For now, since our test files are small, it's not a big deal, but if you want, you can easily offload encoding to a queue for better performance as your app grows.
Finally, we walk through testing the encoding by uploading a new video and watching it get processed and transformed into a shareable, browser-friendly format. This sets us up for the next step: adding functionality to manage and share videos with others!