Answers.Body: <p>What you can do is use <code>-shortest</code> to stop encoding once the shortest stream ends. This option needs to be given before the output file.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-01T10:52:40.933Z
Answers.LastActivityDate: 2021-12-01T10:52:40.933Z
Answers.UserDisplayName: v010dya
Body: <p>I am trying to generate a video from a bunch of png images and audio using the command:</p>
  <pre><code>/usr/bin/ffmpeg -y -framerate 30 -pattern_type glob -i 'images/*.png' -i audio.wav -c:v libx264 -b:v 0 -crf 18 -g 300000 -pix_fmt yuv420p -shortest output.mp4
  </code></pre>
  <p>There are a total of 52 images with their FFprobe output as below:</p>
  <pre><code>Input #0, png_pipe, from 'images/0000000.png':
    Duration: N/A, bitrate: N/A
      Stream #0:0: Video: png, rgb24(pc), 1200x1000, 25 tbr, 25 tbn, 25 tbc
  </code></pre>
  <p>And FFprobe output for audio.wav is:</p>
  <pre><code>Input #0, wav, from 'audio.wav':
    Metadata:
      encoder         : Lavf58.29.100
    Duration: 00:00:01.74, bitrate: 256 kb/s
      Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, 1 channels, s16, 256 kb/s
  </code></pre>
  <p>At 30 fps, the length of output video should be 1.734s, but actually, it is 1.80s.</p>
  <p>FFprobe output for final video:</p>
  <pre><code>Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output.mp4':
    Metadata:
      major_brand     : isom
      minor_version   : 512
      compatible_brands: isomiso2avc1mp41
      encoder         : Lavf58.29.100
    Duration: 00:00:01.80, start: 0.000000, bitrate: 1627 kb/s
      Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1200x1000, 1603 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
      Metadata:
        handler_name    : VideoHandler
      Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 16000 Hz, mono, fltp, 74 kb/s (default)
      Metadata:
        handler_name    : SoundHandler
  </code></pre>
  <p>Can someone help me understand why is there a difference and how can I remove it?</p>
  <p>My use case requires that the video is of the same length as that of audio otherwise it will cause issues. Any help will be appreciated.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-04-11T06:42:10.223Z
Comments.Text: Have you got any explanation?
Comments.UserDisplayName: Sam
Tags: <ffmpeg><encoding>
Title: How to fix the difference between duration of ffmpeg input and output?
UserDisplayName: sy_ar001
ThreadText: # How to fix the difference between duration of ffmpeg input and output?
  
  (Asked by: username_0 on 2020-11-20)
  
  I am trying to generate a video from a bunch of png images and audio using the command:
  
  ```
  /usr/bin/ffmpeg -y -framerate 30 -pattern_type glob -i 'images/*.png' -i audio.wav -c:v libx264 -b:v 0 -crf 18 -g 300000 -pix_fmt yuv420p -shortest output.mp4
  
  ```
  
  There are a total of 52 images with their FFprobe output as below:
  
  ```
  Input #0, png_pipe, from 'images/0000000.png':
    Duration: N/A, bitrate: N/A
      Stream #0:0: Video: png, rgb24(pc), 1200x1000, 25 tbr, 25 tbn, 25 tbc
  
  ```
  
  And FFprobe output for audio.wav is:
  
  ```
  Input #0, wav, from 'audio.wav':
    Metadata:
      encoder         : Lavf58.29.100
    Duration: 00:00:01.74, bitrate: 256 kb/s
      Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, 1 channels, s16, 256 kb/s
  
  ```
  
  At 30 fps, the length of output video should be 1.734s, but actually, it is 1.80s.
  
  FFprobe output for final video:
  
  ```
  Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output.mp4':
    Metadata:
      major_brand     : isom
      minor_version   : 512
      compatible_brands: isomiso2avc1mp41
      encoder         : Lavf58.29.100
    Duration: 00:00:01.80, start: 0.000000, bitrate: 1627 kb/s
      Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1200x1000, 1603 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
      Metadata:
        handler_name    : VideoHandler
      Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 16000 Hz, mono, fltp, 74 kb/s (default)
      Metadata:
        handler_name    : SoundHandler
  
  ```
  
  Can someone help me understand why is there a difference and how can I remove it?
  
  My use case requires that the video is of the same length as that of audio otherwise it will cause issues. Any help will be appreciated.
  
  **Tags:** ffmpeg, encoding
  
  > **Comments:**
  > **username_1**: Have you got any explanation?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2021-12-01. Score: 1
  
  What you can do is use `-shortest` to stop encoding once the shortest stream ends. This option needs to be given before the output file.
  
  
  ---
  
  