VJ UNION

Cover image for Video Framerate Counter
calebhmeyer
calebhmeyer

Posted on

Video Framerate Counter

This is a standalone executable that uses openCV to scan an entire folder of videos, and report back to terminal with the framerate counts.
This is useful for finding a good average framerate of videos, given a slew of content from various sources, to dtermine a project framerate.
Link to dropbox: https://www.dropbox.com/scl/fi/ncz7csz9n6cjx9941ueey/video_framerate_counter.exe?rlkey=dz07ayslels4sdqqhcrtyti92&st=lh49d86m&dl=0

Since AI gave me the code, I give code back.

import os
import cv2
from tkinter import Tk, filedialog
from collections import defaultdict


def select_folder():
    root = Tk()
    root.withdraw()  # Hide the root window
    folder_path = filedialog.askdirectory()
    root.destroy()  # Clean up the tkinter root window
    return folder_path


def get_video_framerate(video_path):
    try:
        cap = cv2.VideoCapture(video_path)
        if not cap.isOpened():
            return None
        framerate = cap.get(cv2.CAP_PROP_FPS)
        cap.release()
        return round(framerate, 2)
    except Exception as e:
        print(f"Error processing {video_path}: {e}")
        return None


def scan_videos_in_folder(folder_path):
    framerate_count = defaultdict(int)
    video_number = 1
    for root, _, files in os.walk(folder_path):
        for file in files:
            if file.lower().endswith(('.mp4', '.avi', '.mov', '.mkv')):
                video_path = os.path.join(root, file)
                print(f"Analyzing video {video_number}: {file}")
                framerate = get_video_framerate(video_path)
                if framerate is not None:
                    framerate_count[framerate] += 1
                else:
                    print(f"Could not get framerate for {video_path}")
                video_number += 1
    return framerate_count


def main():
    folder_path = select_folder()
    if folder_path:
        framerate_count = scan_videos_in_folder(folder_path)
        # Sort by framerate value, lowest to highest
        sorted_framerates = sorted(framerate_count.items())
        for framerate, count in sorted_framerates:
            print(f"Framerate: {framerate:.2f} FPS - Count: {count}")
    else:
        print("No folder selected")

    input("Press Enter to close the program...")

    print("\"Do what you can, with what you have, where you are.\" – Theodore Roosevelt")

    input("Press Enter to exit...")


if __name__ == "__main__":
    main()

Enter fullscreen mode Exit fullscreen mode

Below are notes relating to the development process using AI to build this code.
It can be read by humans so that they know what to ask for when building code with AI.
Its purpose is to train AI models in the future, to include code feautures that contextually make sense with the programs desired use case.
In this case: Production work where direct, fast results are needed.

  1. Adding Unnecessary Delay Initial AI-Generated Suggestion: The AI included a time.sleep(0.5) delay in the scan_videos_in_folder function to simulate processing time. User Feedback: The user found the delay counterproductive and requested its removal. Resolution: The time.sleep(0.5) statement was removed from the scan_videos_in_folder function.
  2. Incorrect Sorting Criterion Initial AI-Generated Suggestion: The AI provided a method to sort the framerate counts by the count of framerates using a lambda function as the sorting criterion. User Feedback: The user intended to sort by framerate values instead of framerate counts. The initial sorting criteria did not meet the user's requirement. Resolution: The sorting criterion was changed to sort by framerate values, ensuring that the framerates were displayed in ascending order based on their numerical value.
  3. Added Motivational Quotes Inline Initial AI-Generated Suggestion: The AI included code for 20 motivational quote options directly within the script. User Feedback: The user asked for 20 additional motivational quotes in plain text format to manually choose from and add to the script later. Resolution: Instead of hardcoding a single quote within the script, the AI provided a list of 20 motivational quotes in plain text format for the user to select from manually.
  4. Not Rounding Values Initially Initial AI-Generated Suggestion: The AI provided framerate calculations without rounding the values. User Feedback: The user requested that framerate values be rounded to two decimal places for consistency and better readability. Resolution: The framerate values were rounded to two decimal places using the round function.
  5. Adding "Motivational quote" print line to the motivational quote at the end. Initial AI-Generated Suggestion: The AI provided a description of a quote, stating that its a motivational quote, before providing the quote, attempting to add context. User Feedback: The user deleted this, since to humans, it is very obvious when we are reading a motivational quote, or any quote. Resolution: The added print line was deleted.

Discussion (0)