blog image

Video conversion for cloud microservices

Cloud native development often brings challenges to serve different scenarios and best fit solution is always matter of selecting the right approach. In todays smartphones world where everybody has a smartphone with high definition camera there are often usecases to upload video to cloud.

This is orchestrated by backend component. Different smartphones of various platforms upload different formats. The orchestration components has to accept all possible formats such as mpeg4, webm, avi and others , convert them to defined format and process or store. The designer of this process can use cloud native service to convert the videos such as OCI Media Services in OCI cloud or AWS Elemental Media Convert or Azure Media Services. This way the solution has a cloud locking and for big amount of files it can be expensive. Sometimes due to other aspects the conversion has to be done within the component.

When you do a research or by asking trained LLM you get answer that there is no dedicated java lib for this purpose. The reason is obvious. The video processing and conversion has high demand on CPU and memory and you cannot do it in java. For this purpose cross platform solution to convert videos, stream and record called FFmpeg is available. You need to install it with binaries into your linux docked and you can call it from java or command line.

Typical installation for alpine docker is 

USER root
RUN apk add ffmpeg

in the dockerfile.jvm

From java code in the quarkus framework you can then call the conversion

if (fileExtension.equals("webm")) {
    // Convert video to mpeg if the file is a webm video
    String convertedFilePath = tempFile.toString().replace(".webm", ".mp4");
    videoConversionService.convertWebmToMpeg(tempFile.toFile(), convertedFilePath, sessionId);
    Files.delete(tempFile);
    tempFile = Path.of(convertedFilePath);
    contentType = Files.probeContentType(tempFile);
    logger.info("[ID-SCAN] Converted file: {} for sessionId: {}", tempFile, sessionId);
}

for the new class

package ch.mobi.dki.integration.control;
import io.quarkus.runtime.annotations.RegisterForReflection;
import jakarta.enterprise.context.ApplicationScoped;
import org.slf4j.Logger;
import jakarta.inject.Inject;

import java.io.File;
import java.io.IOException;

@ApplicationScoped
@RegisterForReflection
public class VideoConversionService {

    @Inject
    Logger logger;

    public File convertWebmToMpeg(File webmFile, String outputPath, String sessionId) throws IOException, InterruptedException {
        logger.info("[ID-SCAN] Starting WebM to MPEG conversion for file: {} for Session Id {}", webmFile.getName(), sessionId);


        ProcessBuilder pb = new ProcessBuilder(
                "ffmpeg",
                "-y",
                "-i", webmFile.getAbsolutePath(),
                "-c:v", "libx264", "-preset", "medium", "-crf", "23",
                "-c:a", "aac", "-b:a", "192k",
                "-movflags", "+faststart",
                outputPath
        );

        Process process = startProcess(pb);
        int exitCode = process.waitFor();

        if (exitCode != 0) {
            throw new IOException("FFmpeg conversion failed with exit code: " + exitCode);
        }

        logger.info("[ID-SCAN] WebM to MPEG conversion completed. Output: {} for the Session {}", outputPath, sessionId);
        return new File(outputPath);
    }

    Process startProcess(ProcessBuilder pb) throws IOException {
        return pb.start();
    }
}

This mimic the call from command line with additional parametersbut in simplified form :

> ffmpeg -i -input.webm output.mp4

This solution prevents cloud and vendor locking but gives higher demand on compute instance cpu, memory and usage. it is good to know this option in case you have requirement defining not using cloud services.




Skôr ako začneme: nahliadnite do spracovania vašich osobných údajov

Ak navštívite stránku, ktorá zapisuje cookies, v počítači sa vám vytvorí malý textový súbor, ktorý sa uloží vo vašom prehliadači. Ak rovnakú stránku navštívite nabudúce, pripojíte sa vďaka nemu na web rýchlejšie. Náš web vám ponúkne relevantné informácie a bude sa vám pracovať jednoduchšie.

Súbory cookies používame najmä na anonymnú analýzu návštevnosti a vylepšovanie našich web stránok. Ak si nastavíte blokovanie zápisu cookies do vášho prehliadača, je možné, že web sa spomalí a niektoré jeho časti nemusia fungovať úplne korektne. Viac info k spracúvaniu cookies.