I'm building a react native app for both Android and IOS, the back-end API is written with NodeJS.
Users may upload video from their phones, once uploaded the user and their friends will be able to view the video - so the videos need to be stored in a format which is playable on both Android & IOS.
My question relates to the conversion of video, uploaded by the user. I developed a similar app a couple of years ago; I used the repo node-fluent-ffmpeg which provides a nice API to interact with FFmpeg.
In the previous project (which was a web app), I converted the uploaded videos into two files, one .mp4 and one .webm - if a user uploaded an mp4, then I would skip the mp4 step, likewise if they uploaded a .webm.
This was kind of slow. Now I've come across the same requirement years later, after some research I think I was wrong to convert the videos to the last project.
I've read that I can simply use FFmpeg to change the container format of the videos, which is a much faster process than converting them from scratch.
The video conversion code I used last time went something along the lines of:
var convertVideo = function (source, format, output, success, failure, progress) {
var converter = ffmpeg(source);
var audioCodec = "libvorbis";
if (format.indexOf("mp4") != -1) {
audioCodec = "aac";
}
converter.format(format)
.withVideoBitrate(1024)
.withAudioCodec(audioCodec)
.on('end', success)
.on('progress', progress)
.on('error', failure);
converter.save(output);
};
Usage:
Convert to mp4:
convertVideo("PATH_TO_VIDEO", "mp4", "foo.mp4", () => {console.log("success");});
Convert to webm:
convertVideo("PATH_TO_VIDEO", "webm", "foo.webm", () => {console.log("success");});
Can anyone point out a code smell here regarding the performance of this operation? Is this code doing a lot more than it should achieve cross-platform compatibility between IOS and Android?
Might be worth mentioning that support for older OS versions is not such a big deal in this project.