Alter.Org.UA
 << Back Home UK uk   Donate Donate

How to detect/drop static video chunks with ffmpeg
(static scene detection without transcoding)

I've a lot of IP-cameras. Each sends h264 stream to the server where all streams are saved as 1-minute chunks without transcoding. It takes much disk space, about 25Gb/day. The task is to drop static fragments or entire chunks without full transcoding of h264 stream to save disk space without significant CPU load. Post-processing of already saved chunks is also acceptable.

The most obvious way requires full transcoding and is not compatible with -vcodec copy.

ffmpeg -nostdin -nostats -i <source> -r 25 -segment_time 60 -f segment -segment_atclocktime 1 
    -reset_timestamps 1 -vcodec copy -strftime 1 %Y%m%d-%H%M.avi
vs
ffmpeg -nostdin -nostats -i <source> -r 25 -segment_time 60 -f segment -segment_atclocktime 1 
    -reset_timestamps 1 -vf "select=gt(scene\,0.003)" -strftime 1 %Y%m%d-%H%M.avi

Simple solution

  • Extract I-frames from saved chunk. This operation doesn't cause high CPU load. -vf "select=eq(pict_type\,I)"
  • Compose video from these frames.
  • Filter it with motion detection filter and look at result. -vf "select=gt(scene\,0.0005)"
    Note: here we use higher threshold (0.0005).
ffmpeg -i big-chunk.avi -vf "[in]select=eq(pict_type\,I)[mid];[mid]select=gt(scene\,0.0005)[out]" 
    -copyts reduced-chunk.avi

and now lets analyze final video

ffprobe -show_streams -count_frames reduced-chunk.avi|grep nb_read_frames

interpretation of nb_read_frames values

  • between 1 and 4 means static scene with integrated time-stamp and minor noise.
  • 5-10 - probably we have small fragments with activity. Just keep intact or dump timestamps of I-frames (ffprobe -count_frames -show_frames reduced-chunk.avi) and extract fragments with activity from original video.
  • 11+ - active motions, keep original video chunk

You can also play with motion threshold gt(scene\,0.0005)

2017.12.12


See also:


FB or mail alterX@alter.org.ua (remove X)   Share
designed by Alter aka Alexander A. Telyatnikov powered by Apache+PHP under FBSD © 2002-2024