3

そこで、ビデオファイルを含むフォルダーと出力ディレクトリを指定できる小さなPythonスクリプトを作成しました。プログラムは、すべてのビデオファイルをループし、次のようなハンドブレーキを使用して変換します。

proc = subprocess.Popen('HandBrakeCLI -i ... -o ...')
proc.wait()

したがって、ディレクトリ内の各ファイルを1つずつ実行します。私はクアッドコアマシンを持っていて、ビデオ変換を並行して実行することで処理を高速化したいと思っていますが、そのアプローチ方法を完全には理解していません。

os.fork()のようなものが必要ですか?マルチプロセッシングモジュールのようなものですか?私はシングルスレッドのJavaScriptランドから来ているので、これは私にとって比較的新しいものです。

ご入力いただきありがとうございます。

4

2 に答える 2

2

これに似た何かがトリックを行うはずです:

import multiprocessing
...
def wrapper(currentfile):
    #create your commandline -- for the sake of simplicity, I assume that
    #the only thing that changes is the filename you are passing to
    #HandBrakeCLI (and that the filename is the last argument to pass)
    cmd='HandBrakeCLI -i ... -o ... '+currentfile 
    proc = subprocess.Popen(cmd)
    proc.wait()
    return 'foo'

files=os.listdir(path)  #Any way that you build up your list of files is fine
output = multiprocessing.Pool(4).map(wrapper,files) #output is ['foo', 'foo', ..., 'foo']

Of course, this uses a map-like function for it's side-effects which many python people dislike... but I find it to be intuitive enough -- especially if you leave a comment. I also made the function return 'foo' to demonstrate that you can access the return value from the function quite easily.

于 2012-05-22T17:18:40.723 に答える
0

I'd suggest using the envoy library. Under the hood it uses the threading library to spawn new cmd line programs, so if you use the connect function like so:

import envoy
proc = envoy.connect('HandBrakeCLI -i ... -o ...')
while proc.status_code = None:
    sleep(5)

You can spawn as many as you want at a time, and wait until one exits before spawning another. Please note if you have issues, that I've got a fork with my fixes that you may want to check.

shlexライブラリが引用符を処理する方法と、cmd行プログラムが何を期待しているのかという引用符の問題に遭遇しました。そして、私はそれをWindowsで使用したので、posix/non-posixモードの問題で。

于 2012-05-22T17:48:00.913 に答える