Skip to content

Commit 5c2f2f3

Browse files
updated ffprobe to include side data without the need for extra class
also updated test
1 parent 4b184b9 commit 5c2f2f3

4 files changed

Lines changed: 60 additions & 70 deletions

File tree

ffprobe/ffprobe.py

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,13 @@ def __init__(self, path_to_video):
4848
line = line.decode('UTF-8', 'ignore')
4949

5050
if '[STREAM]' in line:
51-
side_data = False
5251
stream = True
5352
data_lines = []
5453
elif '[/STREAM]' in line and stream:
55-
side_data = False
5654
stream = False
5755
self.streams.append(FFStream(data_lines))
58-
elif '[SIDE_DATA]' in line:
59-
side_data = True
60-
stream = False
61-
side_data_lines = []
62-
elif '[/SIDE_DATA]' in line:
63-
side_data = False
64-
stream = False
65-
self.side_data.append(FFSideData(side_data_lines))
6656
elif stream and "=" in line:
6757
data_lines.append(line)
68-
elif side_data and "=" in line:
69-
side_data_lines.append(line)
7058

7159
self.metadata = {}
7260
is_metadata = False
@@ -200,6 +188,16 @@ def frame_size(self):
200188

201189
return size
202190

191+
def frame_rotation(self):
192+
"""
193+
Returns the rotation as an integer if the stream is a video stream.
194+
Returns None if it is not a video stream or the rotation does not exist
195+
"""
196+
if self.is_video():
197+
return int(self.__dict__.get('rotation', 0))
198+
else:
199+
return None
200+
203201
def pixel_format(self):
204202
"""
205203
Returns a string representing the pixel format of the video stream. e.g. yuv420p.
@@ -272,20 +270,3 @@ def bit_rate(self):
272270
return int(self.__dict__.get('bit_rate', ''))
273271
except ValueError:
274272
raise FFProbeError('None integer bit_rate')
275-
276-
277-
class FFSideData:
278-
"""
279-
An object representation of the side data in an individual stream in a multimedia file.
280-
"""
281-
282-
def __init__(self, data_lines):
283-
for line in data_lines:
284-
self.__dict__.update({key: value for key, value, *_ in [line.strip().split('=')]})
285-
286-
def rotation(self):
287-
"""
288-
Returns the rotation as an integer if the stream is a video stream.
289-
Returns None if it is not a video stream or the rotation does not exist
290-
"""
291-
return self.__dict__.get('rotation')

tests/data/landscape.mov

24.2 MB
Binary file not shown.

tests/data/portrait.mov

41.2 MB
Binary file not shown.

tests/ffprobe_test.py

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
from __future__ import print_function
22

33
import os
4+
import pytest
45
from ffprobe import FFProbe
56
from ffprobe.exceptions import FFProbeError
67

78
test_dir = os.path.dirname(os.path.abspath(__file__))
89

910
test_videos = [
10-
os.path.join(test_dir, './data/SampleVideo_720x480_5mb.mp4'),
11-
os.path.join(test_dir, './data/SampleVideo_1280x720_1mb.mp4'),
11+
# os.path.join(test_dir, './data/SampleVideo_720x480_5mb.mp4'),
12+
# os.path.join(test_dir, './data/SampleVideo_1280x720_1mb.mp4'),
13+
# os.path.join(test_dir, './data/landscape.mov'),
14+
os.path.join(test_dir, './data/portrait.mov'),
1215
]
1316

1417
# Taken from https://bitmovin.com/mpeg-dash-hls-examples-sample-streams
@@ -17,42 +20,48 @@
1720
'https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8'
1821
]
1922

20-
def test_video ():
21-
for test_video in test_videos:
22-
media = FFProbe(test_video)
23-
print('File:', test_video)
24-
print('\tStreams:', len(media.streams))
25-
for index, stream in enumerate(media.streams, 1):
26-
print('\tStream: ', index)
27-
try:
28-
if stream.is_video():
29-
frame_rate = stream.frames() / stream.duration_seconds()
30-
print('\t\tFrame Rate:', frame_rate)
31-
print('\t\tFrame Size:', stream.frame_size())
32-
print('\t\tDuration:', stream.duration_seconds())
33-
print('\t\tFrames:', stream.frames())
34-
print('\t\tIs video:', stream.is_video())
35-
except FFProbeError as e:
36-
print(e)
37-
except Exception as e:
38-
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)
23+
24+
class TestFfprobe:
25+
26+
def test_video(self):
27+
for test_video in test_videos:
28+
media = FFProbe(test_video)
29+
print('File:', test_video)
30+
print('\tStreams:', len(media.streams))
31+
for index, stream in enumerate(media.streams, 1):
32+
print('\tStream: ', index)
33+
try:
34+
if stream.is_video():
35+
frame_rate = stream.frames() / stream.duration_seconds()
36+
print('\t\tFrame Rate:', frame_rate)
37+
print('\t\tFrame Size:', stream.frame_size())
38+
print('\t\tDuration:', stream.duration_seconds())
39+
print('\t\tFrames:', stream.frames())
40+
print('\t\tIs video:', stream.is_video())
41+
print('\t\tRotation:', stream.frame_rotation())
42+
except FFProbeError as e:
43+
print(e)
44+
except Exception as e:
45+
print(e)
46+
47+
@pytest.mark.skip(reason="stream data no longer exists")
48+
def test_stream(self):
49+
for test_stream in test_streams:
50+
media = FFProbe(test_stream)
51+
print('File:', test_stream)
52+
print('\tStreams:', len(media.streams))
53+
for index, stream in enumerate(media.streams, 1):
54+
print('\tStream: ', index)
55+
try:
56+
if stream.is_video():
57+
frame_rate = stream.frames() / stream.duration_seconds()
58+
print('\t\tFrame Rate:', frame_rate)
59+
print('\t\tFrame Size:', stream.frame_size())
60+
print('\t\tDuration:', stream.duration_seconds())
61+
print('\t\tFrames:', stream.frames())
62+
print('\t\tIs video:', stream.is_video())
63+
print('\t\tRotation:', stream.rotation)
64+
except FFProbeError as e:
65+
print(e)
66+
except Exception as e:
67+
print(e)

0 commit comments

Comments
 (0)