Skip to content
main
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
app
 
 
 
 
 
 
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

ActiveEncode

Code: Version Build Status Coverage Status

Docs: Contribution Guidelines Apache 2.0 License

Jump in: Slack Status

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' }]
options = {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, options)

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.default_options(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.

For example:

# In your application at:
# lib/active_encode/engine_adapters/my_custom_adapter.rb
module ActiveEncode
  module EngineAdapters
    class MyCustomAdapter
      def create(input_url, options = {})
        # Start a new encoding job. This may be an external service, or a
        # locally queued job.

        # Return an instance ActiveEncode::Base (or subclass) that represents
        # the encoding job that was just started.        
      end

      def find(id, opts = {})
        # Find the encoding job for the given parameters.

        # Return an instance of ActiveEncode::Base (or subclass) that represents
        # the found encoding job.
      end

      def cancel(id)
        # Cancel the encoding job for the given id.

        # Return an instance of ActiveEncode::Base (or subclass) that represents
        # the canceled job.
      end
    end
  end
end

Then, use the shared specs...

# In your application at...
# spec/lib/active_encode/engine_adapters/my_custom_adapter_spec.rb
require 'spec_helper'
require 'active_encode/spec/shared_specs'
RSpec.describe MyCustomAdapter do
  let(:created_job) {
    # an instance of ActiveEncode::Base represented a newly created encode job
  }
  let(:running_job) {
    # an instance of ActiveEncode::Base represented a running encode job
  }
  let(:canceled_job) {
    # an instance of ActiveEncode::Base represented a canceled encode job
  }
  let(:completed_job) {
    # an instance of ActiveEncode::Base represented a completed encode job
  }
  let(:failed_job) {
    # an instance of ActiveEncode::Base represented a failed encode job
  }
  let(:completed_tech_metadata) {
    # a hash representing completed technical metadata
  }
  let(:completed_output) {
    # data representing completed output
  }
  let(:failed_tech_metadata) {
    # a hash representing failed technical metadata
  }

  # Run the shared specs.
  it_behaves_like 'an ActiveEncode::EngineAdapter'
end

Acknowledgments

This software has been developed by and is brought to you by the Samvera community. Learn more at the Samvera website.

Samvera Logo

About

Declare encode job classes that can be run by a variety of encoding services

Resources

License

Packages

No packages published

Languages