3

I have an .MP4 file with one video track (H.264, 25 FPS) and one audio track (MP2). The total length is ~2:52 (two hours, 52 minutes). I've noticed that A/V is synchronous before ~2:34 but async afterwards, i.e. audio is late ~0.3 s.

My plan was to

  1. Extract the audio track to .MP2
  2. Cut the 0.3 s (using mp3directCut, which works fine)
  3. Mux the video track and the cut audio track.

Done. However, the result wasn't satisfying because A/V was out of sync right from the start of the video. I took a closer look at the original file and dumped the timestamps. The smallest PTS of the video track is 0.16 s, and the PTSs are all increasing by 0.04 and without any gap. The smallest PTS of the audio track is 0.212 s, and most PTSs increase by 24 ms (normal for MP2), but few diffs are smaller or greater than 24 ms (which is ok, but obviously a problem when extracting the track, see question below)

My questions are: Do I lose timestamps when extracting to MP2? Then my initial plan is foredoomed. Which other way is there to cut a small part of an audio track inside an MP4 file? Without reencoding audio, of course. I'm familiar with FFmpeg and AviDemux.

2 Answers2

1

Do I lose timestamps when extracting to MP2?

Yes. .mp2/.mp3 has no timestamps.

Which other way is there to cut a small part of an audio track inside an MP4 file?

Any container with timestamps that supports MP2 i.e. MPEG-TS, MP4/MOV, MKV. There may be others.


Try the concat demuxer,

ffmpeg -i in.mp4 -f concat -i audio.txt -map 0:v -map 1:a -c copy out.mp4

where audio.txt is

file in.mp4
outpoint 2:34:00
file in.mp4
inpoint 2:34:00.30
Gyan
  • 34,439
  • 6
  • 56
  • 98
  • Oh, why didn't I think about this? Maybe because I'd expect it cutting a part of the track but keeping the timestamps, which would result in a 0.3 s audio gap and the rest at it's old time location. I've tried your suggestion and got a couple of these messages: `Non-monotonous DTS in output stream 0:1; ...... This may result in incorrect timestamps in the output file.` I've dumped the timestamps of the new file and analyzed them: Regarding the audio track, several PTS values are equal now. As space is very limited in comments here, this is all for now; I'm analyzing and will be back. TIA. –  Feb 18 '18 at 13:07
  • Actually FFmpeg is increasing the PTS value by 1, which means 1/48,000 s, but as the dumped values are formatted with a resolution of 1 ms, I _thought_, they were equal. Knowing this, I will continue with the unrounded values now and can continue differently. I'll be back.... –  Feb 18 '18 at 17:08
  • Is the resulting A/V in sync? – Gyan Feb 18 '18 at 17:33
  • Mulvya, only the 1st part before the cut is in sync. That's why I'm still analyzing. Status quo: The timestamps of the 2nd part (after the cut) are shifted by -2.589 s. Consequently, there are overlapping PTS values and the "Non-monotonous DTS" messages appear. I'm currently trying to find out why by -2.589 s. (Maybe I'll even have a look at the ffmpeg source.) I'll report my result as soon as I have more information. Thx. –  Feb 18 '18 at 18:28
  • Try to extract only the post-cut part: `ffmpeg -i in.mp4 -c copy -vn -ss X out.m4a` where X is around 2:34:0.3. Once you have the right start point. Substitute with the new file in the concat (removing the inpoint directive). – Gyan Feb 18 '18 at 18:49
  • I understand your approach, but I already had the same idea to extract the post-cut part and got the error message `[ipod @ 0000026d666b4f40] Tag mp4a incompatible with output codec id 86017 ([0][0][0][0])` This time, I've added `-f mp4`, and it worked. The timestamps in the resulting file start at zero. Fine. However, after concatenating, the post-cut part is just silence. It's obvious by looking at the file size that that audio part is missing. Confirmed by dumping the timestamps. Tried FFmpeg stable and nightly. Please give me some more time to try things out. Thanks again. –  Feb 18 '18 at 20:04
0

The solution was:

  1. Extract the first part of the audio track to part1.mp4
  2. Extract the second part of the audio track to part2.mp4
  3. Concat part1.mp4 and part2.mp4 to complete.mp4
  4. Mux video from the original file and audio from complete.mp4

See comments above from @Mulvya for the specific FFmpeg commands. Thanks again to him/her for the support.