I wanted a way to run the contents of a folder through ffmpeg and turn some mp4 files into avi's my dvd player could play. This program reads through a folder and, depending on file extension, passes it to another program. The code below has been extended to optimize PNG and convert JPEG's to JPEG XL files.
# # Process files in a folder # import sys import os import subprocess folder="" if len(sys.argv)==2: folder=sys.argv[1] else: print("Processes files in a folder") print("Usage: "+sys.argv[0]+" [folder]") exit(0) # # Check folder exists # if os.path.isdir(folder)==False: print("Cannot find this folder?") exit(0) # # Go through files in this folder! # files=os.listdir(folder) for infile in files: outfile="" infile=folder+"\\"+infile # Run the MP4 through FFMPEG (which is in my path) if infile.endswith(".mp4"): outfile=infile[:-3]+"avi" runme=["ffmpeg.exe","-i",infile,"-vtag","xvid","-c:v", "libxvid","-q:v","5",outfile]; # Run the PNG through OPTIPNG (which is in my path) if infile.endswith(".png"): outfile="Optimising" runme=["optipng.exe","-preserve",infile] # Recompress a folder of JPEG files as JPEG XL using CJXL (which is also in my path) if infile.endswith(".jpg"): outfile=infile[:-3]+"jxl"; runme=["cjxl.exe",infile,outfile]; if outfile!="": # Only convert if the destination file does not exist so we do not overwrite! if os.path.exists(outfile)==False: print(infile," => ",outfile); subprocess.run(runme,stdout=False,stderr=False) else: print(infile," == Skipping! (Already processed?)") else: print(infile," == Skipping! (Unknown file type)")
Save the above as something, folder.py for example, then run it giving the path of a folder, e.g:
#skip>py folder.py test test\witch_transform.mp4 => test\witch_transform.avi
And if we run it again:
#skip>py folder.py test test\witch_transform.avi == Skipping! (Unknown file type) test\witch_transform.mp4 == Skipping! (Already processed?)
The above can be easily modified for other tasks, for example to compress a folder of files as JPEG XL you could add the following:
# Recompress a folder of JPEG files as JPEG XL using CJXL (which is also in my path)
if infile.endswith(".jpg"):
outfile=infile[:-3]+"jxl";
runme=["cjxl.exe",infile,outfile];
Updated 19/07/2025
Created 12/12/2024