├── .gitignore ├── LICENSE.txt ├── README.md ├── ffprobe3 ├── __init__.py ├── exceptions.py └── ffprobe.py ├── setup.py └── tests ├── __init__.py ├── data ├── SampleVideo_1280x720_1mb.mp4 ├── SampleVideo_1280x720_50mb.mp4 ├── SampleVideo_360x240_50mb.mp4 └── SampleVideo_720x480_5mb.mp4 └── ffprobe-test.py /.gitignore: -------------------------------------------------------------------------------- 1 | ### Python template 2 | # Byte-compiled / optimized / DLL files 3 | __pycache__/ 4 | *.py[cod] 5 | *$py.class 6 | 7 | # C extensions 8 | *.so 9 | 10 | # Distribution / packaging 11 | .Python 12 | env/ 13 | build/ 14 | develop-eggs/ 15 | dist/ 16 | downloads/ 17 | eggs/ 18 | .eggs/ 19 | lib/ 20 | lib64/ 21 | parts/ 22 | sdist/ 23 | var/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *,cover 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | 55 | # Sphinx documentation 56 | docs/_build/ 57 | 58 | # PyBuilder 59 | target/ 60 | ### JetBrains template 61 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio 62 | 63 | *.iml 64 | 65 | ## Directory-based project format: 66 | .idea/ 67 | # if you remove the above rule, at least ignore the following: 68 | 69 | # User-specific stuff: 70 | # .idea/workspace.xml 71 | # .idea/tasks.xml 72 | # .idea/dictionaries 73 | 74 | # Sensitive or high-churn files: 75 | # .idea/dataSources.ids 76 | # .idea/dataSources.xml 77 | # .idea/sqlDataSources.xml 78 | # .idea/dynamic.xml 79 | # .idea/uiDesigner.xml 80 | 81 | # Gradle: 82 | # .idea/gradle.xml 83 | # .idea/libraries 84 | 85 | # Mongo Explorer plugin: 86 | # .idea/mongoSettings.xml 87 | 88 | ## File-based project format: 89 | *.ipr 90 | *.iws 91 | 92 | ## Plugin-specific files: 93 | 94 | # IntelliJ 95 | /out/ 96 | 97 | # mpeltonen/sbt-idea plugin 98 | .idea_modules/ 99 | 100 | # JIRA plugin 101 | atlassian-ide-plugin.xml 102 | 103 | # Crashlytics plugin (for Android Studio and IntelliJ) 104 | com_crashlytics_export_strings.xml 105 | crashlytics.properties 106 | crashlytics-build.properties 107 | ### VirtualEnv template 108 | # Virtualenv 109 | # http://iamzed.com/2009/05/07/a-primer-on-virtualenv/ 110 | .Python 111 | [Bb]in 112 | [Ii]nclude 113 | [Ll]ib 114 | [Ss]cripts 115 | pyvenv.cfg 116 | pip-selfcheck.json 117 | 118 | # Created by .ignore support plugin (hsz.mobi) 119 | 120 | # Other 121 | MANIFEST 122 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DheerendraRathor/ffprobe3/82440a165a090b142082a12d99cd4d0f5bbad00e/LICENSE.txt -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ffprobe python module 2 | ===================== 3 | 4 | A wrapper around the ffprobe command to extract metadata from media files. 5 | 6 | Usage:: 7 | 8 | #!/usr/bin/env python 9 | 10 | from ffprobe3 import FFProbe 11 | 12 | metadata=FFProbe('test-media-file.mov') 13 | 14 | for stream in metadata.streams: 15 | if stream.is_video(): 16 | print('Stream contains {} frames.'.format(stream.frames())) 17 | 18 | 19 | (The MIT License) 20 | 21 | Copyright � 2013 Simon Hargreaves 22 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the �Software�), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 23 | 24 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 25 | 26 | THE SOFTWARE IS PROVIDED �AS IS�, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27 | -------------------------------------------------------------------------------- /ffprobe3/__init__.py: -------------------------------------------------------------------------------- 1 | from .ffprobe import FFProbe 2 | -------------------------------------------------------------------------------- /ffprobe3/exceptions.py: -------------------------------------------------------------------------------- 1 | class FFProbeError(Exception): 2 | pass 3 | -------------------------------------------------------------------------------- /ffprobe3/ffprobe.py: -------------------------------------------------------------------------------- 1 | """ 2 | Python wrapper for ffprobe command line tool. ffprobe must exist in the path. 3 | """ 4 | import functools 5 | import operator 6 | import os 7 | import pipes 8 | import platform 9 | import subprocess 10 | 11 | from ffprobe3.exceptions import FFProbeError 12 | 13 | 14 | class FFProbe: 15 | """ 16 | FFProbe wraps the ffprobe command and pulls the data into an object form:: 17 | metadata=FFProbe('multimedia-file.mov') 18 | """ 19 | 20 | def __init__(self, path_to_video): 21 | self.path_to_video = path_to_video 22 | 23 | try: 24 | with open(os.devnull, 'w') as tempf: 25 | subprocess.check_call(["ffprobe", "-h"], stdout=tempf, stderr=tempf) 26 | except FileNotFoundError: 27 | raise IOError('ffprobe not found.') 28 | 29 | if os.path.isfile(self.path_to_video): 30 | if platform.system() == 'Windows': 31 | cmd = ["ffprobe", "-show_streams", self.path_to_video] 32 | else: 33 | cmd = ["ffprobe -show_streams " + pipes.quote(self.path_to_video)] 34 | 35 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) 36 | 37 | stream = False 38 | self.streams = [] 39 | self.video = [] 40 | self.audio = [] 41 | self.subtitle = [] 42 | self.attachment = [] 43 | 44 | for line in iter(p.stdout.readline, b''): 45 | line = line.decode('UTF-8') 46 | 47 | if '[STREAM]' in line: 48 | stream = True 49 | data_lines = [] 50 | elif '[/STREAM]' in line and stream: 51 | stream = False 52 | # noinspection PyUnboundLocalVariable 53 | self.streams.append(FFStream(data_lines)) 54 | elif stream: 55 | data_lines.append(line) 56 | 57 | for line in iter(p.stderr.readline, b''): 58 | line = line.decode('UTF-8') 59 | 60 | if '[STREAM]' in line: 61 | stream = True 62 | data_lines = [] 63 | elif '[/STREAM]' in line and stream: 64 | stream = False 65 | self.streams.append(FFStream(data_lines)) 66 | elif stream: 67 | data_lines.append(line) 68 | 69 | p.stdout.close() 70 | p.stderr.close() 71 | 72 | for stream in self.streams: 73 | if stream.is_audio(): 74 | self.audio.append(stream) 75 | elif stream.is_video(): 76 | self.video.append(stream) 77 | elif stream.is_subtitle(): 78 | self.subtitle.append(stream) 79 | elif stream.is_attachment(): 80 | self.attachment.append(stream) 81 | else: 82 | raise IOError('No such media file ' + self.path_to_video) 83 | 84 | def __repr__(self): 85 | return "".format(**vars(self)) 86 | 87 | 88 | class FFStream: 89 | """ 90 | An object representation of an individual stream in a multimedia file. 91 | """ 92 | 93 | def __init__(self, data_lines): 94 | for line in data_lines: 95 | self.__dict__.update({key: value for key, value, *_ in [line.strip().split('=')]}) 96 | 97 | try: 98 | self.__dict__['framerate'] = round( 99 | functools.reduce( 100 | operator.truediv, map(int, self.__dict__.get('avg_frame_rate', '').split('/')) 101 | ) 102 | ) 103 | 104 | except ValueError: 105 | self.__dict__['framerate'] = None 106 | 107 | def __repr__(self): 108 | if self.is_video(): 109 | template = "" 110 | 111 | elif self.is_audio(): 112 | template = " " 114 | 115 | elif self.is_subtitle() or self.is_attachment(): 116 | template = "" 117 | 118 | else: 119 | template = '' 120 | 121 | return template.format(**self.__dict__) 122 | 123 | def is_audio(self): 124 | """ 125 | Is this stream labelled as an audio stream? 126 | """ 127 | return self.__dict__.get('codec_type', None) == 'audio' 128 | 129 | def is_video(self): 130 | """ 131 | Is the stream labelled as a video stream. 132 | """ 133 | return self.__dict__.get('codec_type', None) == 'video' 134 | 135 | def is_subtitle(self): 136 | """ 137 | Is the stream labelled as a subtitle stream. 138 | """ 139 | return self.__dict__.get('codec_type', None) == 'subtitle' 140 | 141 | def is_attachment(self): 142 | """ 143 | Is the stream labelled as a attachment stream. 144 | """ 145 | return self.__dict__.get('codec_type', None) == 'attachment' 146 | 147 | def frame_size(self): 148 | """ 149 | Returns the pixel frame size as an integer tuple (width,height) if the stream is a video stream. 150 | Returns None if it is not a video stream. 151 | """ 152 | size = None 153 | if self.is_video(): 154 | width = self.__dict__['width'] 155 | height = self.__dict__['height'] 156 | 157 | if width and height: 158 | try: 159 | size = (int(width), int(height)) 160 | except ValueError: 161 | raise FFProbeError("None integer size {}:{}".format(width, height)) 162 | else: 163 | return None 164 | 165 | return size 166 | 167 | def pixel_format(self): 168 | """ 169 | Returns a string representing the pixel format of the video stream. e.g. yuv420p. 170 | Returns none is it is not a video stream. 171 | """ 172 | return self.__dict__.get('pix_fmt', None) 173 | 174 | def frames(self): 175 | """ 176 | Returns the length of a video stream in frames. Returns 0 if not a video stream. 177 | """ 178 | if self.is_video() or self.is_audio(): 179 | try: 180 | frame_count = int(self.__dict__.get('nb_frames', '')) 181 | except ValueError: 182 | raise FFProbeError('None integer frame count') 183 | else: 184 | frame_count = 0 185 | 186 | return frame_count 187 | 188 | def duration_seconds(self): 189 | """ 190 | Returns the runtime duration of the video stream as a floating point number of seconds. 191 | Returns 0.0 if not a video stream. 192 | """ 193 | if self.is_video() or self.is_audio(): 194 | try: 195 | duration = float(self.__dict__.get('duration', '')) 196 | except ValueError: 197 | raise FFProbeError('None numeric duration') 198 | else: 199 | duration = 0.0 200 | 201 | return duration 202 | 203 | def language(self): 204 | """ 205 | Returns language tag of stream. e.g. eng 206 | """ 207 | return self.__dict__.get('TAG:language', None) 208 | 209 | def codec(self): 210 | """ 211 | Returns a string representation of the stream codec. 212 | """ 213 | return self.__dict__.get('codec_name', None) 214 | 215 | def codec_description(self): 216 | """ 217 | Returns a long representation of the stream codec. 218 | """ 219 | return self.__dict__.get('codec_long_name', None) 220 | 221 | def codec_tag(self): 222 | """ 223 | Returns a short representative tag of the stream codec. 224 | """ 225 | return self.__dict__.get('codec_tag_string', None) 226 | 227 | def bit_rate(self): 228 | """ 229 | Returns bit_rate as an integer in bps 230 | """ 231 | try: 232 | return int(self.__dict__.get('bit_rate', '')) 233 | except ValueError: 234 | raise FFProbeError('None integer bit_rate') 235 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from distutils.core import setup 4 | 5 | setup( 6 | name='ffprobe3', 7 | version='0.1.2', 8 | description=""" 9 | Original Project: ffprobe (https://pypi.python.org/pypi/ffprobe) 10 | 11 | A wrapper around ffprobe command to extract metadata from media files. 12 | 13 | This project which is maintained by Dheerendra Rathor is a Python 3 port of original ffprobe. 14 | """, 15 | author='Simon Hargreaves', 16 | author_email='simon@simon-hargreaves.com', 17 | maintainer='Dheerendra Rathor', 18 | maintainer_email='dheeru.rathor14@gmail.com', 19 | url='https://github.com/DheerendraRathor/ffprobe3', 20 | packages=['ffprobe3'], 21 | keywords='ffmpeg, ffprobe, mpeg, mp4', 22 | classifiers=[ 23 | 'Development Status :: 4 - Beta', 24 | 'Environment :: Console', 25 | 'Intended Audience :: Developers', 26 | 'License :: OSI Approved :: MIT License', 27 | 'Operating System :: MacOS :: MacOS X', 28 | 'Operating System :: Microsoft :: Windows', 29 | 'Operating System :: POSIX', 30 | 'Programming Language :: Python', 31 | 'Programming Language :: Python :: 2.7', 32 | 'Programming Language :: Python :: 3.5', 33 | 'Programming Language :: Python :: Implementation :: CPython', 34 | 'Natural Language :: English', 35 | 'Topic :: Multimedia :: Video', 36 | 'Topic :: Software Development :: Libraries' 37 | ]) 38 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DheerendraRathor/ffprobe3/82440a165a090b142082a12d99cd4d0f5bbad00e/tests/__init__.py -------------------------------------------------------------------------------- /tests/data/SampleVideo_1280x720_1mb.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DheerendraRathor/ffprobe3/82440a165a090b142082a12d99cd4d0f5bbad00e/tests/data/SampleVideo_1280x720_1mb.mp4 -------------------------------------------------------------------------------- /tests/data/SampleVideo_1280x720_50mb.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DheerendraRathor/ffprobe3/82440a165a090b142082a12d99cd4d0f5bbad00e/tests/data/SampleVideo_1280x720_50mb.mp4 -------------------------------------------------------------------------------- /tests/data/SampleVideo_360x240_50mb.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DheerendraRathor/ffprobe3/82440a165a090b142082a12d99cd4d0f5bbad00e/tests/data/SampleVideo_360x240_50mb.mp4 -------------------------------------------------------------------------------- /tests/data/SampleVideo_720x480_5mb.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DheerendraRathor/ffprobe3/82440a165a090b142082a12d99cd4d0f5bbad00e/tests/data/SampleVideo_720x480_5mb.mp4 -------------------------------------------------------------------------------- /tests/ffprobe-test.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import os 4 | from ffprobe3 import FFProbe 5 | from ffprobe3.exceptions import FFProbeError 6 | 7 | test_dir = os.path.dirname(os.path.abspath(__file__)) 8 | 9 | test_videos = [ 10 | os.path.join(test_dir, './data/SampleVideo_720x480_5mb.mp4'), 11 | os.path.join(test_dir, './data/SampleVideo_1280x720_1mb.mp4'), 12 | os.path.join(test_dir, './data/SampleVideo_360x240_50mb.mp4'), 13 | os.path.join(test_dir, './data/SampleVideo_1280x720_50mb.mp4'), 14 | ] 15 | 16 | for test_video in test_videos: 17 | media = FFProbe(test_video) 18 | print('File:', test_video) 19 | print('\tStreams:', len(media.streams)) 20 | for index, stream in enumerate(media.streams, 1): 21 | print('\tStream: ', index) 22 | try: 23 | if stream.is_video(): 24 | frame_rate = stream.frames() / stream.duration_seconds() 25 | print('\t\tFrame Rate:', frame_rate) 26 | print('\t\tFrame Size:', stream.frame_size()) 27 | print('\t\tDuration:', stream.duration_seconds()) 28 | print('\t\tFrames:', stream.frames()) 29 | print('\t\tIs video:', stream.is_video()) 30 | except FFProbeError as e: 31 | print(e) 32 | except Exception as e: 33 | print(e) 34 | --------------------------------------------------------------------------------