Skip to content

Commit 69cd89f

Browse files
authored
Merge pull request gbstack#9 from norus/master
Allow specifying streams (e.g. IPTV) as input
2 parents 0647809 + 9b40bae commit 69cd89f

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
ffprobe-python module
22
=====================
33

4-
A wrapper around the ffprobe command to extract metadata from media files.
4+
A wrapper around the ffprobe command to extract metadata from media files or streams.
55

6-
Usage::
6+
Usage:
77

88
```python
99
#!/usr/bin/env python
1010

1111
from ffprobe import FFProbe
1212

13+
# Local file
1314
metadata=FFProbe('test-media-file.mov')
1415

16+
# Video stream
17+
# metadata=FFProbe('http://some-streaming-url.com:8080/stream')
18+
1519
for stream in metadata.streams:
1620
if stream.is_video():
1721
print('Stream contains {} frames.'.format(stream.frames()))

ffprobe/ffprobe.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, path_to_video):
2727
except FileNotFoundError:
2828
raise IOError('ffprobe not found.')
2929

30-
if os.path.isfile(self.path_to_video):
30+
if os.path.isfile(self.path_to_video) or self.path_to_video.startswith('http'):
3131
if platform.system() == 'Windows':
3232
cmd = ["ffprobe", "-show_streams", self.path_to_video]
3333
else:
@@ -105,7 +105,7 @@ def __init__(self, path_to_video):
105105
elif stream.is_attachment():
106106
self.attachment.append(stream)
107107
else:
108-
raise IOError('No such media file ' + self.path_to_video)
108+
raise IOError('No such media file or stream is not responding: ' + self.path_to_video)
109109

110110
def __repr__(self):
111111
return "<FFprobe: {metadata}, {video}, {audio}, {subtitle}, {attachment}>".format(**vars(self))
@@ -204,10 +204,14 @@ def frames(self):
204204
Returns the length of a video stream in frames. Returns 0 if not a video stream.
205205
"""
206206
if self.is_video() or self.is_audio():
207-
try:
208-
frame_count = int(self.__dict__.get('nb_frames', ''))
209-
except ValueError:
210-
raise FFProbeError('None integer frame count')
207+
if self.__dict__.get('nb_frames', '') != 'N/A':
208+
try:
209+
frame_count = int(self.__dict__.get('nb_frames', ''))
210+
except ValueError:
211+
raise FFProbeError('None integer frame count')
212+
else:
213+
# When N/A is returned, set frame_count to 0 too
214+
frame_count = 0
211215
else:
212216
frame_count = 0
213217

tests/ffprobe_test.py

+26
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
os.path.join(test_dir, './data/SampleVideo_1280x720_1mb.mp4'),
1212
]
1313

14+
# Taken from https://bitmovin.com/mpeg-dash-hls-examples-sample-streams
15+
test_streams = [
16+
'https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8',
17+
'https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8'
18+
]
19+
1420
def test_video ():
1521
for test_video in test_videos:
1622
media = FFProbe(test_video)
@@ -30,3 +36,23 @@ def test_video ():
3036
print(e)
3137
except Exception as e:
3238
print(e)
39+
40+
def test_stream ():
41+
for test_stream in test_streams:
42+
media = FFProbe(test_stream)
43+
print('File:', test_stream)
44+
print('\tStreams:', len(media.streams))
45+
for index, stream in enumerate(media.streams, 1):
46+
print('\tStream: ', index)
47+
try:
48+
if stream.is_video():
49+
frame_rate = stream.frames() / stream.duration_seconds()
50+
print('\t\tFrame Rate:', frame_rate)
51+
print('\t\tFrame Size:', stream.frame_size())
52+
print('\t\tDuration:', stream.duration_seconds())
53+
print('\t\tFrames:', stream.frames())
54+
print('\t\tIs video:', stream.is_video())
55+
except FFProbeError as e:
56+
print(e)
57+
except Exception as e:
58+
print(e)

0 commit comments

Comments
 (0)