ffmpeg-cli-wrapper
ffmpeg-cli-wrapper copied to clipboard
Add support for input and output streams
For example:
ffmpeg -re -y -i '/file.mp4' -c:v libx264 -f flv 'rtmp://server.com:80/live/...'
setInput() and setOutput() accept Strings right now. Add a File and URL version of the arguments, which would then allow for using URLs as input/output.
And don't forget to handle Path (available since Java 7) which is a better implementation of file system in Java.
It would be nice to also set an InputStream as the input, and an OutputStream as the output? Unless there is a way to do that currently! :)
@bramp was just going to file something to support the idea of an 'ongoing' ffmpeg process (say, capturing from the desktop and recording to a file or pushing to youtube like you have here). is this something you've given any more thought to? any idea how much work would be involved? i've got a use case like this and if it wasn't too bad i could take a shot at implementing it in ffmpeg-cli-wrapper.
example ffmpeg command of what i'm talking about:
ffmpeg -y -v info -f x11grab -draw_mouse 0 -r 30 -s 1280x720 -thread_queue_size 4096 -i :0.0+0,0 -f alsa -thread_queue_size 4096 -i hw:0,1,0 -acodec aac -strict -2 -ar 44100 -c:v libx264 -preset veryfast -maxrate 2976k -bufsize 5952k -pix_fmt yuv420p -r 30 -crf 25 -g 60 -tune zerolatency -f flv rtmp://a.rtmp.youtube.com/XXXX
@bbaldino if I understand your request, you can already do that today. Just start the ffmpeg Process, and let it do its thing. You can also use a ProgressListener to capture some stats from ffmpeg as it goes.
More on topic for this bug, of adding input/output streams. I didn't finish it, but I have an example here where I stream the ffmpeg cli output to stdout, and then read it into Java via the p.getInputStream().
In both cases, I would not be opposed to making the API simpler to use.
hey thanks @bramp. are you talking about just doing:
FFmpeg ffmpeg = new FFmpeg();
ffmpeg.run(<list of args>);
?
I gave that a try but got an ffmpeg error (even though the same command pasted into the shell worked correctly). In fact, this error was the reason I stumbled onto ffmpeg-cli-wrapper...I was seeing this same error when running ffmpeg directly via ProcessBuilder and thought perhaps this lib would be able to do it successfully. I think it must be something about some shell expansion, as running the command from ProcessBuilder via /bin/sh -c worked, but then I don't get the PID of the ffmpeg process from the returned Process object (just the pid of the shell that's launched, I think), which I need.
Taking a random example from https://github.com/bramp/ffmpeg-cli-wrapper/blob/master/src/test/java/net/bramp/ffmpeg/nut/NutReaderTest.java#L38:
List<String> args =
new FFmpegBuilder()
.setInput(Samples.big_buck_bunny_720p_1mb)
.addStdoutOutput()
.setFormat("nut")
.setVideoCodec("rawvideo")
.setVideoPixelFormat("argb") // 8 bits per channel
.setAudioCodec("pcm_s32le")
.done()
.build();
List<String> newArgs =
ImmutableList.<String>builder().add(FFmpeg.DEFAULT_PATH).addAll(args).build();
ProcessBuilder builder = new ProcessBuilder(newArgs);
Process p = builder.start();
// ...
p.waitFor()
That starts a process, and lets it run until competition. You can later kill the process if needed.
If you don't need the Process object, just do:
FFmpeg ffmpeg = new FFmpeg("/path/to/ffmpeg");
FFmpegExecutor executor = new FFmpegExecutor(ffmpeg);
FFmpegBuilder builder = new FFmpegBuilder()
// .. all the arguments.
.done();
executor.createJob(builder).run();
and the library hides the process from you. Eventually I want to add more support to the Job to provide access to a input/output stream, plus other functions to control that underlying process.
Where I ran into issues was trying to find out how to set something to the equivalent of -f x11grab and input options (input capture framerate and resolution, which makes sense for something like a screen capture). Also setting the gop size for the output. Are these options in there somewhere and I missed them?
You should look at the addExtraArgs method, and perhaps take a look at these random examples.
Awesome, that looks like it might do it. Thanks!