简体   繁体   中英

Print subprocess.Popen() output in Python

I am iterating through a directory and running ffprobe against each file to get it's duration. However, the only output I get is

b''
b''

Below is the part of the code I am using,

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)

Currently, the directory I am iterating through has two files in it.

Any ideas how to get the actual duration printed please?

Thanks.

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)

works fine for me. (That is, using check_output() and adding the encoding parameter.)

The result is

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

as expected.

My Ffmpeg version is, for what it's worth,

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

If your internal command is correct, the way to get subprocess output is as so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM