Answers.Body: <p>The <code>fs</code> parameter will stop the encode once it hits its value. So, if the output hits the <code>10MB</code> mark while encoding the 15th second, then that's the duration of your output file.</p>
  
  <p>If you want to make sure that the entire file is encoded but it doesn't cross the set target size, then use the bitrate method. To accommodate the muxing overhead and other data within the target size, set your video bitrate slightly lower by, say, 1-2%</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-27T12:31:26.687Z
Answers.LastActivityDate: 2015-12-27T12:31:26.687Z
Answers.UserDisplayName: Gyan
Body: <p>I want to encode a video, that has a length of 60 seconds, to a <strong>target or maximum size of 10 MB</strong>.</p>
  
  <p>There are two approaches I know of. One is explained in the FFMPEG-Wiki, and the other one I found in the documentation. Unfortunately, I haven't found an explanation on when to use what method.</p>
  
  <p>Is any of those methods recommended? If not, what are the upsides/downsides of each method?</p>
  
  <p>1) Calculate and set Bitrate to match the length of the video as explained in the <a href="https://trac.ffmpeg.org/wiki/Encode/H.264#Two-PassExample" rel="noreferrer">ffmpeg-wiki</a></p>
  
  <blockquote>
    <p>(10 MB * 8192 [converts MB to kilobits]) / 60 seconds = ~1365 kbits/s total bitrate
    1365k - 128k (desired audio bitrate) = 1237k video bitrate</p>
  </blockquote>
  
  <pre><code>ffmpeg -y -i input -c:v copy -preset medium -b:v 1237k -pass 1 -c:a copy -b:a 128k -f mp4 /dev/null &amp;&amp; \
  ffmpeg -i input -c:v libx264 -preset medium -b:v 1237k -pass 2 -c:a libfdk_aac -b:a 128k output.mp4
  </code></pre>
  
  <p>2) Use the <code>-fs</code> <a href="https://ffmpeg.org/ffmpeg.html#Main-options" rel="noreferrer">parameter</a> and let ffmpeg figure it out.</p>
  
  <pre><code>ffmpeg -i input -c:v copy -c:a copy -preset medium -crf 23 -fs 10485760 output.mp4
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-12-27T12:35:47.210Z
Comments.Text: In your 1st example, the first step should also use `libx264` as `c:v`. In the `fs` example, you aren't transcoding the video, since `-c:v copy` is used. So, the `preset` and `crf` also don't have any effect.
Comments.UserDisplayName: Gyan
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-03-20T23:59:57.067Z
Comments.Text: This was helpful for me: https://stackoverflow.com/a/34547102/470749
Comments.UserDisplayName: Ryan
Tags: <ffmpeg>
Title: How to limit file size with ffmpeg?
UserDisplayName: Daniel
ThreadText: # How to limit file size with ffmpeg?
  
  (Asked by: username_0 on 2015-12-27)
  
  I want to encode a video, that has a length of 60 seconds, to a **target or maximum size of 10 MB**.
  
  There are two approaches I know of. One is explained in the FFMPEG-Wiki, and the other one I found in the documentation. Unfortunately, I haven't found an explanation on when to use what method.
  
  Is any of those methods recommended? If not, what are the upsides/downsides of each method?
  
  1) Calculate and set Bitrate to match the length of the video as explained in the [ffmpeg-wiki](https://trac.ffmpeg.org/wiki/Encode/H.264#Two-PassExample)
  
  > (10 MB \* 8192 [converts MB to kilobits]) / 60 seconds = ~1365 kbits/s total bitrate
  > 1365k - 128k (desired audio bitrate) = 1237k video bitrate
  
  ```
  ffmpeg -y -i input -c:v copy -preset medium -b:v 1237k -pass 1 -c:a copy -b:a 128k -f mp4 /dev/null && \
  ffmpeg -i input -c:v libx264 -preset medium -b:v 1237k -pass 2 -c:a libfdk_aac -b:a 128k output.mp4
  
  ```
  
  2) Use the `-fs` [parameter](https://ffmpeg.org/ffmpeg.html#Main-options) and let ffmpeg figure it out.
  
  ```
  ffmpeg -i input -c:v copy -c:a copy -preset medium -crf 23 -fs 10485760 output.mp4
  
  ```
  
  **Tags:** ffmpeg
  
  > **Comments:**
  > **username_1**: In your 1st example, the first step should also use `libx264` as `c:v`. In the `fs` example, you aren't transcoding the video, since `-c:v copy` is used. So, the `preset` and `crf` also don't have any effect.  
  > **username_2**: This was helpful for me: https://stackoverflow.com/a/34547102/470749  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-12-27. Score: 16 | Accepted answer
  
  The `fs` parameter will stop the encode once it hits its value. So, if the output hits the `10MB` mark while encoding the 15th second, then that's the duration of your output file.
  
  If you want to make sure that the entire file is encoded but it doesn't cross the set target size, then use the bitrate method. To accommodate the muxing overhead and other data within the target size, set your video bitrate slightly lower by, say, 1-2%
  
  
  ---
  
  