Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update bitrate setup #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 76 additions & 72 deletions lib/video.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
var fs = require('fs')
, path = require('path')
, when = require('when');
var fs = require('fs')
, path = require('path')
, when = require('when');

var errors = require('./errors')
, presets = require('./presets')
, utils = require('./utils');
var errors = require('./errors')
, presets = require('./presets')
, utils = require('./utils');

module.exports = function (filePath, settings, infoConfiguration, infoFile) {

// Public info about file and ffmpeg configuration
this.file_path = filePath;
this.info_configuration = infoConfiguration;
this.metadata = infoFile;
this.file_path = filePath;
this.info_configuration = infoConfiguration;
this.metadata = infoFile;

// Commands for building the ffmpeg string conversion
var commands = new Array()
, inputs = new Array()
, filtersComlpex = new Array()
, output = null;
var commands = new Array()
, inputs = new Array()
, filtersComlpex = new Array()
, output = null;

// List of options generated from setting functions
var options = new Object();
var options = new Object();

/*****************************************/
/* FUNCTION FOR FILL THE COMMANDS OBJECT */
Expand Down Expand Up @@ -122,6 +122,8 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {
* Sets the video bitrate
*/
this.setVideoBitRate = function (bitrate) {
if (!bitrate.includes('M') && !bitrate.includes('m') || !bitrate.includes('K') && !bitrate.includes('k'))
throw new Error('bitrate must be specified with M for MBit/s or with K for kBit/s')
if (options.video == undefined)
options.video = new Object();
// Set the new option
Expand Down Expand Up @@ -267,6 +269,8 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {
* Sets the audio bitrate
*/
this.setAudioBitRate = function (bitrate) {
if (!bitrate.includes('M') && !bitrate.includes('m') || !bitrate.includes('K') && !bitrate.includes('k'))
throw new Error('bitrate must be specified with "M" for MBit/s or with "K" for kBit/s')
if (options.audio == undefined)
options.audio = new Object();
// Set the new option
Expand All @@ -291,11 +295,11 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {
this.setWatermark = function (watermarkPath, settings) {
// Base settings
var baseSettings = {
position : "SW" // Position: NE NC NW SE SC SW C CE CW
, margin_nord : null // Margin nord
, margin_sud : null // Margin sud
, margin_east : null // Margin east
, margin_west : null // Margin west
position: "SW" // Position: NE NC NW SE SC SW C CE CW
, margin_nord: null // Margin nord
, margin_sud: null // Margin sud
, margin_east: null // Margin east
, margin_west: null // Margin west
};

// Check if watermark exists
Expand All @@ -307,7 +311,7 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {
utils.mergeObject(baseSettings, settings);

// Check if position is valid
if (baseSettings.position == null || utils.in_array(baseSettings.position, ['NE','NC','NW','SE','SC','SW','C','CE','CW']) === false)
if (baseSettings.position == null || utils.in_array(baseSettings.position, ['NE', 'NC', 'NW', 'SE', 'SC', 'SW', 'C', 'CE', 'CW']) === false)
throw errors.renderError('invalid_watermark_position', baseSettings.position);

// Check if margins are valid
Expand Down Expand Up @@ -371,7 +375,7 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {
if (options.video == undefined)
options.video = new Object();
// Set the new option
options.video.watermark = { path : watermarkPath, overlay : overlay };
options.video.watermark = { path: watermarkPath, overlay: overlay };
return this;
} else if (arguments[2] != undefined && arguments[2] === true) {
this.addInput(watermarkPath);
Expand All @@ -395,7 +399,7 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {
if (options.video.hasOwnProperty('codec'))
this.addCommand('-vcodec', options.video.codec);
if (options.video.hasOwnProperty('bitrate'))
this.addCommand('-b', parseInt(options.video.bitrate, 10) + 'kb');
this.addCommand('-b:v', parseInt(options.video.bitrate, 10));
if (options.video.hasOwnProperty('framerate'))
this.addCommand('-r', parseInt(options.video.framerate, 10));
if (options.video.hasOwnProperty('startTime'))
Expand Down Expand Up @@ -437,7 +441,7 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {
if (options.audio.hasOwnProperty('quality'))
this.addCommand('-aq', options.audio.quality);
if (options.audio.hasOwnProperty('bitrate'))
this.addCommand('-ab', parseInt(options.audio.bitrate, 10) + 'k');
this.addCommand('-b:a', parseInt(options.audio.bitrate, 10));
}
}

Expand All @@ -454,11 +458,11 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {
* Reset the list of commands
*/
var resetCommands = function (self) {
commands = new Array()
inputs = [self.file_path];
filtersComlpex = new Array();
output = null;
options = new Object();
commands = new Array()
inputs = [self.file_path];
filtersComlpex = new Array();
output = null;
options = new Object();
}

/**
Expand All @@ -483,15 +487,15 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {
}

// Final data
var width = null
, height = null
, aspect = null;
var width = null
, height = null
, aspect = null;

// Regex to check which type of dimension was specified
var fixedWidth = /([0-9]+)x\?/.exec(options.video.size)
, fixedHeight = /\?x([0-9]+)/.exec(options.video.size)
, percentage = /([0-9]{1,2})%/.exec(options.video.size)
, classicSize = /([0-9]+)x([0-9]+)/.exec(options.video.size);
var fixedWidth = /([0-9]+)x\?/.exec(options.video.size)
, fixedHeight = /\?x([0-9]+)/.exec(options.video.size)
, percentage = /([0-9]{1,2})%/.exec(options.video.size)
, classicSize = /([0-9]+)x([0-9]+)/.exec(options.video.size);

if (fixedWidth) {
// Set the width dimension
Expand Down Expand Up @@ -531,29 +535,29 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {

if (keepAspectRatio) {
// Calculate the new aspect ratio
var gcdValue = utils.gcd(width, height);
var gcdValue = utils.gcd(width, height);

aspect = new Object();
aspect.x = width / gcdValue;
aspect.y = height / gcdValue;
aspect.string = aspect.x + ':' + aspect.y;
}

return { width : width, height : height, aspect : aspect };
return { width: width, height: height, aspect: aspect };
}

/**
* Executing the commands list
*/
var execCommand = function (callback, folder) {
var i;
var i;
// Checking if folder is defined
var onlyDestinationFile = folder != undefined ? false : true;
// Building the value for return value. Check if the callback is not a function. In this case will created a new instance of the deferred class
var deferred = typeof callback != 'function' ? when.defer() : { promise : null };
// Deal with input paths that have spaces in them, by quoting them
for (i=0; i<inputs.length; i++) {
inputs[i] = '\'' + inputs[i] + '\'';
var deferred = typeof callback != 'function' ? when.defer() : { promise: null };
// Deal with input paths that have spaces in them, by quoting them
for (i = 0; i < inputs.length; i++) {
inputs[i] = '\'' + inputs[i] + '\'';
}
// Create a copy of the commands list
var finalCommands = ['ffmpeg -i']
Expand All @@ -570,11 +574,11 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {
if (!error) {
// Check if show only destination filename or the complete file list
if (onlyDestinationFile) {
result = finalCommands[finalCommands.length-1];
result = finalCommands[finalCommands.length - 1];
} else {
// Clean possible "/" at the end of the string
if (folder.charAt(folder.length-1) == "/")
folder = folder.substr(0, folder.length-1);
if (folder.charAt(folder.length - 1) == "/")
folder = folder.substr(0, folder.length - 1);
// Read file list inside the folder
result = fs.readdirSync(folder);
// Scan all file and prepend the folder path
Expand Down Expand Up @@ -613,9 +617,9 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {
fs.unlinkSync(destionationFileName);

// Building the final path
var destinationDirName = path.dirname(destionationFileName)
, destinationFileNameWE = path.basename(destionationFileName, path.extname(destionationFileName)) + '.mp3'
, finalPath = path.join(destinationDirName, destinationFileNameWE);
var destinationDirName = path.dirname(destionationFileName)
, destinationFileNameWE = path.basename(destionationFileName, path.extname(destionationFileName)) + '.mp3'
, finalPath = path.join(destinationDirName, destinationFileNameWE);

resetCommands(this);

Expand All @@ -638,23 +642,23 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {
*/
this.fnExtractFrameToJPG = function (/* destinationFolder, settings, callback */) {

var destinationFolder = null
, newSettings = null
, callback = null;
var destinationFolder = null
, newSettings = null
, callback = null;

var settings = {
start_time : null // Start time to recording
, duration_time : null // Duration of recording
, frame_rate : null // Number of the frames to capture in one second
, size : null // Dimension each frame
, number : null // Total frame to capture
, every_n_frames : null // Frame to capture every N frames
, every_n_seconds : null // Frame to capture every N seconds
, every_n_percentage : null // Frame to capture every N percentage range
, keep_pixel_aspect_ratio : true // Mantain the original pixel video aspect ratio
, keep_aspect_ratio : true // Mantain the original aspect ratio
, padding_color : 'black' // Padding color
, file_name : null // File name
start_time: null // Start time to recording
, duration_time: null // Duration of recording
, frame_rate: null // Number of the frames to capture in one second
, size: null // Dimension each frame
, number: null // Total frame to capture
, every_n_frames: null // Frame to capture every N frames
, every_n_seconds: null // Frame to capture every N seconds
, every_n_percentage: null // Frame to capture every N percentage range
, keep_pixel_aspect_ratio: true // Mantain the original pixel video aspect ratio
, keep_aspect_ratio: true // Mantain the original aspect ratio
, padding_color: 'black' // Padding color
, file_name: null // File name
};

// Scan all arguments
Expand Down Expand Up @@ -810,7 +814,7 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {
}

// Add destination file path to the command list
setOutput([destinationFolder,settings.file_name].join('/'));
setOutput([destinationFolder, settings.file_name].join('/'));

// Executing the commands list
return execCommand.call(this, callback, destinationFolder);
Expand All @@ -821,9 +825,9 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {
*/
this.fnAddWatermark = function (watermarkPath /* newFilepath , settings, callback */) {

var newFilepath = null
, newSettings = null
, callback = null;
var newFilepath = null
, newSettings = null
, callback = null;

// Scan all arguments
for (var i = 1; i < arguments.length; i++) {
Expand All @@ -848,15 +852,15 @@ module.exports = function (filePath, settings, infoConfiguration, infoFile) {

if (newFilepath == null)
newFilepath = path.dirname(this.file_path) + '/' +
path.basename(this.file_path, path.extname(this.file_path)) + '_watermark_' +
path.basename(watermarkPath, path.extname(watermarkPath)) +
path.extname(this.file_path);
path.basename(this.file_path, path.extname(this.file_path)) + '_watermark_' +
path.basename(watermarkPath, path.extname(watermarkPath)) +
path.extname(this.file_path);

// Add destination file path to the command list
setOutput(newFilepath);

commands.push('-strict');
commands.push('-2')
commands.push('-strict');
commands.push('-2')

// Executing the commands list
return execCommand.call(this, callback);
Expand Down