title: Java - Write Video and Audio at the same time to FFmpeg
body: <p>Hello fellow developers,</p>
  
  <p>I'm currently developing a tool which can render videos by executing FFmpeg using a Java Process and feeding video frames to it.</p>
  
  <p>I currently use the following FFmpeg command:
  <code>ffmpeg -y -f rawvideo -pix_fmt rgb24 -s %WIDTH%x%HEIGHT% -r %FPS% -i - -an -c:v libx264 -preset ultrafast -pix_fmt yuv420p "%FILENAME%.mp4"</code>,
  where the placeholders are obviously replaced with real values.</p>
  
  <p>The code I'm using to initialize FFmpeg:</p>
  
  <pre><code>    //commandArgs is a list of command line arguments for FFmpeg
  
      List&lt;String&gt; command = new ArrayList&lt;String&gt;();
      command.add("ffmpeg");
      command.addAll(commandArgs);
  
      process = new ProcessBuilder(command).directory(outputFolder).start();
      OutputStream exportLogOut = new FileOutputStream("export.log");
      new StreamPipe(process.getInputStream(), exportLogOut).start();
      new StreamPipe(process.getErrorStream(), exportLogOut).start();
      outputStream = process.getOutputStream();
      channel = Channels.newChannel(outputStream);
  </code></pre>
  
  <p>Then, I have the following method to write a ByteBuffer containing a Video Frame to FFmpeg:</p>
  
  <pre><code>public void consume(ByteBuffer buf) {
      try {
          channel.write(buf);
          ByteBufferPool.release(buf);
      } catch(Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Now, my question is, how would I go ahead and write synchronous Audio Data to the output file? I assume that I need to use multiple pipes, and of course I will have to modify my command line arguments, but I need help:</p>
  
  <pre><code>1) what kind of Audio Data do I need to feed FFmpeg with?
  2) how do I feed Audio and Video in one go?
  3) how do I keep Audio and Video synchronized?
  </code></pre>
  
  <p>Thanks in advance for any help!</p>
  
  <p>Greetings,
  CrushedPixel</p>