Skip to content

Commit 3626ec9

Browse files
committed
fixed stdout stderr handling
1 parent b03db1d commit 3626ec9

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

ffprobe/ffprobe.py

+29-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(self,video_file):
2626
except:
2727
raise IOError('ffprobe not found.')
2828
if os.path.isfile(video_file):
29-
p = subprocess.check_output(["ffprobe","-show_streams",self.video_file],stderr=subprocess.PIPE,shell=True)
29+
p = subprocess.Popen(["ffprobe","-show_streams",self.video_file],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
3030
self.format=None
3131
self.created=None
3232
self.duration=None
@@ -36,14 +36,24 @@ def __init__(self,video_file):
3636
self.video=[]
3737
self.audio=[]
3838
datalines=[]
39-
for a in str(p.decode(sys.stdout.encoding)).split('\n'):
39+
for a in iter(p.stdout.readline, b''):
4040
if re.match('\[STREAM\]',a):
4141
datalines=[]
4242
elif re.match('\[\/STREAM\]',a):
4343
self.streams.append(FFStream(datalines))
4444
datalines=[]
4545
else:
4646
datalines.append(a)
47+
for a in iter(p.stderr.readline, b''):
48+
if re.match('\[STREAM\]',a):
49+
datalines=[]
50+
elif re.match('\[\/STREAM\]',a):
51+
self.streams.append(FFStream(datalines))
52+
datalines=[]
53+
else:
54+
datalines.append(a)
55+
p.stdout.close()
56+
p.stderr.close()
4757
for a in self.streams:
4858
if a.isAudio():
4959
self.audio.append(a)
@@ -100,7 +110,11 @@ def frameSize(self):
100110
size=None
101111
if self.isVideo():
102112
if self.__dict__['width'] and self.__dict__['height']:
103-
size=(int(self.__dict__['width']),int(self.__dict__['height']))
113+
try:
114+
size=(int(self.__dict__['width']),int(self.__dict__['height']))
115+
except Exception as e:
116+
print "None integer size %s:%s" %(str(self.__dict__['width']),str(+self.__dict__['height']))
117+
size=(0,0)
104118
return size
105119

106120
def pixelFormat(self):
@@ -121,7 +135,10 @@ def frames(self):
121135
f=0
122136
if self.isVideo() or self.isAudio():
123137
if self.__dict__['nb_frames']:
124-
f=int(self.__dict__['nb_frames'])
138+
try:
139+
f=int(self.__dict__['nb_frames'])
140+
except Exception as e:
141+
print "None integer frame count"
125142
return f
126143

127144
def durationSeconds(self):
@@ -132,7 +149,10 @@ def durationSeconds(self):
132149
f=0.0
133150
if self.isVideo() or self.isAudio():
134151
if self.__dict__['duration']:
135-
f=float(self.__dict__['duration'])
152+
try:
153+
f=float(self.__dict__['duration'])
154+
except Exception as e:
155+
print "None numeric duration"
136156
return f
137157

138158
def language(self):
@@ -177,7 +197,10 @@ def bitrate(self):
177197
"""
178198
b=0
179199
if self.__dict__['bit_rate']:
180-
b=int(self.__dict__['bit_rate'])
200+
try:
201+
b=int(self.__dict__['bit_rate'])
202+
except Exception as e:
203+
print "None integer bitrate"
181204
return b
182205

183206
if __name__ == '__main__':

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
setup(
66
name='ffprobe',
7-
version='0.1',
7+
version='0.2',
88
description='Wrapper around ffprobe command to extract metadata from media files',
99
author='Simon Hargreaves',
1010
author_email='[email protected]',

0 commit comments

Comments
 (0)