Skip to content

Instantly share code, notes, and snippets.

@manero6
Last active October 9, 2023 10:49
Show Gist options
  • Save manero6/343d46aed29304c5aa3d093bd3c0837a to your computer and use it in GitHub Desktop.
Save manero6/343d46aed29304c5aa3d093bd3c0837a to your computer and use it in GitHub Desktop.
Run cropdetect and print 10 most detected crops
#!/usr/bin/env python3
import re
import sys
import subprocess
try:
file_to_crop = sys.argv[1]
except:
print("Please provide a video file.")
sys.exit(1)
output = []
best_crop = {}
# regex for extracting only the crop value
regex = r"\d{1,4}:\d{1,4}:\d{1,4}:\d{1,4}"
print("Starting cropdetect on '" + file_to_crop + "'.")
# capturing ffmpeg cropdetect output
command_output = subprocess.run([
"ffmpeg", "-i", file_to_crop,
#debug "-t", "1000",
"-vf", "cropdetect",
"-f", "null", "-"
],
capture_output=True)
# converting bytes to str to list
output = str(command_output.stderr)
output = output.split('\\n')
#debug print(output)
# debug counter=1
# for line in output:
# print("Line " + str(counter) + ": " + line)
# counter += 1
print("Searching and sorting the most detected crops.")
for line in output:
search_crop = re.search(regex, line)
if search_crop is not None:
#debug print(search_crop.group())
crop = search_crop.group()
if crop in best_crop:
best_crop[crop] +=1
else:
best_crop[crop] = 1
# sorting list from highest to lowest value
sorted_best_crop = sorted(best_crop.items(), key=lambda x: x[1], reverse=True)
print()
# printing the most common crops
for i in range(0, len(sorted_best_crop)):
key = sorted_best_crop[i][0]
val = sorted_best_crop[i][1]
print("Crop {:>15} detected {:<6} times.".format(key, val))
print()
@manero6
Copy link
Author

manero6 commented May 21, 2022

Example:

Starting cropdetect.
Searching and sorting the most detected crops.

Crop    640:384:0:50 detected 18673  times.
Crop    640:400:0:40 detected 4800   times.
Crop 208:128:236:148 detected 587    times.
Crop 208:112:236:148 detected 274    times.
Crop 224:272:220:134 detected 207    times.
Crop 208:256:236:148 detected 182    times.
Crop 208:112:236:146 detected 81     times.
Crop 208:256:234:148 detected 29     times.
Crop 208:256:232:146 detected 22     times.
Crop 224:256:222:144 detected 22     times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment