Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to retrieve the timestamp of each frame of a camera using an rstp stream and them. For recording I use the following command line and it's work :

ffmpeg
-correct_ts_overflow 0
-probesize 1G
-analyzeduration 1G
-i rtsp://user:password@ip:port
-vcodec copy
-bsf:v h264_mp4toannexb
-bufsize 10M
-acodec copy
-f ssegment
-segment_list_flags live
-segment_atclocktime 1
-reset_timestamps 1
-write_empty_segments 1
-segment_time 15
-segment_list C:\Video\Delivery\ffmpeg\list.video
-segment_list_type csv
-strftime 1 "C:\Video\Delivery\ffmpeg\%%Y%%m%%d_%%H-%%M-%%S.ts"

And for some utility I would like to be able to retrieve the timestamp of the machine when I receive a frame, so by searching a bit I found different post on '-mkvtimestamp_v2'. By trying it alone with the camera as if below:

ffmpeg
-copyts ^
-correct_ts_overflow 0 ^
-probesize 1G ^
-analyzeduration 1G ^
-i rtsp://user:password@ip:port
-c copy
-pix_fmt yuv420p
-flush_packets 1
-vframes 10
-reset_timestamps 1
-timestamp now
-copyts
-f mkvtimestamp_v2 timestamp.txt
-vsync 0

It works perfectly.

But from the moment I try to record AND try to retrieve the timestamp simultaneously with the following command :

ffmpeg
-use_wallclock_as_timestamps 1
-correct_ts_overflow 0
-probesize 1G
-analyzeduration 1G
-i rtsp://user:password@ip:port
-vcodec copy
-bsf:v h264_mp4toannexb
-bufsize 10M
-acodec copy
-f ssegment
-segment_list_flags live
-segment_atclocktime 1
-reset_timestamps 1
-write_empty_segments 1
-segment_time 15
-segment_list C:\Video\Delivery\ffmpeg\list.video
-segment_list_type csv
-strftime 1 "C:\Video\Delivery\ffmpeg\%%Y%%m%%d_%%H-%%M-%%S.ts"
-copyts
-vcodec copy
-flush_packets 1
-f mkvtimestamp_v2 log.txt
-vsync 0

I get a lot of: Non-monotonous DTS in output stream 0:0 warning. I also have on average one minute delay between the recorded timestamps, and the real timestamp. And the first video recorded have a bugged timer on a video player like this : Here

I've tried arranging the command in different orders but I get nothing conclusive...

So if you have any idea that would be a big help!

I work on Windows 10 and I use ffmpeg-3.4.1.

Cordially,

I solved it by piping the second output to another ffmpeg instance. The reason why I think this works is because the second ffmpeg will discard the timestamp offset that was added by -use_wallclock_as_timestamps 1 and reset the offset to 0.

ffmpeg -use_wallclock_as_timestamps 1 -i rtsp://@ip:port -c copy -copyts -y -f mkvtimestamp_v2 timestamps.txt -vsync 0 -c copy -f mpegts - | ffmpeg -f mpegts -i - -c copy -f segment output-segment-%d.mp4

Another problem however with this solution is that if RTSP drops some frames, then the mkvtimestamp_v2 file will be skipping some time values, making it hard to correlate the segments with the timestamps.txt file.

So instead I solved it by embedding the wall clock timestamps into the segments themselves.

ffmpeg -use_wallclock_as_timestamps 1 -i rtsp://@ip:port -c copy -copyts -vsync passthrough -f segment -segment_time 10 out%d.mp4

Then I can run ffprobe later on each segment to know their actual start time. (relative to system clock).

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.