I'm trying to create a Python script that would :
That doesn't seem too hard, but I suck at python :)
Any ideas on what the script should look like ?
Bonus question : Should I use
os.system
or
subprocess.call
?
Subprocess.call seems to allow for a more readable script, since I can write the command like this :
cmdLine = ['mencoder', sourceVideo, '-ovc', 'copy', '-oac', 'copy', '-ss', '00:02:54', '-endpos', '00:00:54', '-o', destinationVideo]
EDIT : Ok, that works :
import os, subprocess bitrate = '100' mencoder = 'C:\\Program Files\\_utilitaires\\MPlayer-1.0rc2\\mencoder.exe' inputdir = 'C:\\Documents and Settings\\Administrator\\Desktop\\input' outputdir = 'C:\\Documents and Settings\\Administrator\\Desktop\\output' for fichier in os.listdir(inputdir): print 'fichier :' + fichier sourceVideo = inputdir + '\\' + fichier destinationVideo = outputdir + '\\' + fichier[:-4] + ".mp4" commande = [mencoder, '-of', 'lavf', [...] '-mc', '0', sourceVideo, '-o', destinationVideo] subprocess.call(commande) os.remove(sourceVideo) raw_input('Press Enter to exit')
I've removed the mencoder command, for clarity and because I'm still working on it.
Thanks to everyone for your input.
Python as a scripting language provides various methods to iterate over files in a directory. This function returns the list of files and subdirectories present in the given directory. We can filter the list to get only the files using os. path.
Import the os library and pass the directory in the os. listdir() function. Create a tuple having the extensions that you want to fetch. Through a loop iterate over all the files in the directory and print the file having a particular extension.
To find all the filenames use os.listdir()
.
Then you loop over the filenames. Like so:
import os for filename in os.listdir('dirname'): callthecommandhere(blablahbla, filename, foo)
If you prefer subprocess, use subprocess. :-)
Use os.walk to iterate recursively over directory content:
import os root_dir = '.' for directory, subdirectories, files in os.walk(root_dir): for file in files: print os.path.join(directory, file)
No real difference between os.system and subprocess.call here - unless you have to deal with strangely named files (filenames including spaces, quotation marks and so on). If this is the case, subprocess.call is definitely better, because you don't need to do any shell-quoting on file names. os.system is better when you need to accept any valid shell command, e.g. received from user in the configuration file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With