in dynamicfilters/videodynamismfiltering/check_static.py [0:0]
def check_static_video(video_file, segment_duration=60, freeze_n=0.05, freeze_d=50, threshold=0.4):
"""Use ffmpeg freezedetect to check if a video has significant static content."""
# Get video duration using ffprobe
try:
result = subprocess.run(
["ffprobe", "-v", "error", "-show_entries", "format=duration",
"-of", "default=noprint_wrappers=1:nokey=1", video_file],
capture_output=True, text=True
)
video_duration = float(result.stdout.strip())
except Exception as e:
print(f"Error getting video duration for {video_file}: {e}")
return None
# Calculate the number of segments to analyze
num_segments = math.ceil(video_duration / segment_duration)
freeze_count = 0
# Analyze video in segments
for start_time in range(0, int(video_duration), segment_duration):
try:
command = [
"ffmpeg", "-hide_banner", "-ss", str(start_time), "-i", video_file,
"-t", str(segment_duration), "-vf", f"freezedetect=n={freeze_n}:d={freeze_d}", "-an", "-f", "null", "-"
]
result = subprocess.run(command, capture_output=True, text=True)
# Check the stderr output for freeze detection
if "freezedetect" in result.stderr:
print(f"Static content detected in segment starting at {start_time} of {video_file}.")
freeze_count += 1
except Exception as e:
print(f"Error processing segment starting at {start_time} of {video_file}: {e}")
return None
# Calculate the percentage of segments with freezes
freeze_percentage = freeze_count / num_segments
print(f"Freeze percentage for {video_file}: {freeze_percentage:.2%}")
# Determine if the video is considered static based on threshold
return freeze_percentage >= threshold