Skip to content
rosariod edited this page Jan 21, 2020 · 22 revisions

I occasionally get asked how to convert a normal FFmpeg command line, into an equivalent FFmpegBuilder. This page maintain a list of examples.

Feel free to add your own examples, or reach out to me.

Example 1: Recording from local webcam, and send to youtube, using a local build of ffmpeg

ffmpeg\win64\bin\ffmpeg.exe \
  -f dshow -rtbufsize 1500M -re \
  -i video=\"Microsoft Camera Rear\":audio=\"Microphone Array (Realtek High Definition Audio(SST))\" \
  -f flv \
  -vcodec libx264 -pix_fmt yuv420p -s 426x240 -r 30/1 -b:v 2000000 \
  -acodec libmp3lame -ar 44100 -b:a 1000000 -bufsize 4000k -maxrate 1000k \
  -profile:v baseline -deinterlace -preset medium -g 30 \
  "rtmp://a.rtmp.youtube.com/live2/1234-5678"
FFmpeg ffmpeg = new FFmpeg("ffmpeg\\win64\\bin\\ffmpeg.exe");

FFmpegBuilder builder =  new FFmpegBuilder()
  .addExtraArgs("-rtbufsize", "1500M")
  .addExtraArgs("-re")
  .setFormat("dshow")
  .setInput("video=\"Microsoft Camera Rear\":audio=\"Microphone Array (Realtek High Definition Audio(SST))\"")
  .addOutput("rtmp://a.rtmp.youtube.com/live2/1234-5678")
    .setFormat("flv")
    .addExtraArgs("-bufsize", "4000k")
    .addExtraArgs("-maxrate", "1000k")

    .setAudioCodec("libmp3lame")
    .setAudioSampleRate(FFmpeg.AUDIO_SAMPLE_44100)
    .setAudioBitRate(1_000_000)

    .addExtraArgs("-profile:v", "baseline")
    .setVideoCodec("libx264")
    .setVideoPixelFormat("yuv420p")
    .setVideoResolution(426, 240)
    .setVideoBitRate(2_000_000)
    .setVideoFrameRate(30)
    .addExtraArgs("-deinterlace")
    .addExtraArgs("-preset", "medium")
    .addExtraArgs("-g", "30")
    .done();

FFmpegExecutor executor = new FFmpegExecutor(ffmpeg);
executor.createJob(builder).run();

Example 2: Setting ogv quality

FFmpegBuilder builder = new FFmpegBuilder()
    .setInput("input.mkv")
    .addOutput("output.ogv")
      .setVideoCodec("libtheora")
      .addExtraArgs("-qscale:v", "7")
      .setAudioCodec("libvorbis")
      .addExtraArgs("-qscale:a", "5")
    .done();

Example 3: Take a thumbnail from a video and scale it down

FFmpegBuilder builder = new FFmpegBuilder()
  .setInput("sample.avi")
  .addOutput("thumbnail.png")
    .setFrames(1)
    .setVideoFilter("select='gte(n\\,10)',scale=200:-1")
    .done();

Example 4: Read from RTSP (IP camera)

and save the video to a set of imgXXX.jpg files

FFmpegBuilder builder =
    new FFmpegBuilder()
        .setInput("rtsp://192.168.1.1:1234/")
        .addOutput("img%03d.jpg")
          .setFormat("image2")
        .done();

Example 5: Set the working directory of ffmpeg

RunProcessFunction func = new RunProcessFunction();
func.setWorkingDirectory("/path/to/working/dir");

FFmpeg ffmpeg = new FFmpeg("/path/to/ffmpeg", func);
FFprobe ffprobe = new FFprobe("/path/to/ffprobe", func);

FFmpegBuilder builder = new FFmpegBuilder()
    .setInput("input")
    .addOutput("output.mp4")
    .done();

FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);

// Run a two-pass encode
executor.createTwoPassJob(builder).run();

Example 6: Create a video from images

FFmpegBuilder builder = new FFmpegBuilder()
    .addInput("image%03d.png")
    .addOutput("output.mp4")
    .setVideoFrameRate(FFmpeg.FPS_24)
    .done();

Example 7: Complex Filters

FFmpegBuilder builder = new FFmpegBuilder()
    .addInput("original.mp4")
    .addInput("spot.mp4")
    .setComplexFilter(
      "[1:v]scale=368:207,setpts=PTS-STARTPTS+5/TB [ov]; " +
      "[0:v][ov] overlay=x=(main_w-overlay_w)/2:y=(main_h-overlay_h)/2:enable='between(t,5,15)' [v]")
    .addOutput("with-video.mp4")
      .addExtraArgs("-map", "[v]")
      .addExtraArgs("-map", "0:a")
      .setVideoCodec("libx264")
      .setPreset("ultrafast")
      .setConstantRateFactor(20)
      .setAudioCodec("copy")
      .addExtraArgs("-shortest")
      .done();

