PHPackages                             ac/transcoding - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. ac/transcoding

ActiveLibrary[File &amp; Storage](/categories/file-storage)

ac/transcoding
==============

File transcoding abstraction component for PHP 5.3+.

0.4.4(12y ago)135473[8 issues](https://github.com/AmericanCouncils/Transcoding/issues)2MITPHPPHP &gt;=5.3.2

Since Aug 20Pushed 12y ago2 watchersCompare

[ Source](https://github.com/AmericanCouncils/Transcoding)[ Packagist](https://packagist.org/packages/ac/transcoding)[ Docs](http://github.com/americancouncils/transcoding)[ RSS](/packages/ac-transcoding/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (2)Versions (12)Used By (2)

Transcoding
===========

[](#transcoding)

The Transcoding component is a library for abstracting file transcoding tool usage, and the presets that configure them. If you want a quick way to use it directly without implementing it in code elsewhere, check out the [Mutate CLI App](http://github.com/americancouncils/Mutate) or the [Symfony2 Transcoding Bundle](http://github.com/americancouncils/TranscodingBundle).

Implementation Details &amp; Example Usage
------------------------------------------

[](#implementation-details--example-usage)

The Transcoding component consists of several parts.

1. The first is the `Transcoder` class which unifies the transcode process. It provides the glue through which various adapters, presets and transcoding jobs are registered and can interact in order to transcode files consistently and safely. It also dispatches pre/post/error events when any file is transcoded.
2. Second, there are `Adapter` classes which are plugins that receive a standard input file, provide some logic to transcode the file, and return a standard output file. They can put restrictions on the types of input/output files they are allowed to handle.
3. Third are the `Preset` classes, which provide groupings of options for an `Adapter` to use when implementing its transcode logic. They can also put restrictions on the types of input/output files they are allowed to handle.
4. Fourth are `File` instances. They are a thin extension of PHP's standard `SplFileObject` class. These, in conjunction with `Preset` instances, are what `Adapters` take as input. If the `Adapter` returns a file, it should also be an instance of `AC\Transcoding\File`.
5. Fifth are `FileHandlerDefinition` instances. These can be specified by Adapters, as well as Presets, and define what types of files are allowable as both input and output. These instances are used internally by the `Transcoder` to ensure valid input/output and to assist in building a valid output file path if none is specified, or to catch an invalid path if provided, before it gets to the adapter for the transcode process.

Below you will see basic example usage and implementation of each the items mentioned above.

### Transcoder

[](#transcoder)

The Transcoder does the work of standardizing the transcoding input and output. What exactly it does when transcoding a file is determined by the registered presets, and adapters.

#### Usage

[](#usage)

Using the Transcoder by its self is simple, as it has no dependencies. It can accept presets/adapters from anywhere, some of which may have their own dependencies if necessary.

```
$transcoder = new AC\Transcoding\Transcoder;

// ... register presets & adapters ...
$transcoder->registerAdapter(new MyCustomFFmpegAdapter("/path/to/ffmpeg"));
$transcoder->registerPreset(new FFmpeg/WebmHDPreset);

//transcode one file using a preset
$newFile = $transcoder->transcodeWithPreset($inputFilePath, 'webm-hd', $outputFilePath);

//transcode a file with a specific adaptor and options
$newFile = $transcoder->transcodeWithAdapter($inputFilePath, 'ffmpeg', array(
	/* options */
));

```

### Adapters

[](#adapters)

Adapters are wrappers for a pre-existing toolset which does the real work for any file conversion/manipulation. Technically these adapters can be anything. Common examples are `ffmpeg` for audio/video manipulation and ImageMagick for image manipulation in PHP. By default, the library provides `Adapter` implementations for several commonly used tools, including those just mentioned.

#### Registering an adapter

[](#registering-an-adapter)

Adapters can be fairly simple, or quite complex. The adapters included in the library do not have external dependencies which aren't provided by the library (aside from requiring certain tools be installed on the system). However, other adapters may require external PHP dependencies and special set-up. It is beyond the scope of the library to handle this.

```
//build your custom adapter
$adapter = new MyCoolAdapter(/* inject any dependencies */);
$transcoder->registerAdapter($adapter);

//register adapters provided with the library
$transcoder->registerAdapter(new FFmpegAdapter);
$transcoder->registerAdapter(new ImageFormatConverterAdapter);
$transcoder->registerAdapter(new ImageEffectsAdapter);

```

#### Writing an adapter

[](#writing-an-adapter)

All adapters receive input in the same way - they simply take an input file object, a string output path, and a `Preset` instance for use during the transcode process. Generally, adapters aren't used directly, but the `Transcoder` will call passing along registered presets, and testing for valid input/output based on the preset definition. Below is an example template for a very simple custom adapter. For more detailed documentation on writing an adapter, see the `README.md` in `adapters/`.

```
