<p>Usually audio recording is just a continuous steam, with no concept of video frames. The audio is just being encoded/output via a continuous buffer, which will just dump out wherever it is up to you when you stop recording video frames - it is not uncommon for there to be more (or even less) audio in the file than is actually required for the corresponding video.</p>

<p>I would try a de-mux/re-mux of the video files before concatenating, to see if that cleans things up. With the de-muxed files as well, you could calculate how much audio is actually there in the file vs the video play time. I don't have enough experience with ffmpeg to know if this will work, just crossing my fingers that when you re-mux the files it might do a calculation of the required audio length, and trim it appropriately for you (there might also be parameters available to force the audio to trim at the video length, if that doesn't happen by default).</p>

<p>An editor like iMovie is going to properly cut up the audio to exactly match the video cuts, which is why that works. You could also try outputting a single unedited clip from iMovie, and then de-mux that, and see how the audio length differs from the original. Not sure if iMovie lets you output "audio only", but be careful trying that - I know with Adobe Premiere, if I output an audio only WAV, it will differ in length (longer file) than the same clip being output with a video stream as well.</p>

<p>Sorry I can't offer anything more definitive than some guesses/suggestions, I'm fairly positive the root cause is audio length not matching the video in the original file, but I'm not familiar enough with ffmpeg to know the best way to resolve the issue using that tool.</p>

<h2>Update from original poster</h2>

<p>Import the files in question into a program like iMovie and check the beginnings and endings of each file. Does the video stream have more length than the audio stream? Or try to play it in a program like VLC: do you hear a drop in the sound at the end or the beginning?</p>

<p>If so, then your camera, for whatever reason, records more video than sound. You can deal with this quickly in FFMPEG by trimming each clip to the shortest stream:</p>

<pre><code>ffmpeg -i input.MOV -map 0 -c copy -fflags +shortest -max_interleave_delta 0 output.MOV
</code></pre>

<p>You may need to play with <code>-max_interleave_delta</code> depending on your files. From the <a href="https://ffmpeg.org/ffmpeg-formats.html#Format-Options" rel="nofollow noreferrer">documentation</a>:</p>

<blockquote>
  <p><code>‘shortest’</code></p>
  
  <p>Stop muxing at the end of the shortest stream. It may be needed to increase <code>max_interleave_delta</code> to avoid flushing the longer streams before EOF. </p>
</blockquote>

<p>The command is from <a href="https://video.stackexchange.com/questions/28187/trim-audio-stream-to-video-stream-length">Trim audio stream to video stream length</a> .</p>

CC BY-SA 4.0
2019-09-23T18:04:05.800Z
That was it indeed! I opened the files in iMovie and saw that the video stream for the clips was 200 ms longer than the audio stream. I had 6 clips, so the total misalignment was around 1 second, which explains and solves the problem.
emonigma
CC BY-SA 4.0
2019-09-23T18:20:02.020Z
I trimmed each clip to the shortest stream (`ffmpeg -i input.MOV -map 0 -c copy -fflags +shortest -max_interleave_delta 0 output.MOV`, which I found on [this thread](https://video.stackexchange.com/questions/28187/trim-audio-stream-to-video-stream-length)), and that helps a lot. I measured the discrepancy from a segment where someone bangs a chair: it is 0.5 seconds with plain FFMPEG concatenation, it's 0.08 seconds with shortest stream clipping, and it's zero in iMovie. I would like to solve this smaller discrepancy too. I think that would make more sense as a new question. What do you think?
emonigma
CC BY-SA 4.0
2019-09-25T06:54:54.340Z
@miguelmorin - I would agree, post it up as a new question. You're trying to solve  a different problem now, and you have more specific info at this point (it would probably be messy to re-edit this older post and get an appropriate response). It also sounds like a more specific ffmpeg issue with your current solution now, it sounds like ffmpeg isn't trimming the audio with the video as cleanly as iMovie.
Peter Barton
CC BY-SA 4.0
2019-09-27T15:42:06.253Z
@PeterBarton  *Usually audio recording is just a continuous steam, with no concept of video frames.* --> no, compressed audio is framed and thus quantized. See https://superuser.com/a/1155353
Gyan
CC BY-SA 4.0
2019-08-11T13:23:07.357Z
2019-09-29T02:40:40.790Z
2019-09-29T02:40:40.790Z
Peter Barton
<p>I have several <code>MOV</code> files from a DSLR camera. I concatenate them with directions <a href="https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg">from this thread</a>:</p>

<pre><code>ffmpeg -safe 0 -f concat -i files_to_combine -vcodec copy -acodec copy temp.MOV
</code></pre>

<p>where <code>files_to_combine</code> is:</p>

<pre><code>file ./DSC_0013.MOV
...
file ./DSC_0019.MOV
</code></pre>

<p>The result has image and sound in sync for the first clip and is out of sync by fractions of a second in the second clip, and out of sync by around a second for the last clip. It is probably related to this error from the log:</p>

<pre><code>[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f82dd802200] st: 0 edit list: 1 Missing key frame while searching for timestamp: 1000
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f82dd802200] st: 0 edit list 1 Cannot find an index entry before timestamp: 1000.
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f82dd802200] Auto-inserting h264_mp4toannexb bitstream filter
</code></pre>

