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