Extracting Video Metadata¶
DeFFcode's Sourcer API acts as Source Probing Utility for easily probing metadata information for each multimedia stream available in the given video source, and return it as in Human-readable (as JSON string) or Machine-readable (as Dictionary object) type with its
retrieve_metadata()
class method. Apart from this, you can also usemetadata
property object in FFdecoder API to extract this metadata information (only as JSON string).
We'll discuss video metadata extraction using both these APIs briefly in the following recipes:
DeFFcode APIs requires FFmpeg executable
DeFFcode APIs MUST requires valid FFmpeg executable for all of its core functionality, and any failure in detection will raise RuntimeError
immediately. Follow dedicated FFmpeg Installation doc ➶ for its installation.
Never name your python script deffcode.py
When trying out these recipes, never name your python script deffcode.py
otherwise it will result in ModuleNotFound
error.
Extracting video metadata using Sourcer API¶
This is the recommended way for extracting video metadata.
In this example we will probe all metadata information available within foo.mp4
video file on Windows machine, and print it in both Human-readable (as JSON string) and Machine-readable (as Dictionary object) types using retrieve_metadata()
class method in Sourcer API:
The Sourcer API's retrieve_metadata()
class method provides pretty_json
boolean parameter to return metadata as JSON string (if True
) and as Dictionary (if False
).
# import the necessary packages
from deffcode import Sourcer
# initialize and formulate the decoder using suitable source
sourcer = Sourcer("foo.mp4").probe_stream()
# print metadata as `json.dump`
print(sourcer.retrieve_metadata(pretty_json=True))
After running above python code, the resultant Terminal Output will look something as following on Windows machine:
{
"ffmpeg_binary_path": "C:\\Users\\foo\\AppData\\Local\\Temp\\ffmpeg-static-win64-gpl/bin/ffmpeg.exe",
"source": "foo.mp4",
"source_extension": ".mp4",
"source_video_resolution": [
1280,
720
],
"source_video_framerate": 25.0,
"source_video_pixfmt": "yuv420p",
"source_video_decoder": "h264",
"source_duration_sec": 5.31,
"approx_video_nframes": 133,
"source_video_bitrate": "1205k",
"source_audio_bitrate": "384k",
"source_audio_samplerate": "48000 Hz",
"source_has_video": true,
"source_has_audio": true,
"source_has_image_sequence": false,
}
# import the necessary packages
from deffcode import Sourcer
# initialize and formulate the decoder using suitable source
sourcer = Sourcer("foo.mp4").probe_stream()
# print metadata as `dict`
print(sourcer.retrieve_metadata())
After running above python code, the resultant Terminal Output will look something as following on Windows machine:
{'ffmpeg_binary_path': 'C:\\Users\\foo\\AppData\\Local\\Temp\\ffmpeg-static-win64-gpl/bin/ffmpeg.exe', 'source': 'foo.mp4', 'source_extension': '.mp4', 'source_video_resolution': [1280, 720], 'source_video_framerate': 25.0, 'source_video_pixfmt': 'yuv420p', 'source_video_decoder': 'h264', 'source_duration_sec': 5.31, 'approx_video_nframes': 133, 'source_video_bitrate': '1205k', 'source_audio_bitrate': '384k', 'source_audio_samplerate': '48000 Hz', 'source_has_video': True, 'source_has_audio': True, 'source_has_image_sequence': False}
Extracting video metadata using FFdecoder API¶
In this example we will probe all metadata information available within foo.mp4
video file on Windows machine, and print it as JSON string using metadata
property object in FFdecoder API.
You can also update video's metadata by using the same overloaded metadata
property object in FFdecoder API. More information can be found in this Advanced Recipe ➶
# import the necessary packages
from deffcode import FFdecoder
# initialize and formulate the decoder using suitable source
decoder = FFdecoder("foo.mp4").formulate()
# print metadata as `json.dump`
print(decoder.metadata)
# terminate the decoder
decoder.terminate()
After running above python code, the resultant Terminal Output will look something as following on Windows machine:
{
"ffmpeg_binary_path": "C:\\Users\\foo\\AppData\\Local\\Temp\\ffmpeg-static-win64-gpl/bin/ffmpeg.exe",
"source": "foo.mp4",
"source_extension": ".mp4",
"source_video_resolution": [
1280,
720
],
"source_video_framerate": 25.0,
"source_video_pixfmt": "yuv420p",
"source_video_decoder": "h264",
"source_duration_sec": 5.31,
"approx_video_nframes": 133,
"source_video_bitrate": "1205k",
"source_audio_bitrate": "384k",
"source_audio_samplerate": "48000 Hz",
"source_has_video": true,
"source_has_audio": true,
"source_has_image_sequence": false,
"ffdecoder_operational_mode": "Video-Only",
"output_frames_pixfmt": "rgb24"
}