Ruby FFMPEG Project to Read Metadata & Transcode Videos in Command Line

 

 

Streamio FFMPEG

Build Status Code Climate Test Coverage

Simple yet powerful wrapper around the ffmpeg command for reading metadata and transcoding movies.

All work on this project is sponsored by the online video platform Streamio from Rackfish.

Streamio

Installation

Compatibility

Ruby

Only guaranteed to work with MRI Ruby 1.9.3 or later. Should work with rubinius head in 1.9 mode. Will not work in jruby until they fix: http://goo.gl/Z4UcX (should work in the upcoming 1.7.5)

ffmpeg

The current gem is tested against ffmpeg 2.8.4. So no guarantees with earlier (or much later) versions. Output and input standards have inconveniently changed rather a lot between versions of ffmpeg. My goal is to keep this library in sync with new versions of ffmpeg as they come along.

On macOS: brew install ffmpeg.

Usage

Require the gem

Reading Metadata

Transcoding

First argument is the output file path.

Keep track of progress with an optional block.

Give custom command line options with an array.

Use the EncodingOptions parser for humanly readable transcoding options. Below you’ll find most of the supported options. Note that the :custom key is an array so that it can be used for FFMpeg options like -map that can be repeated:

The transcode function returns a Movie object for the encoded file.

Aspect ratio is added to encoding options automatically if none is specified.

Preserve aspect ratio on width or height by using the preserve_aspect_ratio transcoder option.

For constant bitrate encoding use video_min_bitrate and video_max_bitrate with buffer_size.

Specifying Input Options

To specify which options apply the input, such as changing the input framerate, use input_options hash in the transcoder_options.

Overriding the Input Path

If FFMPEG’s input path needs to specify a sequence of files, rather than a path to a single movie, transcoding_options input can be set. If this option is present, the path of the original movie will not be used.

Watermarking

Add watermark image on the video.

For example, you want to add a watermark on the video at right top corner with 10px padding.

Position can be “LT” (Left Top Corner), “RT” (Right Top Corner), “LB” (Left Bottom Corner), “RB” (Right Bottom Corner). The watermark will not appear unless watermark_options specifies the position. padding_x and padding_y default to 10.

Taking Screenshots

You can use the screenshot method to make taking screenshots a bit simpler.

The screenshot method has the very same API as transcode so the same options will work.

To generate multiple screenshots in a single pass, specify vframes and a wildcard filename. Make sure to disable output file validation. The following code generates up to 20 screenshots every 10 seconds:

To specify the quality when generating compressed screenshots (.jpg), use quality which specifies ffmpeg -v:q option. Quality is an integer between 1 and 31, where lower is better quality:

You can preserve aspect ratio the same way as when using transcode.

Create a Slideshow from Stills

Creating a slideshow from stills uses named sequences of files and stiches the result together in a slideshow video.

Since there is not movie to transcode, the Transcoder class needs to be used. The input and input_options are provided through transcoder options.

Specify the path to ffmpeg

By default, the gem assumes that the ffmpeg binary is available in the execution path and named ffmpeg and so will run commands that look something like ffmpeg -i /path/to/input.file .... Use the FFMPEG.ffmpeg_binary setter to specify the full path to the binary if necessary:

This will cause the same command to run as /usr/local/bin/ffmpeg -i /path/to/input.file ... instead.

Automatically kill hung processes

By default, the gem will wait for 30 seconds between IO feedback from the FFMPEG process. After which an error is logged and the process killed. It is possible to modify this behaviour by setting a new default:

Disabling output file validation

By default Transcoder validates the output file, in case you use FFMPEG for HLS format that creates multiple outputs you can disable the validation by passing validate: false to transcoder_options.

Note that transcode will not return the encoded movie object in this case since attempting to open a (possibly) invalid output file might result in an error being raised.

 

Leave a Reply