title: ffmpeg, stretch audio to x seconds
body: <p>I am trying to make an audio file be exactly x second.</p>
  
  <p>So far i tried using the <code>atempo</code> filter by doing the following calculation </p>
  
  <p><code>Audio length / desired length = atempo.</code></p>
  
  <p>But this is not accurate, and I am having to tweak the tempo manually to get it to an exact fit. </p>
  
  <p>Are there any other solutions to get this work ? Or am I doing this incorrectly?</p>
  
  <p>My original file is a wav file, and my output in an mp3</p>
  
  <p>Here is a sample command</p>
  
  <pre><code>ffmpeg -i input.wav -codec:a libmp3lame -filter:a "atempo=0.9992323" -b:a 320K output.mp3
  </code></pre>
  
  <p>UPDATE.</p>
  
  <p>I was able to correctly calculate the tempo by changing the way I am receiving the audio length.</p>
  
  <p>I am now calculating the current audio length using the actual file size and the sample rate. </p>
  
  <p>Audio Length = file size / (sample rate * 2)</p>
  
  <p>Sample rate is something like 16000 Hz. You can get that by using ffprob or ffmpeg.</p>
  
  <h2>EDIT - Solved</h2>
  
  <p>Figured it out.
  I was calculating the tempo incorrectly.</p>
  
  <pre><code>Audio length / desired length = atempo
  </code></pre>
  
  <p>Should be</p>
  
  <pre><code>desired length / Audio length = atempo
  </code></pre>
  
  <p>I had to do a bit more reading on what the tempo actually does. Hope this helps someone.</p>