Skip to content

Instantly share code, notes, and snippets.

@JasonHoku
Last active April 25, 2023 22:56
Show Gist options
  • Save JasonHoku/d0335fe9bb46463dfa7123a183c463c6 to your computer and use it in GitHub Desktop.
Save JasonHoku/d0335fe9bb46463dfa7123a183c463c6 to your computer and use it in GitHub Desktop.
Rename All Image Files In Folder Directory And FFMPEG Input To Convert To Slideshow Gif Or Video - Multithreaded Python Renamer
import os
import threading
import subprocess
# Set the directory containing the PNG files
directory = "C:/path/to/images"
# Set the prefix to use for the renamed files
prefix = "image_"
# Set the output video file name
output_file = "output.mp4"
# Set the frame rate of the output video
frame_rate = 25
# Function to rename a single file
def rename_file(filename):
# Get the original file name without the extension
basename = os.path.splitext(os.path.basename(filename))[0]
# Rename the file with the specified prefix
os.rename(filename, os.path.join(directory, prefix + basename + ".png"))
# Create a list of all PNG files in the directory
png_files = [filename for filename in os.listdir(directory) if filename.endswith(".png")]
# Rename all PNG files in the directory using multiple threads
threads = []
for filename in png_files:
t = threading.Thread(target=rename_file, args=(os.path.join(directory, filename),))
t.start()
threads.append(t)
for t in threads:
t.join()
# Create a list of all renamed PNG files in the directory
renamed_files = [filename for filename in os.listdir(directory) if filename.startswith(prefix) and filename.endswith(".png")]
# Create a list file containing the names of all renamed PNG files
with open("list.txt", "w") as f:
for filename in sorted(renamed_files):
f.write("file '{}'\n".format(os.path.join(directory, filename)))
# Use FFmpeg to create a video from the renamed PNG files
subprocess.call(["ffmpeg", "-f", "concat", "-safe", "0", "-i", "list.txt", "-framerate", str(frame_rate), output_file])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment