GithubHelp home page GithubHelp logo

nulib / active_encode Goto Github PK

View Code? Open in Web Editor NEW

This project forked from samvera-labs/active_encode

0.0 2.0 0.0 2.04 MB

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

License: Other

Ruby 100.00%

active_encode's Introduction

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

active_encode's People

Contributors

afred avatar bkeese avatar cbeer avatar cjcolvar avatar jcoyne avatar jeremyf avatar mbklein avatar mjgiarlo avatar phuongdh avatar tpendragon avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.