Media Types

From GNU MediaGoblin Wiki
Jump to navigation Jump to search

Media types are plugins (note, this page hasn't been fully updated since they were just plugin-like ;))

File structure

mediagoblin/media_types/video
├── __init__.py      # Contains the MEDIA_MANAGER
├── migrations.py    # DB migrations
├── models.py        # DB models
├── processing.py    # De-facto standard main file
├── ....             # Additional files
└── ....             # 

MEDIA_MANAGER

The media manager contains metadata about the media type, along with a reference to the method used to process the media.

MEDIA_MANAGER = {
    "human_readable": "Video",
    "processor": process_video,
    "sniff_handler": sniff_handler,
    "display_template": "mediagoblin/media_displays/video.html",
    "default_thumb": "images/media_thumbs/video.jpg",
    "accepted_extensions": [
        "mp4", "mov", "webm", "avi", "3gp", "3gpp", "mkv", "ogv", "m4v"]}

human_readable

A human-readable name for the media type.

processor

A reference to the media processing method for the media type.

sniff_handler

A reference to the sniffing handler for the media type.

display_template

A path to the display template, relative to mediagoblin/templates.

default_thumb

No current use.

accepted_extensions

A list containing file extensions that should be associated to this media type. This is used as a fast way to get the media type based on an uploaded file before MediaGoblin falls back to sniffing.

Negotiating media type based on a file upload

To decide which of the available media types that should handle a file upload, we first match the file extension to extensions in the available MEDIA_MANAGERS, then we resort to sniffing.

Sniffing

As a second step in media type negotiation, if the file-extension based lookup fails, mediagoblin.media_types.get_media_type_and_manager will call mediagoblin.media_types.sniff_media, which will iterate and run all the enabled media types's sniffing handlers.

The sniffing handlers can respond with either true or false, responding with true indicates that the media type believes it can parse the file.

This is mediagoblin.media_types.video.processing.sniff_handler.

def sniff_handler(media_file, **kw):
    transcoder = transcoders.VideoTranscoder()
    data = transcoder.discover(media_file.name)

    _log.debug('Discovered: {0}'.format(data))

    if not data:
        _log.error('Could not discover {0}'.format(
                kw.get('media')))
        return False

    if data['is_video'] == True:
        return True

    return False