I'm trying to create a Python script that would :
我正在尝试创建一个Python脚本,它可以:
- Look into the folder "/input"
- 查看文件夹“/输入”
- For each video in that folder, run a mencoder command (to transcode them to something playable on my phone)
- 对于该文件夹中的每个视频,运行一个mencoder命令(将它们转换为可在手机上播放的内容)
- Once mencoder has finished his run, delete the original video.
- 一旦mencoder完成了他的运行,删除原始视频。
That doesn't seem too hard, but I suck at python :)
这似乎不难,但我的python学得很烂。
Any ideas on what the script should look like ?
关于脚本应该是什么样子的任何想法?
Bonus question : Should I use
附加问题:我应该用吗
os.system
os.system
or
或
subprocess.call
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]
sourceVideo cmdLine =[' mencoder ',‘ovc’,‘复制’,‘-oac’,‘复制’,‘s’,‘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.
为了清晰起见,我已经删除了mencoder命令,因为我还在处理它。
Thanks to everyone for your input.
谢谢大家的参与。
6 个解决方案
#1
77
To find all the filenames use os.listdir()
.
要查找所有文件名,请使用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. :-)
如果您喜欢子进程,请使用子进程。:-)
#2
22
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.
操作系统之间没有真正的区别。系统和子流程。调用这里-除非你必须处理奇怪的命名文件(文件名包括空格,引号等)。如果是这样,子进程。调用肯定更好,因为您不需要对文件名进行任何shell引用。操作系统。当您需要接受任何有效的shell命令时,例如从配置文件中的用户接收到的命令,系统会更好。
#3
11
Python might be overkill for this.
对此,Python可能有点过头了。
for file in *; do mencoder -some options $file; rm -f $file ; done
#4
2
AVI
to MPG
(pick your extensions):
AVI至MPG(选择扩展名):
files = os.listdir('/input')
for sourceVideo in files:
if sourceVideo[-4:] != ".avi"
continue
destinationVideo = sourceVideo[:-4] + ".mpg"
cmdLine = ['mencoder', sourceVideo, '-ovc', 'copy', '-oac', 'copy', '-ss',
'00:02:54', '-endpos', '00:00:54', '-o', destinationVideo]
output1 = Popen(cmdLine, stdout=PIPE).communicate()[0]
print output1
output2 = Popen(['del', sourceVideo], stdout=PIPE).communicate()[0]
print output2
#5
1
Or you could use the os.path.walk function, which does more work for you than just os.walk:
或者你可以用os。path。步行功能,它对你的工作比仅仅是o。
A stupid example:
一个愚蠢的例子:
def walk_func(blah_args, dirname,names):
print ' '.join(('In ',dirname,', called with ',blah_args))
for name in names:
print 'Walked on ' + name
if __name__ == '__main__':
import os.path
directory = './'
arguments = '[args go here]'
os.path.walk(directory,walk_func,arguments)
#6
1
I had a similar problem, with a lot of help from the web and this post I made a small application, my target is VCD and SVCD and I don't delete the source but I reckon it will be fairly easy to adapt to your own needs.
我有一个类似的问题,在网上有很多帮助,这篇文章我做了一个小的应用,我的目标是VCD和SVCD,我不删除源代码,但我认为它很容易适应你自己的需要。
It can convert 1 video and cut it or can convert all videos in a folder, rename them and put them in a subfolder /VCD
它可以转换1个视频并剪切它,也可以将所有视频转换到一个文件夹中,重命名它们并将它们放到子文件夹/VCD中
I also add a small interface, hope someone else find it useful!
我还添加了一个小界面,希望有人发现它有用!
I put the code and file in here btw: http://tequilaphp.wordpress.com/2010/08/27/learning-python-making-a-svcd-gui/
我把代码和文件放在这里:http://tequilaphp.wordpress.com/2010/08/27/learning-python- make -a-svcd-gui/
#1
77
To find all the filenames use os.listdir()
.
要查找所有文件名,请使用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. :-)
如果您喜欢子进程,请使用子进程。:-)
#2
22
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.
操作系统之间没有真正的区别。系统和子流程。调用这里-除非你必须处理奇怪的命名文件(文件名包括空格,引号等)。如果是这样,子进程。调用肯定更好,因为您不需要对文件名进行任何shell引用。操作系统。当您需要接受任何有效的shell命令时,例如从配置文件中的用户接收到的命令,系统会更好。
#3
11
Python might be overkill for this.
对此,Python可能有点过头了。
for file in *; do mencoder -some options $file; rm -f $file ; done
#4
2
AVI
to MPG
(pick your extensions):
AVI至MPG(选择扩展名):
files = os.listdir('/input')
for sourceVideo in files:
if sourceVideo[-4:] != ".avi"
continue
destinationVideo = sourceVideo[:-4] + ".mpg"
cmdLine = ['mencoder', sourceVideo, '-ovc', 'copy', '-oac', 'copy', '-ss',
'00:02:54', '-endpos', '00:00:54', '-o', destinationVideo]
output1 = Popen(cmdLine, stdout=PIPE).communicate()[0]
print output1
output2 = Popen(['del', sourceVideo], stdout=PIPE).communicate()[0]
print output2
#5
1
Or you could use the os.path.walk function, which does more work for you than just os.walk:
或者你可以用os。path。步行功能,它对你的工作比仅仅是o。
A stupid example:
一个愚蠢的例子:
def walk_func(blah_args, dirname,names):
print ' '.join(('In ',dirname,', called with ',blah_args))
for name in names:
print 'Walked on ' + name
if __name__ == '__main__':
import os.path
directory = './'
arguments = '[args go here]'
os.path.walk(directory,walk_func,arguments)
#6
1
I had a similar problem, with a lot of help from the web and this post I made a small application, my target is VCD and SVCD and I don't delete the source but I reckon it will be fairly easy to adapt to your own needs.
我有一个类似的问题,在网上有很多帮助,这篇文章我做了一个小的应用,我的目标是VCD和SVCD,我不删除源代码,但我认为它很容易适应你自己的需要。
It can convert 1 video and cut it or can convert all videos in a folder, rename them and put them in a subfolder /VCD
它可以转换1个视频并剪切它,也可以将所有视频转换到一个文件夹中,重命名它们并将它们放到子文件夹/VCD中
I also add a small interface, hope someone else find it useful!
我还添加了一个小界面,希望有人发现它有用!
I put the code and file in here btw: http://tequilaphp.wordpress.com/2010/08/27/learning-python-making-a-svcd-gui/
我把代码和文件放在这里:http://tequilaphp.wordpress.com/2010/08/27/learning-python- make -a-svcd-gui/