Example 8: Transcode to iOS HEVC format, with video filter set before output

FFmpegBuilder builder = new FFmpegBuilder()
    .addInput("original.mp4")
    .setVideoFilter("select='gte(n\\,10)',scale=200:-1")
    .addOutput("hevc-video.mp4")
    .addExtraArgs("-tag:v", "hvc1")
    .setVideoCodec("libx265")
    .done();

Example 9: Convert a stereo mp3 into two mono tracks.

FFmpegBuilder builder = new FFmpegBuilder()
  .setVerbosity(FFmpegBuilder.Verbosity.DEBUG)
  .setInput("input.mp3")
  .addOutput("left.mp3")
      .addExtraArgs("-map_channel", "0.0.0")
      .done()
  .addOutput("right.mp3")
      .addExtraArgs("-map_channel", "0.0.1")
      .done();

Example 10:Change aspect ratio for a video

FFmpegBuilder builder = new FFmpegBuilder()
			  .setInput(pathFileInput)     // Filename, or a FFmpegProbeResult
			  //.setInput(pathFileLogo)
			  .addInput(pathFileLogo)
			  .overrideOutputFiles(true) // Override the output if it exists
			  .setComplexFilter("overlay="+position_width+":"+position_height+"")
				    
			  .addOutput(pathFileOutput)   // Filename for the destination
			   .setFormat("mp4")        // Format is inferred from filename, or can be set
			   .setAudioBitRate(bitRateAudio)      // at 32 kbit/s*/
			    //.setTargetSize(250_000)  // Aim for a 250KB file

			    //.disableSubtitle()       // No subtiles

			    /*.setAudioChannels(1)         // Mono audio
			    .setAudioCodec("aac")        // using the aac codec
			    .setAudioSampleRate(48_000)  // at 48KHz
			    //.setAudioBitRate(32768)      // at 32 kbit/s*/
			    //.setVideoCodec("libx264")     // Video using x264
			    //.setVideoFrameRate(24, 1)     // at 24 frames per second
			    //.setVideoResolution(640, 480) // at 640x480 resolution
			    .setVideoResolution(height, width) // at 640x480 resolution
			    .setVideoBitRate(bitRateVideo)	    
			    .addExtraArgs("-aspect","16:9")// we can add args not implemented in java wrapper using this line we can write the string "-aspect 4:3" in ffmpeg command line
			    .setStrict(FFmpegBuilder.Strict.EXPERIMENTAL) // Allow FFmpeg to use experimental specs
			    .done();

Example 11:Creating a video setting output channel using some input channel

FFmpegBuilder builder = new FFmpegBuilder()
			  .setInput(pathFileInput)     
			  .overrideOutputFiles(true) // Override the output if it exists
			  .addOutput(pathFileOutput)   // Filename for the destination
			   .setFormat("mp4")        // Format is inferred from filename, or can be set
                           .addExtraArgs("-ac","1")// you need -ac param to use properly -af param with pan value
			   .addExtraArgs("-af","pan=stereo|c0=c0|c1=c1") //we are getting only first and second channel from input and putting in output file in same order. 
//Notice that output is in left side, whereas input is in right side. So if you want mapping the first and third channels of the input to the first and second channels of the output, you must write pan=stereo|c0=c0|c1=c2
			    .setStrict(FFmpegBuilder.Strict.EXPERIMENTAL) // Allow FFmpeg to use experimental specs
			    .done();

Example 12:Increasing a video volume

FFmpegBuilder builder = new FFmpegBuilder()
			  .setInput(pathFileInput)     
			  .overrideOutputFiles(true) // Override the output if it exists
			  .addOutput(pathFileOutput)   // Filename for the destination
			   .setFormat("mp4")        // Format is inferred from filename, or can be set
			   .addExtraArgs("-filter:a","volume=5") // increase volume using formula dBnumber=20*lg(amplitude ratio). For details see link https://lists.ffmpeg.org/pipermail/ffmpeg-user/2013-February/013168.html. To decrease use volume with dB as shown in this link https://trac.ffmpeg.org/wiki/AudioVolume
			    .setStrict(FFmpegBuilder.Strict.EXPERIMENTAL) // Allow FFmpeg to use experimental specs
			    .done();