Media Types: Difference between revisions

From GNU MediaGoblin Wiki
Jump to navigation Jump to search
(Created page with "Media types are plugin-like == File structure == <pre> mediagoblin/media_types/video ├── __init__.py # Contains the MEDIA_MANAGER ├── migrations.py # DB mi...")
 
No edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Media types are plugin-like
Media types are plugins (note, this page hasn't been fully updated since they were just plugin-like ;))


== File structure ==
== File structure ==
Line 9: Line 9:
├── models.py # DB models
├── models.py # DB models
├── processing.py # De-facto standard main file
├── processing.py # De-facto standard main file
├── transcoders.py # Additional python models for video
├── .... # Additional files
# specifically, name it appropriately
└── .... #
# for your media type.
</pre>
</pre>


Line 46: Line 45:
A path to the [[Media Types/Display|display template]], relative to <code>mediagoblin/templates</code>.
A path to the [[Media Types/Display|display template]], relative to <code>mediagoblin/templates</code>.


==== defailt_thumb ====
==== default_thumb ====


No current use.
No current use.
Line 53: Line 52:


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.
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 Types#MEDIA_MANAGER|MEDIA_MANAGERS]], then we resort to sniffing.

=== Sniffing ===

As a second step in media type negotiation, if the file-extension based lookup fails, <code>mediagoblin.media_types.get_media_type_and_manager</code> will call <code>mediagoblin.media_types.sniff_media</code>, 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.

<pre>
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
</pre>

Latest revision as of 20:06, 23 July 2013

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