2016-06-20 10 views
0

私はハンドブレーキとPythonを使用してスケジュールに基づいてビデオをエンコードしています。進歩を監視する必要があります。なぜなら、それを使ってエンコード時間を見積もるためです。それから私はそれを私のスケジューラーに合わせることができます。ハンドブレーキのPython監視の進捗状況

プロセスからETAおよび%完了を取得する際に問題が発生しています。ここに私がこれまで持っていたものがあります。

profile = ["HandBrakeCLI","-i",input,"-o","output","-e","x264"] 
cp = subprocess.Popen(profile, stderr=subprocess.PIPE, bufsize=1) 
for line in iter(cp.stderr.readline, b''): 

    # regex match for % complete and ETA 
    matches = re.match(r'.*(\d+\.\d+)\s%.*ETA\s(\d+)h(\d+)m(\d+)s', line.decode('utf-8')) 

    if matches: 
    print(matches.group()) 

    print(line), 

cp.stderr.close() 
cp.wait() 

実際には何が起こっているのかよく分かりません。スクリプトを実行すると、ETAと%完了が表示されます。

Encoding: task 1 of 1, 1.19 % (45.57 fps, avg 62.74 fps, ETA 00h08m01s) 

私はstdoutを試してみましたが、どちらも動作しません。

+0

Handbrake CLIは、プロセスが少し進んだときに新しい追加の行を出力せず、既存の行を変更するという問題があると確信しています。 'for line in inter..'の直後に行を印刷して、実際にあなたに与えるものを見てください。 – advance512

+0

は、forループが終了した後に出力されませんか?プロセスが終了したら?プロセスが進むにつれて情報を入手したいと思います。 –

+0

まず、stderrを反復処理しています。おそらく、stdoutを反復処理する必要があります。第二に、私は、cp.stderr.readlineが、サブプロセス自体から完全な行を返すと思います。 'handbreak'プロセスが終了した後の最後の行を印刷して、それがどのように見えるかを確認します。そして、私が言ったように、私は実際に何が受け取られているかを理解するために、forループの各行を印刷します。 – advance512

答えて

0

stderrではなくstdoutから読み込む必要があります。

profile = ["HandBrakeCLI","-i",input,"-o","output","-e","x264"] 
cp = subprocess.Popen(profile, stderr=subprocess.PIPE, strout=subprocess.PIPE, bufsize=1) 
for line in iter(cp.stdout.readline, b''): 

    # regex match for % complete and ETA 
    matches = re.match(r'.*(\d+\.\d+)\s%.*ETA\s(\d+)h(\d+)m(\d+)s', line.decode('utf-8')) 

    if matches: 
    print(matches.group()) 

    print(line), 

cp.stderr.close() 
cp.stdout.close() 
cp.wait() 

(clint.textui.progress.Barを使用)、バイト単位で読み進捗ラッパーを使用するには、(私の作品):

profile = ["HandBrakeCLI","-i",input,"-o","output","-e","x264"] 
cp = subprocess.Popen(profile, stderr=subprocess.PIPE, strout=subprocess.PIPE, close_fds=True) 
bar = Bar(label="Encoding %s" % input, width=30, expected_size=10000, every=1) 
bar.show(0) 

line = "" 
c = 0 

while True:  
    nl = cp.stdout.read(1) 
    if nl == '' and cp.poll() is not None: 
    break # Aborted, no characters available, process died. 
    if nl == "\n": 
    line = "" 
    elif nl == "\r": 
    # regex match for % complete and ETA, assuming the regex is ok. 
    matches = re.match(r'.*(\d+\.\d+)\s%.*ETA\s(\d+)h(\d+)m(\d+)s', line.decode('utf-8')) 

    if matches: 
     print(matches.group()) 
     # do something 
    line = "" 
    else: 
    line += nl 

error = cp.stderr.read() 
success = "Encode done!" in error 

コードをテストしていない、一致するように書き直しスレッドの初期投稿。

希望に役立ちます。

関連する問題