簡體   English   中英

在 Python 中打印 subprocess.Popen() output

[英]Print subprocess.Popen() output in Python

我正在遍歷一個目錄並對每個文件運行ffprobe以獲得它的持續時間。 但是,我得到的唯一 output 是

b''
b''

以下是我正在使用的部分代碼,

for y in filename:
    p1 = subprocess.Popen (['ffprobe', '-i', y, '-show_entries', 'format=duration', '-sexagesimal', '-v', 'quiet', '-of', 'csv=%s' % ("p=0")], stdout=subprocess.PIPE).communicate()[0]
    print (p1)

目前,我正在迭代的目錄中有兩個文件。

有什么想法可以打印出實際的持續時間嗎?

謝謝。

import subprocess
import glob

for y in glob.glob("*.mkv"):
    p1 = subprocess.check_output(
        [
            "ffprobe",
            "-i",
            y,
            "-show_entries",
            "format=duration",
            "-sexagesimal",
            "-v",
            "quiet",
            "-of",
            "csv=%s" % ("p=0"),
        ],
        encoding="utf-8",
    ).strip()
    print(y, p1)

對我來說很好。 (即使用check_output()並添加encoding參數。)

結果是

2018-02-19T00:01.mkv 0:17:47.120000

正如預期的那樣。

我的 Ffmpeg 版本值得,

ffmpeg version 4.2.1 Copyright (c) 2000-2019 the FFmpeg developers
built with Apple clang version 11.0.0 (clang-1100.0.33.8)
libavutil      56. 31.100 / 56. 31.100
libavcodec     58. 54.100 / 58. 54.100
libavformat    58. 29.100 / 58. 29.100
libavdevice    58.  8.100 / 58.  8.100
libavfilter     7. 57.100 /  7. 57.100
libavresample   4.  0.  0 /  4.  0.  0
libswscale      5.  5.100 /  5.  5.100
libswresample   3.  5.100 /  3.  5.100
libpostproc    55.  5.100 / 55.  5.100

如果您的內部命令正確,則獲取子進程 output 的方法如下:

p1 = subprocess.run(['ffprobe', '-i', y, '-show_entries', 'format=duration', '-sexagesimal', '-v', 'quiet', '-of', 'csv=%s' % ("p=0")], shell=True, capture_output=True).stdout

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM