79

我正在尝试创建一个 Python 脚本:

  1. 查看文件夹“/输入”
  2. 对于该文件夹中的每个视频,运行 mencoder 命令(将它们转码为可在我的手机上播放的内容)
  3. 一旦 mencoder 完成运行,请删除原始视频。

这似乎不太难,但我很讨厌 python :)

关于脚本应该是什么样子的任何想法?

奖励问题:我应该使用

操作系统

或者

子进程调用

?

Subprocess.call 似乎允许编写更具可读性的脚本,因为我可以这样编写命令:

cmdLine = ['mencoder', sourceVideo, '-ovc', 'copy', '-oac', 'copy', '-ss', '00:02:54', '-endpos', '00:00: 54', '-o', 目的地视频]

编辑:好的,这有效:

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')

为了清楚起见,我已经删除了 mencoder 命令,因为我仍在努力。

感谢大家的意见。

4

7 回答 7

149

要查找所有文件名,请使用os.listdir().

然后你遍历文件名。像这样:

import os
for filename in os.listdir('dirname'):
     callthecommandhere(blablahbla, filename, foo)

如果您更喜欢子流程,请使用子流程。:-)

于 2009-07-13T16:55:06.170 回答
34

使用os.walk递归遍历目录内容:

import os

root_dir = '.'

for directory, subdirectories, files in os.walk(root_dir):
    for file in files:
        print os.path.join(directory, file)

os.system 和 subprocess.call 在这里没有真正的区别——除非你必须处理名字奇怪的文件(文件名包括空格、引号等)。如果是这种情况, subprocess.call 肯定更好,因为您不需要对文件名进行任何 shell 引用。当您需要接受任何有效的 shell 命令(例如从配置文件中的用户接收)时,os.system 会更好。

于 2009-07-13T17:01:45.360 回答
8

Python 可能对此有点矫枉过正。

for file in *; do mencoder -some options "$file"; rm -f "$file" ; done

rm -f "$file"删除文件。

于 2009-07-13T18:58:39.290 回答
7

Python3 中新的推荐方式是使用pathlib

from pathlib import Path

mydir = Path("path/to/my/dir")
for file in mydir.glob('*.mp4'):
    print(file.name)
    # do your stuff

代替*.mp4您可以使用任何过滤器,甚至是递归过滤器,例如**/*.mp4. 如果您想使用多个扩展名,您可以简单地用*or **/*(递归)迭代所有文件并检查每个文件的扩展名file.name.endswith(('.mp4', '.webp', '.avi', '.wmv', '.mov'))

于 2021-02-20T22:20:38.360 回答
3

AVIMPG(选择您的扩展名):

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
于 2009-07-13T17:08:33.240 回答
1

或者你可以使用 os.path.walk 函数,它比 os.walk 为你做更多的工作:

一个愚蠢的例子:

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)
于 2009-07-13T18:24:06.397 回答
1

我遇到了类似的问题,在网络的帮助下,这篇文章我做了一个小应用程序,我的目标是 VCD 和 SVCD,我没有删除源,但我认为它会很容易适应你自己的需要。

它可以转换 1 个视频并将其剪切,也可以转换文件夹中的所有视频,重命名它们并将它们放在子文件夹 /VCD 中

我还添加了一个小界面,希望别人觉得有用!

我把代码和文件放在这里顺便说一句:http: //tequilaphp.wordpress.com/2010/08/27/learning-python-making-a-svcd-gui/

于 2010-08-27T11:53:07.513 回答