Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe series of images from java application to ffmpeg subprocess

I am looking for a way to stream series of images (jpegs) from java application into FFMpeg STDIN pipe. FFMpeg should process these images and create a video file as an output.

FFMpeg is executed as sub process of java application with the following command "ffmpeg.exe -i pipe:0 out.avi"

When i run "ffmpeg -i input.jpg out.avi" command in the console, i get the "out.avi" file as expected

But when i use the following tester code in my java application, i got an error.

Code in Java application:

File ffmpeg_output_msg = new File("ffmpeg_output_msg.txt");
ProcessBuilder pb = new ProcessBuilder(
        "ffmpeg.exe","-i","pipe:0","out.avi");
pb.redirectErrorStream(true);
pb.redirectOutput(ffmpeg_output_msg);
pb.redirectInput(ProcessBuilder.Redirect.PIPE);
Process p = pb.start();
OutputStream ffmpegInput = p.getOutputStream();

byte[] image;
File file = new File("input.jpg");
image = new byte[(int)file.length()];

FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(image);

ImageInputStream iis = ImageIO.createImageInputStream(
        new ByteArrayInputStream(image));
BufferedImage img = ImageIO.read(iis);

ImageIO.write(img, "JPEG", ffmpegInput);

FFMpeg output:

ffmpeg version N-59664-g94cf4f8 Copyright (c) 2000-2014 the FFmpeg developers built on Jan 7 2014 22:07:02 with gcc 4.8.2 (GCC)
configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib

libavutil      52. 62.100 / 52. 62.100
libavcodec     55. 47.100 / 55. 47.100
libavformat    55. 22.102 / 55. 22.102
libavdevice    55.  5.102 / 55.  5.102
libavfilter     4.  0.103 /  4.  0.103
libswscale      2.  5.101 /  2.  5.101
libswresample   0. 17.104 /  0. 17.104
libpostproc    52.  3.100 / 52.  3.100

pipe:: Invalid data found when processing input

Any ideas how to make it work?

Thank you very much for your time.

like image 961
Marco Avatar asked Jan 10 '14 10:01

Marco


2 Answers

First make sure the exact command you use in the code works from the command line. It appears that for pipes the format and codec must be specified manually:

ffmpeg -f image2pipe -codec mjpeg -i pipe:0 out.avi < input.jpg

The Java program itself looks fine.

like image 180
tom Avatar answered Oct 19 '22 20:10

tom


I don't think you should be redirecting the input. By default the ProcessBuilder builds a process that reads input from a pipe.

like image 27
mikea Avatar answered Oct 19 '22 19:10

mikea