ActiveEncode
What is ActiveEncode?
ActiveEncode serves as the basis for the interface between a Ruby (Rails) application and a provider of encoding services such as FFmpeg, Amazon Elastic Transcoder, and Zencoder.
Help
The Samvera community is here to help. Please see our support guide.
Installation
Add this line to your application's Gemfile:
gem 'active_encode'
And then execute:
$ bundle
Or install it yourself as:
$ gem install active_encode
Prerequisites
FFmpeg (tested with version 4+) and mediainfo (version 17.10+) need to be installed to use the FFmpeg engine adapter.
Usage
Set the engine adapter (default: test), configure it (if neccessary), then submit encoding jobs!
ActiveEncode::Base.engine_adapter = :ffmpeg
file = "file://#{File.absolute_path "spec/fixtures/fireworks.mp4"}"
ActiveEncode::Base.create(file, { outputs: [{ label: "low", ffmpeg_opt: "-s 640x480", extension: "mp4"}, { label: "high", ffmpeg_opt: "-s 1280x720", extension: "mp4"}] })
Create returns an encoding job that has been submitted to the adapter for processing. At this point it will have an id, a state, the input, and any additional information the adapter returns.
#<ActiveEncode::Base:0x007f8ef3b2ae88 @input=#<ActiveEncode::Input:0x007f8ef3b23188 @url="file:///Users/cjcolvar/Documents/Code/samvera-labs/active_encode/spec/fixtures/fireworks.mp4", @width=960.0, @height=540.0, @frame_rate=29.671, @duration=6024, @file_size=1629578, @audio_codec="mp4a-40-2", @video_codec="avc1", @audio_bitrate=69737, @video_bitrate=2092780, @created_at=2018-12-03 14:22:05 -0500, @updated_at=2018-12-03 14:22:05 -0500, @id=7653>, @options={:outputs=>[{:label=>"low", :ffmpeg_opt=>"-s 640x480", :extension=>"mp4"}, {:label=>"high", :ffmpeg_opt=>"-s 1280x720", :extension=>"mp4"}]}, @id="1e4a907a-ccff-494f-ad70-b1c5072c2465", @created_at=2018-12-03 14:22:05 -0500, @updated_at=2018-12-03 14:22:05 -0500, @current_operations=[], @output=[], @state=:running, @percent_complete=1, @errors=[]>
encode.id # "1e4a907a-ccff-494f-ad70-b1c5072c2465"
encode.state # :running
This encode can be looked back up later using #find. Alternatively, use #reload to refresh an instance with the latest information from the adapter:
encode = ActiveEncode::Base.find("1e4a907a-ccff-494f-ad70-b1c5072c2465")
encode.reload
Progress of a running encode is shown with current operations (multiple are possible when outputs are generated in parallel) and percent complete. Technical metadata about the input file may be added by the adapter. This should include a mime type, checksum, duration, and basic technical details of the audio and video content of the file (codec, audio channels, bitrate, frame rate, and dimensions). Outputs are added once they are created and should include the same technical metadata along with an id, label, and url.
If you want to stop the encoding job call cancel:
encode.cancel!
encode.cancelled? # true
An encoding job is meant to be the record of the work of the encoding engine and not the current state of the outputs. Therefore moved or deleted outputs will not be reflected in the encoding job.
AWS ElasticTranscoder
To use active_encode with the AWS ElasticTransoder, the following are required:
- An S3 bucket to store master files
- An S3 bucket to store derivatives (recommended to be separate)
- An ElasticTranscoder pipeline
- Some transcoding presets for the pipeline
Set the adapter:
ActiveEncode::Base.engine_adapter = :elastic_transcoder
Construct the options hash:
outputs = [{ key: "quality-low/hls/fireworks", preset_id: '1494429796844-aza6zh', segment_duration: '2' },
{ key: "quality-medium/hls/fireworks", preset_id: '1494429797061-kvg9ki', segment_duration: '2' },
{ key: "quality-high/hls/fireworks", preset_id: '1494429797265-9xi831', segment_duration: '2' }]
= {pipeline_id: 'my-pipeline-id', masterfile_bucket: 'my-master-files', outputs: outputs}
Create the job:
file = 'file:///path/to/file/fireworks.mp4' # or 's3://my-bucket/fireworks.mp4'
encode = ActiveEncode::Base.create(file, )
Custom jobs
Subclass ActiveEncode::Base to add custom callbacks or default options. Available callbacks are before, after, and around the create and cancel actions.
class CustomEncode < ActiveEncode::Base
after_create do
logger.info "Created encode with id #{self.reload.id}"
end
def self.(input)
{preset: 'avalon-skip-transcoding'}
end
end
Engine Adapters
Engine adapters are shims between ActiveEncode and the back end encoding service. You can add an additional engine by creating an engine adapter class that implements :create
, :find
, and :cancel
and passes the shared specs.
Acknowledgments
This software has been developed by and is brought to you by the Samvera community. Learn more at the Samvera website.