<p>One should use the avformat library flag to stop muxing with the shortest stream.</p>

<pre><code>ffmpeg -i file.mp4 -map 0 -c copy -fflags +shortest -max_interleave_delta 1G -movflags +faststart out.mp4
</code></pre>

CC BY-SA 4.0
2019-08-12T02:39:59.757Z
This ended up doing some kind of partial truncation on the mp4 file. I'm not sure what was going on. However, this approach did work to correct the mkv file, so I have a proper source from which to encode my mp4. Many thanks!

Final command line: `ffmpeg -i file.mkv -map 0 -c copy -fflags +shortest -max_interleave_delta 0 out.mkv`
Stephen Cleary
CC BY-SA 4.0
2019-10-19T01:57:35.237Z
@StephenCleary: Annoyingly the `shortest` logic *also* applies to subtitle tracks, which are usually shorter than both the video and audio :(  I had to strip off the subtitles in the initial shortening re-mux: `ffmpeg -i original.mkv -c copy -sn -fflags +shortest -max_interleave_delta 0 trimmed-no-subs.mkv`, then glue the subtitles back in afterward: `ffmpeg -i trimmed-no-subs.mkv -i original.mkv -map 0 -map 1:s -c copy trimmed.mkv`
genpfault
CC BY-SA 4.0
2019-08-03T18:55:13.910Z
2019-08-03T18:55:13.910Z
Gyan
<p>I have an mp4 file (converted from an mkv file) that has a longer audio than video stream. This file doesn't play back nicely in my media streaming app (or MPC-HC) when I reach the end of the file.</p>

<p>Example output from mp4info:</p>

<pre><code>File:
  major brand:      mp42
  minor version:    200
  compatible brand: isom
  compatible brand: iso2
  compatible brand: avc1
  compatible brand: mp41
  fast start:       yes

Movie:
  duration:   1553152 ms
  time scale: 1000
  fragments:  no

Found 3 Tracks
Track 1:
  flags:        3 ENABLED IN-MOVIE
  id:           1
  type:         Video
  duration: 1524398 ms
  language: und
  media:
    sample count: 36549
    timescale:    90000
    duration:     137195807 (media timescale units)
    duration:     1524398 (ms)
    bitrate (computed): 8508.739 Kbps
  display width:  1444.000000
  display height: 1080.000000
  frame rate (computed): 23.976
  Sample Description 0
    Coding:      avc1 (H.264)
    Width:       1444
    Height:      1080
    Depth:       24
    AVC Profile:          77 (Main)
    AVC Profile Compat:   40
    AVC Level:            40
    AVC NALU Length Size: 4
    AVC SPS: [674d4028eca02d8113cf2e02d404040500000300010002bf200f183196]
    AVC PPS: [68cb8132c8]
    Codecs String: avc1.4D4028
Track 2:
  flags:        3 ENABLED IN-MOVIE
  id:           2
  type:         Audio
  duration: 1553152 ms
  language: eng
  media:
    sample count: 72804
    timescale:    48000
    duration:     74551296 (media timescale units)
    duration:     1553152 (ms)
    bitrate (computed): 125.679 Kbps
  Sample Description 0
    Coding:      mp4a (MPEG-4 Audio)
    Stream Type: Audio
    Object Type: MPEG-4 Audio
    Max Bitrate: 125678
    Avg Bitrate: 125678
    Buffer Size: 0
    Codecs String: mp4a.40.2
    MPEG-4 Audio Object Type: 2 (AAC Low Complexity)
    MPEG-4 Audio Decoder Config:
      Sampling Frequency: 48000
      Channels: 1
      Extension:
        Object Type: Spectral Band Replication
        SBR Present: no
        PS Present:  no
        Sampling Frequency: 0
    Sample Rate: 48000
    Sample Size: 16
    Channels:    2
Track 3:
  flags:        2 IN-MOVIE
  id:           3
  type:         Text
  duration: 1524356 ms
  language: ```
  media:
    sample count: 5
    timescale:    1000
    duration:     1524356 (media timescale units)
    duration:     1524356 (ms)
    bitrate (computed): 0.001 Kbps
  Sample Description 0
    Coding:      text
</code></pre>

<p>Specifically, the "Movie" duration is 1553152ms (25:53.152), which is too long. The duration of the video stream is 1524398ms (25:24.398), which is correct. The duration of the audio stream is 1553152ms (25:53.152), and seems to be driving the "Movie" duration.</p>

<p>I want to set the "Movie" duration to the video duration value <em>without re-encoding</em>. It seems like this would be an easy thing to do, but I can't find any solution.</p>

<p>I've tried:</p>

<ul>
<li><code>ffmpeg -i file.mp4 -shortest -codec copy out.mp4</code> - doesn't have any effect (other than removing the mp4 faststart, which I want to keep).</li>
<li><code>mp4box -add file.mp4 -new out.mp4</code> - also doesn't fix the duration.</li>
<li>Remuxing the original mkv file using MkvToolnix.</li>
</ul>

<p><code>ffmpeg</code> with the <code>-t</code> option may work, but I've got a few thousand files to go through and I don't want to manually type a different command for each one.</p>

<hr/>

<p>More details:</p>

<p>I have a bunch of BluRays that I ripped a couple of years ago with MakeMKV 1.10.4. The MKV files themselves have the same problem: the segment duration is 25:53.088, with a track 1 movie DURATION tag of 25:24.397875. The track 2 (audio) movie DURATION tag is 25:53.14133333. Re-ripping the BluRays is an acceptable solution, if this problem has been fixed (I haven't pulled the BluRays out of storage and tried it).</p>

<p>I converted these MakeMKV mkv files using HandBrake into h265 mkv files. These files also have the segment duration vs movie DURATION tag mismatch.</p>

<p>When playing the MKV files, my media streaming app (and MPC-HC) correctly determine the length of the file from the movie DURATION tag. There's no problem there, even though the mkv file durations are wrong.</p>

<p>What I want to do is enable several older players by encoding to h264 mp4s with faststart. I believe my media streaming app will then be able to serve the files directly instead of re-encoding to h264 or remuxing (depending on the client, it always does one or the other right now).</p>

<p>I have HandBrake all set up with my new h264/mp4 settings and would like to continue using that application to re-encode the MakeMKV source files.</p>

<p>An ideal solution would be to fix the mkv segment duration in the MakeMKV source files. Fixing the mp4 files after the fact would also be acceptable, but at that point I don't want a re-encode.</p>

<p>Versions are current on everything except MakeMkv since I ripped these a couple years ago:</p>

<pre><code>ffmpeg 4.1.4
mp4info 1.3.4 (Bento4 1.5.1.0)
MKVToolNix 35.0.0
MP4Box 0.8.0-rev47-gbafe4cd3-master
MakeMKV 1.10.4
</code></pre>

<ffmpeg><mp4><mkv><handbrake>
Trim audio stream to video stream length
Stephen Cleary
# Trim audio stream to video stream length

(Asked by: username_0 on 2019-08-03)

I have an mp4 file (converted from an mkv file) that has a longer audio than video stream. This file doesn't play back nicely in my media streaming app (or MPC-HC) when I reach the end of the file.
Example output from mp4info:
File:
  major brand:      mp42
  minor version:    200
  compatible brand: isom
  compatible brand: iso2
  compatible brand: avc1
  compatible brand: mp41
  fast start:       yes

Movie:
  duration:   1553152 ms
  time scale: 1000
  fragments:  no

Found 3 Tracks
Track 1:
  flags:        3 ENABLED IN-MOVIE
  id:           1
  type:         Video
  duration: 1524398 ms
  language: und
  media:
    sample count: 36549
    timescale:    90000
    duration:     137195807 (media timescale units)
    duration:     1524398 (ms)
    bitrate (computed): 8508.739 Kbps
  display width:  1444.000000
  display height: 1080.000000
  frame rate (computed): 23.976
  Sample Description 0
    Coding:      avc1 (H.264)
    Width:       1444
    Height:      1080
    Depth:       24
    AVC Profile:          77 (Main)
    AVC Profile Compat:   40
    AVC Level:            40
    AVC NALU Length Size: 4
    AVC SPS: [674d4028eca02d8113cf2e02d404040500000300010002bf200f183196]
    AVC PPS: [68cb8132c8]
    Codecs String: avc1.4D4028
Track 2:
  flags:        3 ENABLED IN-MOVIE
  id:           2
  type:         Audio
  duration: 1553152 ms
  language: eng
  media:
    sample count: 72804
    timescale:    48000
    duration:     74551296 (media timescale units)
    duration:     1553152 (ms)
    bitrate (computed): 125.679 Kbps
  Sample Description 0
    Coding:      mp4a (MPEG-4 Audio)
    Stream Type: Audio
    Object Type: MPEG-4 Audio
    Max Bitrate: 125678
    Avg Bitrate: 125678
    Buffer Size: 0
    Codecs String: mp4a.40.2
    MPEG-4 Audio Object Type: 2 (AAC Low Complexity)
    MPEG-4 Audio Decoder Config:
      Sampling Frequency: 48000
      Channels: 1
      Extension:
        Object Type: Spectral Band Replication
        SBR Present: no
        PS Present:  no
        Sampling Frequency: 0
    Sample Rate: 48000
    Sample Size: 16
    Channels:    2
Track 3:
  flags:        2 IN-MOVIE
  id:           3
  type:         Text
  duration: 1524356 ms
  language: ```
  media:
    sample count: 5
    timescale:    1000
    duration:     1524356 (media timescale units)
    duration:     1524356 (ms)
    bitrate (computed): 0.001 Kbps
  Sample Description 0
    Coding:      text

Specifically, the "Movie" duration is 1553152ms (25:53.152), which is too long. The duration of the video stream is 1524398ms (25:24.398), which is correct. The duration of the audio stream is 1553152ms (25:53.152), and seems to be driving the "Movie" duration.
I want to set the "Movie" duration to the video duration value without re-encoding. It seems like this would be an easy thing to do, but I can't find any solution.
I've tried:

ffmpeg -i file.mp4 -shortest -codec copy out.mp4 - doesn't have any effect (other than removing the mp4 faststart, which I want to keep).
mp4box -add file.mp4 -new out.mp4 - also doesn't fix the duration.
Remuxing the original mkv file using MkvToolnix.

ffmpeg with the -t option may work, but I've got a few thousand files to go through and I don't want to manually type a different command for each one.

More details:
I have a bunch of BluRays that I ripped a couple of years ago with MakeMKV 1.10.4. The MKV files themselves have the same problem: the segment duration is 25:53.088, with a track 1 movie DURATION tag of 25:24.397875. The track 2 (audio) movie DURATION tag is 25:53.14133333. Re-ripping the BluRays is an acceptable solution, if this problem has been fixed (I haven't pulled the BluRays out of storage and tried it).
I converted these MakeMKV mkv files using HandBrake into h265 mkv files. These files also have the segment duration vs movie DURATION tag mismatch.
When playing the MKV files, my media streaming app (and MPC-HC) correctly determine the length of the file from the movie DURATION tag. There's no problem there, even though the mkv file durations are wrong.
What I want to do is enable several older players by encoding to h264 mp4s with faststart. I believe my media streaming app will then be able to serve the files directly instead of re-encoding to h264 or remuxing (depending on the client, it always does one or the other right now).
I have HandBrake all set up with my new h264/mp4 settings and would like to continue using that application to re-encode the MakeMKV source files.
An ideal solution would be to fix the mkv segment duration in the MakeMKV source files. Fixing the mp4 files after the fact would also be acceptable, but at that point I don't want a re-encode.
Versions are current on everything except MakeMkv since I ripped these a couple years ago:
ffmpeg 4.1.4
mp4info 1.3.4 (Bento4 1.5.1.0)
MKVToolNix 35.0.0
MP4Box 0.8.0-rev47-gbafe4cd3-master
MakeMKV 1.10.4

**Tags:** ffmpeg, mp4, mkv, handbrake


---

## 1 answers

### Answer by username_1 on 2019-08-03. Score: 4 | Accepted answer

One should use the avformat library flag to stop muxing with the shortest stream.
ffmpeg -i file.mp4 -map 0 -c copy -fflags +shortest -max_interleave_delta 1G -movflags +faststart out.mp4

> **Comments:**
> **username_0**: This ended up doing some kind of partial truncation on the mp4 file. I'm not sure what was going on. However, this approach did work to correct the mkv file, so I have a proper source from which to encode my mp4. Many thanks!  Final command line: `ffmpeg -i file.mkv -map 0 -c copy -fflags +shortest -max_interleave_delta 0 out.mkv`  
> **username_2**: @StephenCleary: Annoyingly the `shortest` logic *also* applies to subtitle tracks, which are usually shorter than both the video and audio :(  I had to strip off the subtitles in the initial shortening re-mux: `ffmpeg -i original.mkv -c copy -sn -fflags +shortest -max_interleave_delta 0 trimmed-no-subs.mkv`, then glue the subtitles back in afterward: `ffmpeg -i trimmed-no-subs.mkv -i original.mkv -map 0 -map 1:s -c copy trimmed.mkv`  

---

