Skip to content

Instantly share code, notes, and snippets.

@Jthomas54
Created May 27, 2016 16:28
Show Gist options
  • Save Jthomas54/4896f648a6bd1e1144a89d68b906a3d4 to your computer and use it in GitHub Desktop.
Save Jthomas54/4896f648a6bd1e1144a89d68b906a3d4 to your computer and use it in GitHub Desktop.
Simple Elastic Transcoder Fluent Interface
using System;
using System.Collections.Generic;
using System.Linq;
using Amazon.ElasticTranscoder.Model;
namespace AmazonHelpers
{
public sealed class ElasticTranscoder
{
private ElasticTranscoder()
{
}
public sealed class JobBuilder : IJobBuilderAndCreateable, IJobOutputBuilder
{
private CreateJobRequest _jobRequest = new CreateJobRequest();
private CreateJobOutput _currentJobOutput;
/// <summary>
/// Creates a new instance of the ElasticTranscoder JobBuilder
/// </summary>
/// <returns></returns>
public static IJobBuilder Create()
{
return new JobBuilder();
}
private JobBuilder()
{
_jobRequest.Input = new JobInput();
_jobRequest.Outputs = new List<CreateJobOutput>();
}
public IJobBuilder UsePipeline(string pipeline)
{
_jobRequest.PipelineId = pipeline;
return this;
}
public IJobBuilder UseInputKey(string key)
{
_jobRequest.Input.Key = key;
return this;
}
public IJobOutputBuilder AddOutput()
{
_currentJobOutput = new CreateJobOutput();
return this;
}
public IJobOutputBuilder UsePresetId(string preset)
{
_currentJobOutput.PresetId = preset;
return this;
}
public IJobOutputBuilder WithOutputKey(string key)
{
_currentJobOutput.Key = key;
return this;
}
public IJobOutputBuilder ClipBetween(double start, double end)
{
if (_currentJobOutput.Composition == null)
_currentJobOutput.Composition = new List<Clip>();
var clip = new Clip();
clip.TimeSpan = new Amazon.ElasticTranscoder.Model.TimeSpan()
{
StartTime = Math.Round(start, 3).ToString(),
Duration = Math.Round(end, 3).ToString()
};
_currentJobOutput.Composition.Add(clip);
return this;
}
public IJobOutputBuilder AddCaptionOutputs(string keyPattern, params string[] captionFormats)
{
if (_currentJobOutput.Captions == null)
{
_currentJobOutput.Captions = new Captions()
{
MergePolicy = "MergeRetain",
CaptionFormats = new List<CaptionFormat>()
};
}
var hasFormatTag = keyPattern.IndexOf("{format}") > -1;
if (!hasFormatTag)
keyPattern += "{format}";
foreach (var format in captionFormats)
{
var captionFormat = new CaptionFormat()
{
Format = format,
Pattern = keyPattern.Replace("{format}", format)
};
_currentJobOutput.Captions.CaptionFormats.Add(captionFormat);
}
return this;
}
public IJobBuilderAndCreateable Push()
{
_jobRequest.Outputs.Add(_currentJobOutput);
return this;
}
public CreateJobRequest Build()
{
return _jobRequest;
}
}
}
/// <summary>
/// Interface used to help guide the user to setup job inputs and add an output
/// </summary>
public interface IJobBuilder
{
IJobBuilder UsePipeline(string pipeline);
IJobBuilder UseInputKey(string key);
IJobOutputBuilder AddOutput();
}
/// <summary>
/// Interface used to help guide the user to add output parameters
/// </summary>
public interface IJobOutputBuilder
{
IJobOutputBuilder UsePresetId(string preset);
IJobOutputBuilder WithOutputKey(string key);
IJobOutputBuilder ClipBetween(double start, double end);
IJobOutputBuilder AddCaptionOutputs(string keyPattern, params string[] captionType);
IJobBuilderAndCreateable Push();
}
/// <summary>
/// Interface used to hint to the user that they have added enough information to create a Job Request
/// </summary>
public interface IJobCreateable
{
CreateJobRequest Build();
}
/// <summary>
/// Interface used to join the IJobBuilder and IJobCreateable interfaces
/// </summary>
public interface IJobBuilderAndCreateable : IJobBuilder, IJobCreateable
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment