Good morning, after 1 month of research and attempts, still wondering the correct solution.
I have frequently tv mpeg-ts (h264+aac) file to cut part from. I have a script to cut at exact times inserted in a text file; the timestamp of the cut point is identified overlaying the pts on every frame with drawtext filter, and/or exporting exact frame number as a pic and checking frame's timestamp with ffprobe, and/or with
ffmpeg -i xxxx -ss [...] (accurate seek).
if I cut only the video, with the trim filter, according those timestamp, the result is perfect. the problem arises when I cut audio as well (with the same timestamps, obviously, from the same input (
[0:v]trim.....; [0:a]trim.....): audio/video is synced BUT not at the exact position.
after troubleshooting I checked that
start_pts of the video and audio track are different; after checking many files I found that audio always starts earlier, between around 0.4 - 1.5 sec.
video start pts:
ffprobe -show_entries stream=codec_type,start_time 00001.ts 2>/dev/null | grep -m 1 -A1 codec_type=video | grep -v video | sed -e 's/^.*=//'
audio start pts:
ffprobe -show_entries stream=codec_type,start_time 00001.ts 2>/dev/null | grep -m 1 -A1 codec_type=audio | grep -v audio | sed -e 's/^.*=//'
in this case, for instance, I get:
35918.124589 (video) 35917.700589 (audio)
difference is 35918.124589 - 35917.700589 = 0.424 s.
this is the exact offset of the cut as compared from my input timestamps.
my understanding is that if I use the trim filter only on a video track, ffmpeg position on the requested time starting from intial video track timestamp, but if I trim both video and audio, BOTH are cut at requested time starting from intial AUDIO track timestamp (thus shifting the video cutting point).
if I decide to use separate inputs (
ffmpeg -i 00001.ts -i 00001.is -filter_complex "[0:v]trim xxx yyy[v1];[1:a]atrim xxx yyy[a1]") then video is cut at original desired position, but audio and video are out of sync.
presently my two options are to use separate inputs and INCREASE audio cut time by the calculated offset, OR use, as normal, one input for each part to be cut, and increase both video and audio cut time by the offset (since is going to be counted from audio initial pts for both the audio and the video).
I would like to understand how ffmpeg use pts from different tracks: how it decides if looking to audio track pts, or video track pts, etc? which should be the correct approach to have audio vidfeo in sync, and cutting exactly at the timestamp identified from the video track?