Deno FFMPEG
FFmpeg is really nice to use and quite handy for alot of applications.
So now we do :D
But we didn't have any easy wrapper for it in Deno!List of features
- Video bitrate(VBR and CBR)
- FFMPEG Filters
- Easy to use
- All methods are chainable
- Frequently updated and maintained by me
Basic example
save()
is used to start the render process. you should always use save()
as last option
import ffmpeg from "./mod.ts";
let videoRender = ffmpeg({ source: './dev/video0', ffmpegDir: './dev/ffmpeg' })
videoRender.videoBitrate(1000).save('./output.mp4');
Documentation
Creating a new instance of ffmpeg
The denoFFMPEG module returns a constructor that you can use to instanciate FFmpeg commands.
import ffmpeg from "./mod.ts";
let render = ffmpeg();
//or use ffmpeg class
import { FfmpegClass } from "./mod.ts";
let render = new FfmpegClass();
You may pass a single input file and configuration object to the constructor.
import ffmpeg from "./mod.ts";
let render = ffmpeg({source:'./path/to/video0', ffmpegDir: './path/to/ffmpeg'})
import ffmpeg from "./mod.ts";
ffmpeg({ ffmpegDir: './path/to/ffmpeg'})
.input('./path/to/video0')
the options are the following:
ffmpegdir
: set the ffmpeg path./relative/path/to/ffmpeg.exe
orC:/full/path/to/ffmpeg.exe
source
: ffmpeg input sourceniceness
: ffmpeg niceness value, between -20 and 20; ignored on Windows platforms (defaults to 0)Specifying inputs
An input can be:
- a file name (eg.
./path/to/file.avi
); - an image pattern (eg.
./path/to/frame%03d.png
); - a link to a video (eg.
http://samples.ffmpeg.org/MPEG-4/video.mp4
); ```js // Note that all denoFFMPEG methods are chainable // currently the last input path will be used ffmpeg() .input('./path/to/input1.avi')
// You can also use links ffmpeg({source:'http://samples.ffmpeg.org/MPEG-4/video.mp4', ffmpegDir: './path/to/ffmpeg'}) .save('./coolvideo.mp4');
// Passing an input to the constructor is the same as calling .input() let render = ffmpeg({source:'./path/to/file.avi'}) // <= this one will be ignored .input('./path/to/input2.avi') // <= this one will also be ignored .input('./path/to/input3.avi'); // <= this is the input
multiple input support will be added later on
## Audio options
The following methods change the audio stream(s) in the produced output.
### noAudio(): disable audio altogether
Disables audio in the output and remove any previously set audio option.
```js
ffmpeg({source:'./path/to/file.avi'}).noAudio();
audioCodec(codec): set audio codec
ffmpeg({source:'./path/to/file.avi'}).audioCodec('libmp3lame');
denoFFMPEG checks for codec availability before actually running the command, and throws an error when a specified audio codec is not available.
audioBitrate(bitrate): set audio bitrate
Sets the audio bitrate in kbps. The bitrate
parameter may be a number or a string with an optional k
suffix.
This method is used to enforce a constant bitrate;
use audioQuality()
to encode using a variable bitrate
ffmpeg({source:'./path/to/file.avi'}).audioBitrate(128);
ffmpeg({source:'./path/to/file.avi'}).audioBitrate('128');
ffmpeg({source:'./path/to/file.avi'}).audioBitrate('128k');
Video options
The following methods change the video stream(s) in the produced output.
noVideo(): disable video altogether
This method disables video output and removes any previously set video option.
ffmpeg({source: './path/to/file.avi'}).noVideo();
videoCodec('codec'): set video codec
ffmpeg({source: './path/to/file.avi'}).videoCodec('libx264');
denoFFMPEG checks for codec availability before actually running the command, and throws an error when a specified video codec is not available.
videoBitrate(bitrate[, constant=true]): set video bitrate
Sets the target video bitrate in kbps. The bitrate
argument may be a number or a string with an optional k
or m
suffix. The constant
argument specifies whether a constant bitrate should be enforced (defaults to true).
ffmpeg({source: './path/to/file.avi'}).videoBitrate(1000);
ffmpeg({source: './path/to/file.avi'}).videoBitrate('1000');
ffmpeg({source: './path/to/file.avi'}).videoBitrate('1000k');
ffmpeg({source: './path/to/file.avi'}).videoBitrate('1000k', false);
videoFilters(filter...): add custom video filters
This method enables adding custom video filters. You may add multiple filters at once by passing either several arguments or an array. See the Ffmpeg documentation for available filters and their syntax.
Each filter pased to this method can only be a filter specification object with the following keys:
filterName
: filter nameoptions
: optional; use object mapping option names to values (eg.{ t: 'in', s: 0, n: 30 }
). Whenoptions
is not specified, the filter will be added without any options.
ffmpeg().videoFilters(
{
filterName: "drawtext",
options: {
fontfile: "/path/to/fontfile.ttf",
fontcolor: "white",
fontsize: 12,
x: 12,
y: 12,
shadowcolor: "black",
shadowx: 12,
shadowy: 12,
text: "sum text"
}
}
)
save('path'): path to output file
path to your output file. eg ./output.mp4
or C:/full/path/to/output.mp4
save('path')
or pipe()
should either be specified to run ffmpeg
ffmpeg().save('./path/to/output.mp4');
on('data')
listener
pipe(): pipe to pipe stream output to on('data')
listener for handling data yourself.
You can also use pipe('await')
to let the data be returned when done rendering
Recommended to always specify the videoCodec with videoCodec('codecname')
ffmpeg().videoCodec('codecname').pipe();
Misc options
on('event', func): event listener
listen to one of the events and do something with it
end
, error
and progress
the first two indicate that ffmpeg has stopped.
Either by being finished or because ffmpeg/denoFFMPEG threw an error
ffmpeg().on('error', errorSTRING => {
console.log(error);
}).save('./savefile.mp4');
ffmpeg().on('end', statusOBJ => {
console.log(JSON.Stringify(statusOBJ));
}).save('./savefile.mp4');
// Object values for
// statusOBJ {
// success: boolean;
// code: number
// }
ffmpeg().on('progress', progressOBJ => {
console.log(JSON.Stringify(progressOBJ));
}).save('./savefile.mp4');
// Object values for
// progressOBJ {
// ETA: Date;
// percentage: number
// }
off('event', func): same as on('event', func) but just once
listen to one of the events and fire once
end
, error
and progress
the first two indicate that ffmpeg has stopped.
Either by being finished or because ffmpeg/denoFFMPEG threw an error
ffmpeg().on('error', errorSTRING => {
console.log(error);
}).save('./savefile.mp4');
ffmpeg().on('end', statusOBJ => {
console.log(JSON.Stringify(statusOBJ));
}).save('./savefile.mp4');
// Object values for
// statusOBJ {
// success: boolean;
// code: number
// }
ffmpeg().on('progress', progressOBJ => {
console.log(JSON.Stringify(progressOBJ));
}).save('./savefile.mp4');
// Object values for
// progressOBJ {
// ETA: Date;
// percentage: number
// }
Changelog
1.2
- added pipe method
- added 'data' eventEmitter for pipe method
- added fatalError in constructor object
- added ffmpeg function as default export. Use
import namehere from "./mod.ts"
for the function andimport {FfmpegClass} from "./mod.ts"
- rewrote constructor into one parameter with an object for everything. Check docs
- changed constructer. Now it is one object like with the following options
ffmpegDir
,niceness
(not used on windows),fatalError
(on by default) andsource
which is the inputfile
You don't need to specify all these, but you can1.1.1
- added url input's
- changed variable sumshit => to progressOBJ
1.1
- added noAudio() method
- added noVideo() method
- added 'progress' eventEmitter
- added audiobitrate() method
- rewrote videoFilters() method for better filtering
- changed addFilters() => videoFilters()
- changed ffmpegWrapper.ts => mod.ts
- changed std@0.79.0/node/events.ts => event@0.2.0
- merged interfaces.ts & mod.ts
- fucked up a bunch
- removed deprecated code
- updated external libs
1.0
- Added buggy ffmpeg wrapper
Authors or Acknowledgments
- Christiaan "MierenMans" van Boheemen - Author
- crowlKats - Made event@0.2.0 chainable for me :)
License
Copyright 2020 Christiaan "MierenMans" van BoheemenPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.