0

For a reason I cannot understand I cannot do nothing with the output of Popen.communicate, except for print it to terminal.

If I save the output to a variable, the variable contains the text, because I can print it to terminal too, but len returns 0, re.search match nothing, and find always returns -1.

The offending function:

#!/usr/bin/env python
# coding: utf-8

import os
import sys
import getopt
import subprocess
import os.path
import re

def get_video_duration (ifile):
    p = subprocess.Popen(["ffprobe", ifile], stdout=subprocess.PIPE)
    info_str = p.communicate()[0].decode(sys.stdout.encoding)
    print(info_str) # for debug, the information about the file prints ok
    duration_start = info_str.find("Duration")
    # duration_start = "AJDKLAJSL Duration:".find("Duration"), this test was ok
    duration_end = info_str.find("start", duration_start)
    number_start = info_str.find(" ", duration_start, duration_end) + 1
    number_end = info_str.find(",", number_start, duration_end)

    temp = info_str[number_start:number_end].split(":")

    return int(temp[0]) * 60 * 60 + int(temp[1]) * 60 + int(temp[2])

I attempted different variations. Like do not use .decode(), change find for a single re.search, implement my own find by iterating each character (the problem is that I need len for this, and len always returns 0 for that particular string/byte_array).

It is a bug of some sort or I am doing something wrong about the string encoding. I cannot figure what exactly is the problem. Any help appreciated.

Ubuntu 12.10 64 bits Python 2.7

4

1 回答 1

1

您在编码方面没有做错任何事情。您的问题是 ffprobe 将其输出(包括您正在寻找的持续时间信息)发送到标准错误,而不是标准输出。这样做,你应该没问题:

def get_video_duration (ifile):
    p = subprocess.Popen(["ffprobe", ifile], stderr=subprocess.PIPE)
    info_str = p.communicate()[1].decode(sys.stderr.encoding)

您的 print() 调用似乎正常工作的原因是它没有打印任何内容(因为 info_str 确实是空的)......但是 stderr 输出被转储到控制台,这让您产生一种错觉,即您所看到的是您的 print() 调用的结果。

于 2013-04-24T08:08:19.173 回答