Skip to content

Instantly share code, notes, and snippets.

@seanupton
Last active August 29, 2015 14:07
Show Gist options
  • Save seanupton/f1e0e72efed014d400b4 to your computer and use it in GitHub Desktop.
Save seanupton/f1e0e72efed014d400b4 to your computer and use it in GitHub Desktop.
Media encoding options (policy) utility interface
from zope.interface import Interface
class IMediaEncodingPolicy(Interface):
"""
Utility interface, to determine options for transcoding based on
container and/or stream metadata, which is assumed to be a dict matching
the avprobe JSON schema, with two primary keys of 'format' (container),
and 'streams' -- merged from two respective avprobe commands:
$ avprobe -v 0 -show_format -of json [INPUTFILE]
$ avprobe -v 0 -show_streams -of json [INPUTFILE]
It may be preferable for implementations to subclass or call base
implementations for fallback purposes; the exact mechanism by which
this is done is left to the implementation, as attempting to use a
component registry (e.g. zope.component.getAllUtilitiesRegisteredFor)
for this may have issues with enforcing desired order.
"""
def encoding_options(media_info, output_format, profile=None):
"""
Given media_info (metadata dict matching schema from avprobe), and
output_format (name such as 'webm', 'ogg', 'mp4'). Implementations
should be capable of handling partial, empty, or NoneType media_info
metadata, the result of which is situation/implementation-dependent.
An optional profile name for the output may be provided, if there
are sensible named presets to encode with.
Returns a list of command-line tokens suitable to insert into command
run by wildcard.media.convert.BaseSubProcess.
If no options are to be found, returns None; in such case,
"""
def replace_original(media_info, output_format, profile=None):
"""
Given metadata about the input/souce media, output format, and
an optional profile name, determine if original should be replaced
by a transcoded version for the given output_format.
Returns boolean. Should only return True if output_format matches
the source media container type (e.g. output_format is 'mp4' and
the source/uploaded media is an mpeg4 file), and the situation
dictates that original should be replaced (e.g. you have a "High"
profile h264 video and want to support devices that only support
the "Baseline" profile).
"""
@seanupton
Copy link
Author

Purpose here is an interface that the conversion process can use after it has introspected source media, but before conversion (via avconv) is run, to compute options.

It is supposed that there could be some base utility, but if such existed, it could if necessary be overridden by a site-specific policy implementation in a site policy product add-on package (anything of the sort might subclass the original and/or fall back to it's decisions when appropriate).

@seanupton
Copy link
Author

One of my other motives for policy based on introspecting the source metadata was the discovery that we need different quality parameters on my site for h264 when the source material was SD vs HD. It was easier to use more aggressive encoding on the HD streams with less notice of quality decline; I had more complaints about the same encoder settings on the SD source material, and have less aggressive need to speed up encoding or limit filesize, since that is less of an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment