I am trying to extract video and generate a timelapse between two PTS values ("make a timelapse of all video between 8am and 6pm").

The original video comes from a network camera and contains a -initial_offset equivalent to the unix wallclock:

ffprobe  -i camera01-20200312-092453.mkv -show_frames -select_streams v:0 -print_format flat | grep pkt_pts=

Input #0, matroska,webm, from '/srv/video/netcams/archive/recordall/camera01-20200312-092453.mkv':
    ENCODER         : Lavf58.29.100
  Duration: 440024:24:42.97, start: 1584001479.967000, bitrate: 0 kb/s
    Stream #0:0: Video: hevc (Main), yuv420p(tv), 2048x1536, 1 fps, 1 tbr, 1k tbn, 1 tbc (default)
DURATION        : 440024:24:42.967000

I have tried -ss and -t but these seem to assume the file starts at 0.

Some background: I use the -initial_offset to ensure that each frame's PTS is set to the unix timestamp:

... -f segment                      \
        -segment_time 86400         \
        -initial_offset "$(date +%%s.%%N)" \
        -segment_format matroska    \
        -strftime 1                 \
        "camera01-%%Y%%m%%d-%%H%%M%%S.mkv" \

My current approach is to look for videos in the last day, concatenate them and then aim to extract video between two known times:

find /srv/video/netcams/archive/recordall -newermt $(date +%Y-%m-%d -d '1 days ago') -type f -print | \ 
  sed -e "s/^/file '/"  -e "s/$/'/" | \
  ffmpeg -f concat -safe 0  -protocol_whitelist file,pipe,crypto -i - -c:v libx264 -r 30 -filter:v "trim=start_pts=1584001492967:end_pts=1584001514967000,setpts=PTS/500," -preset ultrafast

What's the best way to go about this? I guess I'm missing something to do with the filter ordering?

  • The concat demuxer will necessarily start its emitted streams with timestamp 0. For all subsequent files, their initial timestamp is set to be smooth with the last timestamp of the earlier file. To maintain absolute timestamps, you need to use a streaming container like MPEG-TS and then use the concat protocol with -copyts flag. However, MPEG-TS can only timestamps as large as 95443s. Larger timestamps get rolled over.
    – Gyan
    Mar 13, 2020 at 14:29
  • I suggest you loop through files to identify the segment containing the starting in timestamp and its relative offset in that segment. Same for outpoint. Then set the inpoint and outpoint directives for those segments. and comment out all entries for segments before and after in the concat list.
    – Gyan
    Mar 13, 2020 at 14:29
  • Good to know about the timespamp size limitations. Presumably ffmpeg -i camera01-20200312-092453.mkv -filter:v|trim=start_pts=1584001483967:end_pts=1584001500967" out.mkv should work though?
    – Imaginator
    Mar 13, 2020 at 15:19
  • After adding -copyts
    – Gyan
    Mar 13, 2020 at 15:21
  • So this works - I was just impatient with the command - seems seeking through an 8GB file for specific PTS takes a bit of time. Thanks again @gyan
    – Imaginator
    Mar 13, 2020 at 16:46


You must log in to answer this question.

Browse other questions tagged .