Created
November 7, 2023 06:06
-
-
Save ParkWardRR/91f9d2641e2b3f5ef08e8bec77d8167d to your computer and use it in GitHub Desktop.
cleanUpOldVideos_Duration_Orientation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This Python Script is designed to scan a given directory for video files with | |
certain extensions (.mp4, .avi, .flv, .wmv, .mov), and then delete the video files | |
that meet the following two criteria: | |
1. The video is less than 10 minutes long. | |
2. The video is in portrait mode (height greater than width). | |
The script is designed to work in a multi-threaded manner for improved speed, utilizing all available CPU cores. | |
The script outputs progress to the terminal, including the current file being processed, | |
skip reasons, the number of files deleted so far, and the total size of the deleted files. | |
To use the script: | |
- Ensure you have Python installed on your machine. | |
- You need to have ffprobe (part of the ffmpeg) installed and available in your PATH. | |
- Modify the 'target_folder' variable in the script with the path where you want to scan for video files. | |
- Set the 'check_portrait' variable to True if you want to check for portrait videos; set it to False otherwise. | |
- Run the script: python cleanUpOldVideos_Duration_Orientation.py | |
NOTE: Be careful while executing, as the files are permanently deleted. | |
Make sure to backup your data or test the script with sample data before actual use. | |
""" | |
from pathlib import Path | |
import subprocess | |
import concurrent.futures | |
import shlex | |
import os | |
# Set the target folder path where video files will be scanned. | |
target_folder = "/mnt/hereChangeMe" | |
# Set the toggle to check for portrait videos. | |
check_portrait = True | |
# Global variables to keep track of total size and total files deleted | |
total_size = 0 | |
total_files = 0 | |
def process_file(path): | |
global total_size, total_files | |
# Command to get the video duration | |
get_duration_cmd = shlex.split(f'ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "{path}"') | |
duration = float(subprocess.check_output(get_duration_cmd).decode().strip()) | |
if not check_portrait: | |
# Process files without checking for portrait mode | |
if duration < 600: | |
total_size += os.path.getsize(path) | |
total_files += 1 | |
os.remove(path) | |
print(f"Deleted: {path}") | |
else: | |
print(f"Skipped: Duration is not less than 10 minutes. {path}") | |
else: | |
# Command to get the video width and height | |
get_dimension_cmd = shlex.split(f'ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "{path}"') | |
width, height = map(int, subprocess.check_output(get_dimension_cmd).decode().strip().split('x')) | |
# Check if video is less than 10 minutes and is in portrait mode (height > width) | |
if duration < 600 and height > width: | |
total_size += os.path.getsize(path) | |
total_files += 1 | |
os.remove(path) | |
print(f"Deleted: {path}") | |
else: | |
if duration >= 600: | |
print(f"Skipped: Duration is not less than 10 minutes. {path}") | |
elif height <= width: | |
print(f"Skipped: Not a portrait video. {path}") | |
def main(): | |
video_file_extensions = ['.mp4', '.avi', '.flv', '.wmv', '.mov'] | |
video_files = [str(path) for path in Path(target_folder).rglob("*") if path.suffix in video_file_extensions] | |
with concurrent.futures.ThreadPoolExecutor() as executor: | |
executor.map(process_file, video_files) | |
print(f"Number of files deleted: {total_files}") | |
print(f"Total size deleted: {total_size / (1024 ** 3):.2f} GB") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment