简体   繁体   中英

Images to video in FFMpeg from within java program

At the minute I am writing BufferedImages to a temp folder then calling ffmpeg to convert them into a video. Is there a way to send the images directly to ffmpeg from within java rather than writing them out first? I am assuming this would be a faster way to do it if it's possible?

This is what I have which is currently working fine reading from a directory.

            ProcessBuilder processBuilder = new ProcessBuilder(
                    "ffmpeg",
                    "-y", 
                    "-r", framerate,
                    "-f", "image2",
                    "-i", imageFramesDir,
                    "-itsoffset", offset.toString(),
                    "-i", audioFile,
                    "-c:v", "libx264",
                    "-c:a", "aac",
                    "-pix_fmt", "yuv420p",
                    "-crf", "23",
                    "-r", "24",
                    "-vf", "scale=720x406,setdar=16:9",
                    outputFile);

            processBuilder.redirectErrorStream(false);
            Process process = processBuilder.start();

When you are inputting images to ffmpeg, you have multiple options such as images sequentially, with a glob pattern, via piping, or with the concat demuxer. Since images are stored in memory, you don't have to write images into local drive. It will decrease application speed and badly affect performance .You can use a pipe to feed images to ffmpeg. Check this link to see more about pipe data to ffmpeg.

Also you can use concat demuxer to create a video from multiple images. Concat demuxer allows you to use multiple location images using single text file. You just have to save image path into a text file and run the following command.

ffmpeg -f concat -safe 0 -i text.txt -vcodec mpeg4  -f mpegts test.mp4

Pipe would be a more solid option for your requirements. But both methods can be applied to your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM