npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

stream-transcoder

v0.0.5

Published

FFmpeg based media transcoder that supports streams.

Downloads

127,372

Readme

#stream-transcoder.js FFmpeg based media transcoder that supports streams.

##Introduction Flexible media transcoding using FFmpeg. Stream media in and out - converting it on the fly.

I created this, because the current FFmpeg transcoders for node.js did not properly support streams as I needed.

Along with the stream-body-parser.js, this makes a great companion for doing stuff like this.

var express = require('express'),
    StreamBodyParser = require('stream-body-parser'),
    Transcoder = require('stream-transcoder');

var app = express();

var bodyParser = new StreamBodyParser(app);

bodyParser.process('video/*', function(stream, req, next) {
	
	var myGridFSWriteStream = (Some MongoDB GridFS stream)
	
	new Transcoder(stream)
	    .maxSize(320, 240)
	    .videoCodec('h264')
	    .videoBitrate(800 * 1000)
	    .fps(25)
	    .audioCodec('libfaac')
	    .sampleRate(44100)
	    .channels(2)
	    .audioBitrate(128 * 1000)
	    .format('mp4')
	    .on('finish', function() {
	    	next();
	    })
	    .stream().pipe(myGridFSWriteStream);
	
});

app.post('/', function(req, res) {
	res.send(200); // File uploaded
});

app.listen(3000);

In the above example the video is transcoded as it is being uploaded, and then piped directly into the database. So when the route is being called, the video is transcoded and stored.

Installing FFmpeg

FFmpeg is not installed with this package. So before usage, install FFmpeg using your favorite package manager or download it at ffmpeg.org.

Configure FFmpeg binary path

Set the FFMPEG_BIN_PATH env var. (ffmpeg by default)

Class: Transcoder

Transcoder is an EventEmitter.

This class transcodes from one media format to another. It supports both files and streams as input and/or output. Some formats are not suited for streaming, in which case the Transcoder will emit an error, but most formats are.

new Transcoder(stream)

  • stream Object - A readable stream.

Prepares a new Transcoder with a stream as its input.

new Transcoder(file)

  • file String - The path of the file to be transcoded.

Prepares a new Transcoder with a file as its input.

Event: 'metadata'

  • metadata Object - Metadata of input and output streams.

Emitted when metadata is available for both input and output streams. If no output is specified (by using transcoder.exec()), only input streams will be described.

This an example of a transcoding process metadata.

{
    "input": {
        "streams": [
            {
                "type": "video",
                "codec": "h264",
                "bitrate": 10131000,
                "fps": 25,
                "size": {
                    "width": 1280,
                    "height": 720
                },
                "aspect": 1.7777777777777777,
                "colors": "yuv420p"
            },
            {
                "type": "audio",
                "codec": "aac",
                "samplerate": 44100,
                "channels": 2,
                "bitrate": 106000
            }
        ],
        "duration": 250068,
        "synched": true
    },
    "output": {
        "streams": [
            {
                "type": "video",
                "codec": "h264",
                "bitrate": 800000,
                "size": {
                    "width": 320,
                    "height": 180
                },
                "aspect": 1.7777777777777777,
                "colors": "yuv420p"
            },
            {
                "type": "audio",
                "codec": "aac",
                "samplerate": 44100,
                "channels": 2,
                "bitrate": 128000
            }
        ]
    }
}

Event: 'progress'

  • progress Object - Object describing current progress.

Emitted when progress has been made in the transcoding.

This is an example of the progress object. Where progress.progress is a percentage of the total transcoding job.

{
    "frame": 508,
    "fps": 253,
    "quality": 16,
    "size": 1553408,
    "time": 20041,
    "bitrate": 608000,
    "progress": 0.08014220132124063
}

Event: 'finish'

Emitted when transcoding is complete.

Event: 'error'

  • error Error - The error that occured.

Emitted when FFmpeg exits with an error.

transcoder.videoCodec(codec)

  • codec String - Name of the video codec. As an example h264.

Sets the video codec.

Returns transcoder object.

Notice: Supported video codecs depends on your FFmpeg installation. Running ffmpeg -codecs from your terminal will list the supported codecs.

transcoder.videoBitrate(bitrate)

  • bitrate Number or String - The bitrate of the encoded video. Both 1280000 or 128 kbit can be passed.

Sets the video bitrate.

Returns transcoder object.

transcoder.fps(fps)

  • fps Number - Frames per second.

Sets the number of frames per second.

Returns transcoder object.

transcoder.format(format)

  • format String - Output format.

Sets the output format.

Returns transcoder object.

Notice: Supported formats also depends on you FFmpeg installation. Running ffmpeg -formats from your terminal will list the supported formats.

transcoder.maxSize(width, height)

  • width Number - Maximum width of video.
  • height Number - Miximum height of video.

Sets the output video size, shrinking to fit the size to maintain aspect ratio. The output video will be within the defined size, but with aspect ratio is preserved.

Returns transcoder object.

transcoder.minSize(width, height)

  • width Number - Minimum width of video.
  • height Number - Minimum height of video.

Sets the output video size, scaling it to have a minimum of both directions, while maintaining aspect ratio.

transcoder.size(width, height)

  • width Number - Minimum width of video.
  • height Number - Minimum height of video.

Sets the output video size, not maintaining aspect ratio if it doesn't fit.

Returns transcoder object.

transcoder.passes(passes)

  • passes Number - The number of encoder passes.

Sets the number of encoder passes.

Returns transcoder object.

transcoder.aspectRatio(ratio)

  • ratio Number - The desired aspect ratio. As an example 1.7777777.

Sets the video aspect ratio.

Returns transcoder object.

transcoder.audioCodec(codec)

  • codec String - Name of the audio codec. As an example mp3 or aac.

Sets the audio codec.

Returns transcoder object.

Notice: Supported audio codecs depends on your FFmpeg installation. Running ffmpeg -codecs from your terminal will list the supported codecs.

transcoder.sampleRate(rate)

  • rate Number - Audio sample rate. As an example 44100.

Sets the audio sample rate.

Returns transcoder object.

transcoder.channels(channels)

  • channels Number - Number of audio channels.

Sets the number of audio channels.

Returns transcoder object.

transcoder.audioBitrate(bitrate)

  • bitrate Number - The audio bitrate.

Sets the audio bitrate.

Returns transcoder object.

transcoder.captureFrame(time)

  • time Number - Time of frame in milliseconds.

Capture a single frame at time. Sets up transcoder to jpeg output.

Returns transcoder object.

transcoder.stream()

Returns a writeable stream that will emit the transcoded media data.

transcoder.writeToFile(file)

  • file String - Path of filename.

Writes transcoded media data to file.

Returns transcoder object.

transcoder.exec()

Executes the transcoder without outputting any data. This is useful if you only need metadata for a media file.

Returns child process.

transcoder.custom(key, value)

  • key String - The key for the parameter
  • value String [optional] - The value for the parameter.

Adds a custom parameter to the FFmpeg command line - this is for all your special needs that is currently not implemented as a function in the Transcoder.

As an example:

.custom('ss', '00:30:00')

Translates to on the FFmpeg command line:

ffmpeg -ss 00:30:00