<p>How can I trim the frames to the available sound stream, then concatenate the two videos?</p>

<p>The full log from the <code>ffmpeg</code> command is:</p>

<pre><code>ffmpeg version 4.1.3 Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple LLVM version 10.0.1 (clang-1001.0.46.4)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1.3_1 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f82dc00e000] Auto-inserting h264_mp4toannexb bitstream filter
Input #0, concat, from 'files_to_combine':
  Duration: N/A, start: -0.592000, bitrate: 36888 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, smpte170m/bt709/bt470m), 1920x1080, 35352 kb/s, 50 fps, 50 tbr, 50k tbn, 100 tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: pcm_s16le (sowt / 0x74776F73), 48000 Hz, stereo, s16, 1536 kb/s
    Metadata:
      handler_name    : SoundHandler
Output #0, mov, to 'temp.MOV':
  Metadata:
    encoder         : Lavf58.20.100
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, smpte170m/bt709/bt470m), 1920x1080, q=2-31, 35352 kb/s, 50 fps, 50 tbr, 50k tbn, 50k tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: pcm_s16le (sowt / 0x74776F73), 48000 Hz, stereo, s16, 1536 kb/s
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -&gt; #0:0 (copy)
  Stream #0:1 -&gt; #0:1 (copy)
Press [q] to stop, [?] for help
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f82dd802200] st: 0 edit list: 1 Missing key frame while searching for timestamp: 1000
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f82dd802200] st: 0 edit list 1 Cannot find an index entry before timestamp: 1000.
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f82dd802200] Auto-inserting h264_mp4toannexb bitstream filter
frame=41886 fps=547 q=-1.0 Lsize= 3789826kB time=00:13:58.75 bitrate=37014.8kbits/s speed=10.9x    
video:3631879kB audio:157123kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.021759%
</code></pre>

<h2>Update (1 July 2019)</h2>

<p>I thought that the files had a problem at the beginning or at the end, so I
trimmed one second from each end, but it still had the sound out of sync:</p>

<pre><code>FILES=files_to_combine
OUTPUT=show2.MOV
rm $FILES
for i in 3 4 5 6 7 8 9; do
    rm ${i}.MOV
    duration=$(ffprobe -v 0 -show_entries format=duration -of compact=p=0:nk=1  DSC_001${i}.MOV)
    trimmed=$(echo $duration - 1 | bc)
    ffmpeg -ss 1 -t $trimmed -i DSC_001${i}.MOV -vcodec copy -acodec copy ${i}.MOV
    echo file ./${i}.MOV &gt;&gt; $FILES
done

rm $OUTPUT
ffmpeg -safe 0 -f concat -i $FILES -vcodec copy -acodec copy $OUTPUT
</code></pre>

<p>When I trim a single file near the end, the sound and video do not seem out of sync:</p>

<pre><code>ffmpeg -ss 00:09:20 -t 20 -i DSC_0014.MOV -vcodec copy -acodec copy end.MOV
</code></pre>

<p>When I concatenate only 30 seconds from each video, the result seems OK:</p>

<pre><code>FILES=files_to_combine
OUTPUT=show2.MOV
rm $FILES
for i in 3 4 5 6 7 8 9; do
    rm ${i}.MOV
    duration=$(ffprobe -v 0 -show_entries format=duration -of compact=p=0:nk=1  DSC_001${i}.MOV)
    start=$(echo $duration - 30 | bc)
    end=$(echo $duration - 1 | bc)
    ffmpeg -ss $start -t $end -i DSC_001${i}.MOV -vcodec copy -acodec copy ${i}.MOV
    echo file ./${i}.MOV &gt;&gt; $FILES
done

rm $OUTPUT
ffmpeg -safe 0 -f concat -i $FILES -vcodec copy -acodec copy $OUTPUT
</code></pre>

<p>This last concatenation gives this error multiple times:</p>

<pre><code>[mov @ 0x7fc3c7837400] Non-monotonous DTS in output stream 0:0; previous: 9080205, current: 9080200; changing to 9080206. This may result in incorrect timestamps in the output file.
</code></pre>

<p>So I am guessing that the problem is small differences in timestamps that
accumulate and become more noticeable with longer durations and the
concatenation of multiple files.</p>

<p>For reference, the DSLR that shot these clips is a Nikon D3300 and the result
of <code>ffprobe</code> on one of the files is:</p>

<pre><code>$ ffprobe DSC_0017.MOV -hide_banner
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fab70003800] st: 0 edit list: 1 Missing key frame while searching for timestamp: 1000
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fab70003800] st: 0 edit list 1 Cannot find an index entry before timestamp: 1000.
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'DSC_0017.MOV':
  Metadata:
    major_brand     : qt  
    minor_version   : 537331968
    compatible_brands: qt  niko
    creation_time   : 2019-06-12T23:52:37.000000Z
  Duration: 00:09:53.58, start: 0.000000, bitrate: 36843 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, smpte170m/bt709/bt470m), 1920x1080, 35300 kb/s, 50 fps, 50 tbr, 50k tbn, 100 tbc (default)
    Metadata:
      creation_time   : 2019-06-12T23:52:37.000000Z
    Stream #0:1(eng): Audio: pcm_s16le (sowt / 0x74776F73), 48000 Hz, 2 channels, s16, 1536 kb/s (default)
    Metadata:
      creation_time   : 2019-06-12T23:52:37.000000Z
</code></pre>

<h2>Update (9 August 2019)</h2>

<p>I concatenated the files in iMovie and the sound and image are not as out of sync as with FFMPEG. Maybe iMovie aligns the timestamps at the end of each clip instead of concatenating the audio and image streams separately.</p>

<p>I ran the concatenation again with the latest <code>ffmpeg 4.1.4_1</code> on these files and others from the same camera. The audio and image are in sync in one case (the results lasts 46 minutes) out of sync in another (the result lasts 48 minutes).</p>

