Run command for every file in directory
I'm converting all of my video files to WebM, to greatly reduce the amount of hard disk space being used. To do this I use the program "ffmpeg" which requires an input and output file specified. Since the converting process takes a long time, I wish to run it day and night for every video file in my video folder, so I don't have to manually do it myself.
By Googling myself I found ways to do this but the output would be 1 big file. Can anybody explain me how I convert "video1.avi" to "video1.webm", and "video2.avi" to "video2.webm", etc etc automatically?
Solution 1:
You can use a bash loop - the basic structure would be something like
for file in *.avi; do ffmpeg -i "$file" "${file%.avi}".webm; done
You can add whatever other ffmpeg
command line options you require as appropriate.
Solution 2:
Below a python script to do the job:
#!/usr/bin/env python3
import os
import subprocess
sourcedir = "/path/to/sourcedirectory"
for file in os.listdir(sourcedir):
name = file[:file.rfind(".")]
subprocess.call(["fmpeg", "-i", sourcedir+"/"+name+".avi", sourcedir+"/"+name+".webm"])
By using subproces.call() the script will only continue on the next conversion when the last one has finished.
To use:
Copy the script into an empty text file, set the source directory of your files, save it as convert.py
and run it by the command
python3 /path/to/convert.py