2024-07-18, 05:22 PM
(2024-07-18, 06:08 AM)stewed pork roti Wrote: Let me know if anybody finds something to do this efficiently, and I'll let you know if I ever write that script
This python script should do the job, adjust to your needs. It's set to create three backdrops; one at 20 & 40 seconds and one random within the first 5 minutes. Just set the script to be executable and run it without passing any arguments. Requirements: Python3 & FFmpeg.
Code:
#!/usr/bin/env python3
import os
import random
import subprocess
from pathlib import Path
def extract_frames(input_file, output_prefix, first_file):
# Define times to extract frames (in seconds)
times = [20, 40]
random_time = random.randint(60, 300) # Random time between 60 and 300 seconds
times.append(random_time)
for i, time in enumerate(times):
if i == 0 and first_file:
output_file = f"{output_prefix}-backdrop.jpg" # First file without number sequence
else:
output_file = f"{output_prefix}-backdrop{i+1}.jpg" # Subsequent files with number sequence
output_path = os.path.join(os.path.dirname(input_file), output_file)
subprocess.run(['ffmpeg', '-hide_banner', '-loglevel', 'error',
'-i', input_file, '-ss', str(time), '-vframes', '1', output_path])
print(f"Processed: {output_file}")
def process_media_file(media_file):
filename = media_file.name
print(f"Processing: {filename}")
filename_no_ext = filename[:filename.rfind('.')]
output_prefix = f"{filename_no_ext}"
extract_frames(str(media_file), output_prefix, first_file=True)
def scan_directory(directory):
media_files = []
for root, _, files in os.walk(directory):
for file in files:
if any(file.lower().endswith(ext) for ext in ['.mp4', '.avi', '.mkv', '.mov']):
media_files.append(Path(root) / file)
total_files = len(media_files)
print(f"Total media files found: {total_files}")
for media_file in media_files:
process_media_file(media_file)
if __name__ == "__main__":
directory = input("Enter the directory path where media files are located: ").strip()
if not os.path.isdir(directory):
print("Invalid directory path.")
exit()
scan_directory(directory)
print("Frame extraction completed.")