CC BY-SA 4.0
2019-09-27T14:47:46.270Z
*instead of concatenating the audio and image streams separately.* --> yes. One way around this is to add a duration directive for each file in the concat text. The duration will be that shown by ffmpeg.
Gyan
CC BY-SA 4.0
2019-09-27T14:56:44.077Z
@Gyan Your idea is to get the duration from ffmpeg, then include `-t <seconds>` as an input option for each file to concatenate? The code for that was not immediate to me, could you give some pointers?
emonigma
CC BY-SA 4.0
2019-09-27T15:38:58.723Z
Basically, yes, but you have to add it as a directive. See [docs](http://ffmpeg.org/ffmpeg-formats.html#concat-1) and an [example](https://trac.ffmpeg.org/wiki/Slideshow#Concatdemuxer).
Gyan
CC BY-SA 4.0
2019-10-06T16:57:27.913Z
I tried a concatenation including `duration X` after each file, where `X` is the duration of the clip (obtained with `ffprobe`) minus 1 second, and the result is even more out of sync, around 3 seconds over the course of 1 hour.
emonigma
CC BY-SA 4.0
2019-10-06T17:13:16.077Z
Duration cannot be less than actual duration of clip, or if it is, you have to add an outpoint directive with a value equal to or less than the duration value.
Gyan
CC BY-SA 4.0
2019-10-09T17:31:42.667Z
I tried the same concatenation as in my last comment, with `outpoint X` after each file, where `X` is the duration of the clip minus 1 second. The result is still out of sync by about 2 seconds over the course of 1 hour.
emonigma
<ffmpeg><sync>
Video concatenation puts sound out of sync
emonigma
# Video concatenation puts sound out of sync

(Asked by: username_0 on 2019-06-18)

I have several MOV files from a DSLR camera. I concatenate them with directions from this thread:
ffmpeg -safe 0 -f concat -i files_to_combine -vcodec copy -acodec copy temp.MOV

where files_to_combine is:
file ./DSC_0013.MOV
...
file ./DSC_0019.MOV

The result has image and sound in sync for the first clip and is out of sync by fractions of a second in the second clip, and out of sync by around a second for the last clip. It is probably related to this error from the log:
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f82dd802200] st: 0 edit list: 1 Missing key frame while searching for timestamp: 1000
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f82dd802200] st: 0 edit list 1 Cannot find an index entry before timestamp: 1000.
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f82dd802200] Auto-inserting h264_mp4toannexb bitstream filter

How can I trim the frames to the available sound stream, then concatenate the two videos?
The full log from the ffmpeg command is:
ffmpeg version 4.1.3 Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple LLVM version 10.0.1 (clang-1001.0.46.4)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1.3_1 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f82dc00e000] Auto-inserting h264_mp4toannexb bitstream filter
Input #0, concat, from 'files_to_combine':
  Duration: N/A, start: -0.592000, bitrate: 36888 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, smpte170m/bt709/bt470m), 1920x1080, 35352 kb/s, 50 fps, 50 tbr, 50k tbn, 100 tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: pcm_s16le (sowt / 0x74776F73), 48000 Hz, stereo, s16, 1536 kb/s
    Metadata:
      handler_name    : SoundHandler
Output #0, mov, to 'temp.MOV':
  Metadata:
    encoder         : Lavf58.20.100
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, smpte170m/bt709/bt470m), 1920x1080, q=2-31, 35352 kb/s, 50 fps, 50 tbr, 50k tbn, 50k tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: pcm_s16le (sowt / 0x74776F73), 48000 Hz, stereo, s16, 1536 kb/s
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f82dd802200] st: 0 edit list: 1 Missing key frame while searching for timestamp: 1000
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f82dd802200] st: 0 edit list 1 Cannot find an index entry before timestamp: 1000.
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f82dd802200] Auto-inserting h264_mp4toannexb bitstream filter
frame=41886 fps=547 q=-1.0 Lsize= 3789826kB time=00:13:58.75 bitrate=37014.8kbits/s speed=10.9x    
video:3631879kB audio:157123kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.021759%

Update (1 July 2019)
I thought that the files had a problem at the beginning or at the end, so I
trimmed one second from each end, but it still had the sound out of sync:
FILES=files_to_combine
OUTPUT=show2.MOV
rm $FILES
for i in 3 4 5 6 7 8 9; do
    rm ${i}.MOV
    duration=$(ffprobe -v 0 -show_entries format=duration -of compact=p=0:nk=1  DSC_001${i}.MOV)
    trimmed=$(echo $duration - 1 | bc)
    ffmpeg -ss 1 -t $trimmed -i DSC_001${i}.MOV -vcodec copy -acodec copy ${i}.MOV
    echo file ./${i}.MOV >> $FILES
done

rm $OUTPUT
ffmpeg -safe 0 -f concat -i $FILES -vcodec copy -acodec copy $OUTPUT

When I trim a single file near the end, the sound and video do not seem out of sync:
ffmpeg -ss 00:09:20 -t 20 -i DSC_0014.MOV -vcodec copy -acodec copy end.MOV

When I concatenate only 30 seconds from each video, the result seems OK:
FILES=files_to_combine
OUTPUT=show2.MOV
rm $FILES
for i in 3 4 5 6 7 8 9; do
    rm ${i}.MOV
    duration=$(ffprobe -v 0 -show_entries format=duration -of compact=p=0:nk=1  DSC_001${i}.MOV)
    start=$(echo $duration - 30 | bc)
    end=$(echo $duration - 1 | bc)
    ffmpeg -ss $start -t $end -i DSC_001${i}.MOV -vcodec copy -acodec copy ${i}.MOV
    echo file ./${i}.MOV >> $FILES
done

rm $OUTPUT
ffmpeg -safe 0 -f concat -i $FILES -vcodec copy -acodec copy $OUTPUT

This last concatenation gives this error multiple times:
[mov @ 0x7fc3c7837400] Non-monotonous DTS in output stream 0:0; previous: 9080205, current: 9080200; changing to 9080206. This may result in incorrect timestamps in the output file.

So I am guessing that the problem is small differences in timestamps that
accumulate and become more noticeable with longer durations and the
concatenation of multiple files.
For reference, the DSLR that shot these clips is a Nikon D3300 and the result
of ffprobe on one of the files is:
$ ffprobe DSC_0017.MOV -hide_banner
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fab70003800] st: 0 edit list: 1 Missing key frame while searching for timestamp: 1000
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fab70003800] st: 0 edit list 1 Cannot find an index entry before timestamp: 1000.
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'DSC_0017.MOV':
  Metadata:
    major_brand     : qt  
    minor_version   : 537331968
    compatible_brands: qt  niko
    creation_time   : 2019-06-12T23:52:37.000000Z
  Duration: 00:09:53.58, start: 0.000000, bitrate: 36843 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, smpte170m/bt709/bt470m), 1920x1080, 35300 kb/s, 50 fps, 50 tbr, 50k tbn, 100 tbc (default)
    Metadata:
      creation_time   : 2019-06-12T23:52:37.000000Z
    Stream #0:1(eng): Audio: pcm_s16le (sowt / 0x74776F73), 48000 Hz, 2 channels, s16, 1536 kb/s (default)
    Metadata:
      creation_time   : 2019-06-12T23:52:37.000000Z

Update (9 August 2019)
I concatenated the files in iMovie and the sound and image are not as out of sync as with FFMPEG. Maybe iMovie aligns the timestamps at the end of each clip instead of concatenating the audio and image streams separately.
I ran the concatenation again with the latest ffmpeg 4.1.4_1 on these files and others from the same camera. The audio and image are in sync in one case (the results lasts 46 minutes) out of sync in another (the result lasts 48 minutes).

**Tags:** ffmpeg, sync

