title: Ffmpeg: writing audio and video to a stream in an mpeg format
body: <p>I had the commands for exporting a video stream to an mpeg file working correctly with the following code:</p>
  
  <pre><code>ffmpeg -r 24 -pix_fmt rgba -s 1280x720 -f rawvideo -y -i -vf vflip -vcodec mpeg1video -qscale 4 -bufsize 500KB -maxrate 5000KB OUTPUT_FILE
  </code></pre>
  
  <p>Now, I wanted to add the commands so that audio can be used as well since there's no option for that right now.</p>
  
  <p>I've edited the previous command to the next one:</p>
  
  <pre><code>ffmpeg -r 24 -pix_fmt rgba -s 1280x720 -f rawvideo -y -i -f s16le -ac 1 -ar 44100 -i - -acodec pcm_s16le -ac 1 -b:a 320k -ar 44100 -vf vflip -vcodec mpeg1video -qscale 4 -bufsize 500KB -maxrate 5000KB OUTPUT_FILE
  </code></pre>
  
  <p>So as you can see I added a new input with the settings for the audio I'm going to be inputting (I'm going to test this with the values of a sine wave).</p>
  
  <p>I'm writing the data to the file like this:</p>
  
  <pre><code>// Write a frame to the ffmpeg stream
  fwrite(frame, sizeof(unsigned char*) * frameWidth * frameHeight, 1, ffmpeg); 
  
  // Write multiple sound samples per written frame
  for (int t = 0; t &lt; 44100/24; ++t)
      fwrite(&amp;audio, sizeof(short int), 1, ffmpeg);
  </code></pre>
  
  <p>The first line is the one that only writes the video (where the frame object is a render to texture from the video I'm inputting)
  After that I'm trying to add the audio. I'm using a for-loop so I can use multiple samples per video frame (because otherwise you would only have 24 audio samples per second)</p>
  
  <p>This does render with a couple of issues:</p>
  
  <ul>
  <li><p>The rendered video shows green flashes </p></li>
  <li><p>The video slides across the screen. For example, if it slides 200 pixels to the right those pixels get rendered at the other side. Also a bit of the frame that should be at the bottom is rendered at the top (so the frame also slides down but this is a constant, it doesn't move over time)</p></li>
  </ul>
  
  <p>I can't figure out where my mistake is. I've tried multiple codecs and tried different orders for the commands but it stays the same or gets worse.</p>
  
  <p>Thanks in advance</p>