> **Comments:**
> **username_1**: *instead of concatenating the audio and image streams separately.* --> yes. One way around this is to add a duration directive for each file in the concat text. The duration will be that shown by ffmpeg.  
> **username_0**: @username_1 Your idea is to get the duration from ffmpeg, then include `-t <seconds>` as an input option for each file to concatenate? The code for that was not immediate to me, could you give some pointers?  
> **username_1**: Basically, yes, but you have to add it as a directive. See [docs](http://ffmpeg.org/ffmpeg-formats.html#concat-1) and an [example](https://trac.ffmpeg.org/wiki/Slideshow#Concatdemuxer).  
> **username_0**: I tried a concatenation including `duration X` after each file, where `X` is the duration of the clip (obtained with `ffprobe`) minus 1 second, and the result is even more out of sync, around 3 seconds over the course of 1 hour.  
> **username_1**: Duration cannot be less than actual duration of clip, or if it is, you have to add an outpoint directive with a value equal to or less than the duration value.  
> **username_0**: I tried the same concatenation as in my last comment, with `outpoint X` after each file, where `X` is the duration of the clip minus 1 second. The result is still out of sync by about 2 seconds over the course of 1 hour.  

---

## 1 answers

### Answer by username_2 on 2019-08-11. Score: 1 | Accepted answer

Usually audio recording is just a continuous steam, with no concept of video frames. The audio is just being encoded/output via a continuous buffer, which will just dump out wherever it is up to you when you stop recording video frames - it is not uncommon for there to be more (or even less) audio in the file than is actually required for the corresponding video.
I would try a de-mux/re-mux of the video files before concatenating, to see if that cleans things up. With the de-muxed files as well, you could calculate how much audio is actually there in the file vs the video play time. I don't have enough experience with ffmpeg to know if this will work, just crossing my fingers that when you re-mux the files it might do a calculation of the required audio length, and trim it appropriately for you (there might also be parameters available to force the audio to trim at the video length, if that doesn't happen by default).
An editor like iMovie is going to properly cut up the audio to exactly match the video cuts, which is why that works. You could also try outputting a single unedited clip from iMovie, and then de-mux that, and see how the audio length differs from the original. Not sure if iMovie lets you output "audio only", but be careful trying that - I know with Adobe Premiere, if I output an audio only WAV, it will differ in length (longer file) than the same clip being output with a video stream as well.
Sorry I can't offer anything more definitive than some guesses/suggestions, I'm fairly positive the root cause is audio length not matching the video in the original file, but I'm not familiar enough with ffmpeg to know the best way to resolve the issue using that tool.
Update from original poster
Import the files in question into a program like iMovie and check the beginnings and endings of each file. Does the video stream have more length than the audio stream? Or try to play it in a program like VLC: do you hear a drop in the sound at the end or the beginning?
If so, then your camera, for whatever reason, records more video than sound. You can deal with this quickly in FFMPEG by trimming each clip to the shortest stream:
ffmpeg -i input.MOV -map 0 -c copy -fflags +shortest -max_interleave_delta 0 output.MOV

You may need to play with -max_interleave_delta depending on your files. From the documentation:

‘shortest’
Stop muxing at the end of the shortest stream. It may be needed to increase max_interleave_delta to avoid flushing the longer streams before EOF. 

The command is from Trim audio stream to video stream length .

> **Comments:**
> **username_0**: That was it indeed! I opened the files in iMovie and saw that the video stream for the clips was 200 ms longer than the audio stream. I had 6 clips, so the total misalignment was around 1 second, which explains and solves the problem.  
> **username_0**: I trimmed each clip to the shortest stream (`ffmpeg -i input.MOV -map 0 -c copy -fflags +shortest -max_interleave_delta 0 output.MOV`, which I found on [this thread](https://video.stackexchange.com/questions/28187/trim-audio-stream-to-video-stream-length)), and that helps a lot. I measured the discrepancy from a segment where someone bangs a chair: it is 0.5 seconds with plain FFMPEG concatenation, it's 0.08 seconds with shortest stream clipping, and it's zero in iMovie. I would like to solve this smaller discrepancy too. I think that would make more sense as a new question. What do you think?  
> **username_2**: @miguelmorin - I would agree, post it up as a new question. You're trying to solve  a different problem now, and you have more specific info at this point (it would probably be messy to re-edit this older post and get an appropriate response). It also sounds like a more specific ffmpeg issue with your current solution now, it sounds like ffmpeg isn't trimming the audio with the video as cleanly as iMovie.  
> **username_1**: @PeterBarton  *Usually audio recording is just a continuous steam, with no concept of video frames.* --> no, compressed audio is framed and thus quantized. See https://superuser.com/a/1155353  

---

