Update Tdarr_Plugin_A47j_FFMPEG_NVENC_HEVC_Video_Only.js (#253)
* Update Tdarr_Plugin_A47j_FFMPEG_NVENC_HEVC_Video_Only.js Updated to make use of mediaInfo file object and dropdowns. All external dependencies removed. * Update Tdarr_Plugin_A47j_FFMPEG_NVENC_HEVC_Video_Only.js Fixed typo * Update Tdarr_Plugin_A47j_FFMPEG_NVENC_HEVC_Video_Only.js another small tweak * Update Tdarr_Plugin_A47j_FFMPEG_NVENC_HEVC_Video_Only.js Fixed id error. * Update Tdarr_Plugin_A47j_FFMPEG_NVENC_HEVC_Video_Only.js Removed eslint disable. * Fix lint errors * Update Tdarr_Plugin_A47j_FFMPEG_NVENC_HEVC_Video_Only.js Updated to make use of mediaInfo file object and dropdowns. All external dependencies removed. * Update Tdarr_Plugin_A47j_FFMPEG_NVENC_HEVC_Video_Only.js Fixed typo * Update Tdarr_Plugin_A47j_FFMPEG_NVENC_HEVC_Video_Only.js another small tweak * Update Tdarr_Plugin_A47j_FFMPEG_NVENC_HEVC_Video_Only.js Fixed id error. * Update Tdarr_Plugin_A47j_FFMPEG_NVENC_HEVC_Video_Only.js Removed eslint disable. * Fix lint errors Co-authored-by: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com>make-only-subtitle-default
parent
d0a5ccf078
commit
84ba82bcf1
@ -1,350 +1,288 @@
|
|||||||
/* eslint-disable */
|
/* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */
|
||||||
const details = () => {
|
/* eslint-disable no-restricted-globals */
|
||||||
return {
|
|
||||||
id: "Tdarr_Plugin_A47j_FFMPEG_NVENC_HEVC_Video_Only",
|
const details = () => ({
|
||||||
Stage: 'Pre-processing',
|
id: 'Tdarr_Plugin_A47j_FFMPEG_NVENC_HEVC_Video_Only',
|
||||||
Name: "FFMPEG nvenc_H265 Video Only",
|
Stage: 'Pre-processing',
|
||||||
Type: "Video",
|
Name: 'FFMPEG nvenc_H265 Video Only',
|
||||||
Stage: "Pre-processing",
|
Type: 'Video',
|
||||||
Operation: "Transcode",
|
Operation: 'Transcode',
|
||||||
Description: `[Contains built-in filter] This plugin transcodes non-h265 files into h265 mkv using NVENC,
|
Description: `
|
||||||
reducing resolution to 1920x1080 using nvenc. Audio/subtitles not affected. Bitrate is scaled based on input file quality.
|
[Contains built-in filter] This plugin transcodes non-h265 files into h265 mkv using NVENC,
|
||||||
== This plugin depends on mediainfo and mkvpropedit, which must be installed manually!
|
reducing resolution to 1920x1080 using nvenc. Audio/subtitles not affected. Bitrate is scaled based on
|
||||||
Check this gist for details: https://gist.github.com/jeff47/4ec428e329a485a102bab0398e6ac4be == `,
|
input file quality.`,
|
||||||
Version: "1.00",
|
Version: '1.1',
|
||||||
Tags: "pre-processing,video only,ffmpeg,nvenc h265,h265",
|
Tags: 'pre-processing,video only,ffmpeg,nvenc h265,h265',
|
||||||
|
|
||||||
Inputs: [
|
Inputs: [
|
||||||
{
|
{
|
||||||
name: "compressionFactor",
|
name: 'compressionFactor',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
defaultValue:'0.07',
|
defaultValue: '0.07',
|
||||||
inputUI: {
|
inputUI: {
|
||||||
type: 'text',
|
type: 'text',
|
||||||
},
|
},
|
||||||
tooltip: `== Compression Factor == \\n\\n
|
tooltip: `
|
||||||
How much does HEVC compress raw video? I suggest something between 0.04-0.08. Remember that GPU encoding is not as
|
== Compression Factor == \\n\\n
|
||||||
efficient as CPU encoding, so resulting file sizes will be larger.\\n\\n
|
How much does HEVC compress raw video? I suggest something between 0.04-0.08. Remember that GPU encoding is
|
||||||
0.07 will result in a 1080p@29.92fps having a target bitrate of 5.4mbps. This is the default.\\n`
|
faster but not as space efficient as CPU encoding, so resulting file sizes will be larger than if you used CPU
|
||||||
|
encoding.\\n\\n
|
||||||
|
0.07 will result in a 1080p@29.92fps having a target bitrate of 5.4mbps. This is the default.\\n`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'maxResolution',
|
||||||
|
type: 'string',
|
||||||
|
defaultValue: 'false',
|
||||||
|
inputUI: {
|
||||||
|
type: 'dropdown',
|
||||||
|
options: [
|
||||||
|
'false',
|
||||||
|
'8KUHD',
|
||||||
|
'4KUHD',
|
||||||
|
'1080p',
|
||||||
|
'720p',
|
||||||
|
'576p',
|
||||||
|
'480p',
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
tooltip: `== Maximum Resolution ==\\n\\n
|
||||||
name: "maxResolution",
|
|
||||||
type: 'string',
|
|
||||||
defaultValue:'false',
|
|
||||||
inputUI: {
|
|
||||||
type: 'text',
|
|
||||||
},
|
|
||||||
tooltip: `== Maximum Resolution ==\\n\\n
|
|
||||||
Videos that exceed this resolution will be resized down to this resolution.\\n
|
Videos that exceed this resolution will be resized down to this resolution.\\n
|
||||||
Accepted options: 480p, 576p, 720p, 1080p, 4KUHD, 8KUHD. If false, no resizing will occur.\\n`
|
If false, no resizing will occur.\\n`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'ffmpegPreset',
|
||||||
|
type: 'string',
|
||||||
|
defaultValue: 'medium',
|
||||||
|
inputUI: {
|
||||||
|
type: 'dropdown',
|
||||||
|
options: [
|
||||||
|
'veryslow',
|
||||||
|
'slower',
|
||||||
|
'slow',
|
||||||
|
'medium',
|
||||||
|
'fast',
|
||||||
|
'faster',
|
||||||
|
'veryfast',
|
||||||
|
'superfast',
|
||||||
|
'ultrafast',
|
||||||
|
],
|
||||||
},
|
},
|
||||||
],
|
tooltip: `== FFmpeg Preset ==\\n\\n
|
||||||
|
Select the ffmpeg preset.\\n`,
|
||||||
|
},
|
||||||
};
|
{
|
||||||
}
|
name: 'container',
|
||||||
|
type: 'string',
|
||||||
|
defaultValue: 'mp4',
|
||||||
var MediaInfo = {
|
inputUI: {
|
||||||
videoHeight: "",
|
type: 'dropdown',
|
||||||
videoWidth: "",
|
options: [
|
||||||
videoFPS:"",
|
'mp4',
|
||||||
videoBR: "",
|
'mkv',
|
||||||
videoBitDepth: "",
|
],
|
||||||
overallBR: "",
|
},
|
||||||
JSRProcessed: false,
|
tooltip: `== Container ==\\n\\n
|
||||||
JSRVersion: 0,
|
mkv or mp4.\\n`,
|
||||||
JSRProcessedTime: 0,
|
},
|
||||||
|
],
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
const MediaInfo = {
|
||||||
|
videoHeight: '',
|
||||||
|
videoWidth: '',
|
||||||
|
videoFPS: '',
|
||||||
|
videoBR: '',
|
||||||
|
videoBitDepth: '',
|
||||||
|
overallBR: '',
|
||||||
}; // var MediaInfo
|
}; // var MediaInfo
|
||||||
|
|
||||||
// Easier for our functions if response has global scope.
|
// Easier for our functions if response has global scope.
|
||||||
var response = {
|
const response = {
|
||||||
processFile: false,
|
processFile: false,
|
||||||
preset: "",
|
preset: '',
|
||||||
container: ".mkv",
|
container: '.mp4',
|
||||||
handBrakeMode: false,
|
handBrakeMode: false,
|
||||||
FFmpegMode: true,
|
FFmpegMode: true,
|
||||||
reQueueAfter: true,
|
reQueueAfter: true,
|
||||||
infoLog: "",
|
infoLog: '',
|
||||||
}; // var response
|
}; // var response
|
||||||
|
|
||||||
// Runs mkvpropedit --add-track-statistics on the file.
|
// Finds the first video stream and populates some useful variables
|
||||||
function updateTrackStats(file) {
|
|
||||||
response.infoLog += `☑Running mkvpropedit.\n`;
|
|
||||||
try {
|
|
||||||
const proc = require("child_process");
|
|
||||||
proc.execFile('mkvpropedit', [ '--delete-track-statistics-tags', file._id], (error,stdout,stderr) => {
|
|
||||||
if (error) throw `mkvpropedit failed: ${error}\n`;
|
|
||||||
});
|
|
||||||
proc.execFile('mkvpropedit', [ '--add-track-statistics-tags', file._id], (error,stdout,stderr) => {
|
|
||||||
if (error) throw `mkvpropedit failed: ${error}\n`;
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
response.infoLog += `mkvpropedit failed: ${err}.\n`;
|
|
||||||
throw `mkvpropedit failed: ${err}.\n`;
|
|
||||||
}; // end try/catch
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
} // end updateTrackStats()
|
|
||||||
|
|
||||||
// Runs mediainfo on the file, gets JSON output, finds the first video stream and returns the video bit rate and bit depth.
|
|
||||||
function getMediaInfo(file) {
|
function getMediaInfo(file) {
|
||||||
var objMedInfo = "";
|
let videoIdx = -1;
|
||||||
|
|
||||||
response.infoLog += `☑Running mediainfo.\n`;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const proc = require('child_process')
|
|
||||||
objMedInfo = JSON.parse(proc.execFileSync('mediainfo', [file._id,'--output=JSON']));
|
|
||||||
} catch (err) {
|
|
||||||
response.infoLog += `Mediainfo failed: ${err}.\n`;
|
|
||||||
throw `Mediainfo failed: ${err}.\n`;
|
|
||||||
}; // end try/catch
|
|
||||||
|
|
||||||
var videoIdx = -1;
|
|
||||||
var videoInxFirst = -1;
|
|
||||||
|
|
||||||
for (var i = 0; i < file.ffProbeData.streams.length; i++) {
|
for (let i = 0; i < file.ffProbeData.streams.length; i += 1) {
|
||||||
|
const strstreamType = file.ffProbeData.streams[i].codec_type.toLowerCase();
|
||||||
|
|
||||||
strstreamType = file.ffProbeData.streams[i].codec_type.toLowerCase();
|
// Looking For Video
|
||||||
|
// Check if stream is a video.
|
||||||
|
if (videoIdx === -1 && strstreamType === 'video') {
|
||||||
|
videoIdx = i;
|
||||||
|
|
||||||
//Looking For Video
|
MediaInfo.videoHeight = Number(file.ffProbeData.streams[i].height);
|
||||||
// Check if stream is a video.
|
MediaInfo.videoWidth = Number(file.ffProbeData.streams[i].width);
|
||||||
if (videoIdx == -1 && strstreamType == "video") {
|
MediaInfo.videoFPS = Number(file.mediaInfo.track[i + 1].FrameRate);
|
||||||
videoIdx = i;
|
MediaInfo.videoBR = Number(file.mediaInfo.track[i + 1].BitRate);
|
||||||
videoInxFirst = i;
|
MediaInfo.videoBitDepth = Number(file.mediaInfo.track[i + 1].BitDepth);
|
||||||
|
}
|
||||||
MediaInfo.videoHeight = Number(file.ffProbeData.streams[i].height);
|
|
||||||
MediaInfo.videoWidth = Number(file.ffProbeData.streams[i].width);
|
|
||||||
MediaInfo.videoFPS = Number(objMedInfo.media.track[i + 1].FrameRate);
|
|
||||||
MediaInfo.videoBR = Number(objMedInfo.media.track[i + 1].BitRate);
|
|
||||||
MediaInfo.videoBitDepth = Number(objMedInfo.media.track[i + 1].BitDepth);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
MediaInfo.overallBR = objMedInfo.media.track[0].OverallBitRate;
|
MediaInfo.overallBR = file.mediaInfo.track[0].OverallBitRate;
|
||||||
|
|
||||||
try {
|
|
||||||
MediaInfo.JSRVersion = Number(objMedInfo.media.track[0].extra.JSRVERSION);
|
|
||||||
} catch (err) {
|
|
||||||
MediaInfo.JSRVersion = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
MediaInfo.JSRProcessed = Boolean(objMedInfo.media.track[0].extra.JSRPROCESSED);
|
|
||||||
} catch (err) {
|
|
||||||
MediaInfo.JSRProcessed = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
MediaInfo.JSRProcessedTime = Number(objMedInfo.media.track[0].extra.JSRPROCESSEDTIME);
|
|
||||||
} catch (err) {
|
|
||||||
MediaInfo.JSRProcessedTime = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
} // end getMediaInfo()
|
} // end getMediaInfo()
|
||||||
|
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
const plugin = (file, librarySettings, inputs, otherArguments) => {
|
const plugin = (file, librarySettings, inputs, otherArguments) => {
|
||||||
|
const lib = require('../methods/lib')();
|
||||||
const lib = require('../methods/lib')();
|
|
||||||
// eslint-disable-next-line no-unused-vars,no-param-reassign
|
// eslint-disable-next-line no-unused-vars,no-param-reassign
|
||||||
inputs = lib.loadDefaultValues(inputs, details);
|
inputs = lib.loadDefaultValues(inputs, details);
|
||||||
|
|
||||||
if (file.fileMedium !== "video") {
|
if (file.fileMedium !== 'video') {
|
||||||
response.processFile = false;
|
response.processFile = false;
|
||||||
response.infoLog += "☒File is not a video.\n";
|
response.infoLog += '☒File is not a video.\n';
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
// How much does HVEC compress the raw stream?
|
// How much does HVEC compress the raw stream?
|
||||||
var compressionFactor = 0.07;
|
let compressionFactor = 0.07;
|
||||||
if ( ! isNaN(Number(inputs.compressionFactor)) ) {
|
if (!isNaN(Number(inputs.compressionFactor))) {
|
||||||
compressionFactor = inputs.compressionFactor;
|
compressionFactor = inputs.compressionFactor;
|
||||||
} else {
|
} else {
|
||||||
response.infoLog += `No compression factor selected, defaulting to ${compressionFactor}.\n`;
|
response.infoLog += `No compression factor selected, defaulting to ${compressionFactor}.\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
response.container = `.${inputs.container}`;
|
||||||
|
|
||||||
// Do we resize?
|
// Do we resize?
|
||||||
var resolutionOrder = [ "480p", "576p", "720p", "1080p", "4KUHD", "8KUHD" ];
|
const resolutionOrder = ['480p', '576p', '720p', '1080p', '4KUHD', '8KUHD'];
|
||||||
|
|
||||||
|
|
||||||
// Define the dimensions and the number of pixels (weightxheight) for each resolution.
|
// Define the dimensions and the number of pixels (weightxheight) for each resolution.
|
||||||
var resolutions = {
|
const resolutions = {
|
||||||
"480p": { "dimensions": "640x480", "pixelCount": 307200 },
|
'480p': { dimensions: '640x480', pixelCount: 307200 },
|
||||||
"576p": { "dimensions": "720x576", "pixelCount": 414720 },
|
'576p': { dimensions: '720x576', pixelCount: 414720 },
|
||||||
"720p": { "dimensions": "1280x720", "pixelCount": 921600 },
|
'720p': { dimensions: '1280x720', pixelCount: 921600 },
|
||||||
"1080p": { "dimensions": "1920x1080", "pixelCount": 2073600 },
|
'1080p': { dimensions: '1920x1080', pixelCount: 2073600 },
|
||||||
"4KUHD": { "dimensions": "3840x2160", "pixelCount": 8294400 },
|
'4KUHD': { dimensions: '3840x2160', pixelCount: 8294400 },
|
||||||
"8KUHD": { "dimensions": "7680x4320", "pixelCount": 33177600 }
|
'8KUHD': { dimensions: '7680x4320', pixelCount: 33177600 },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let maxResolution = '8KUHD';
|
||||||
var maxResolution = "8KUHD";
|
if (resolutionOrder.indexOf(inputs.maxResolution) > 0) {
|
||||||
if ( resolutionOrder.indexOf(inputs.maxResolution) > 0 ) {
|
|
||||||
maxResolution = inputs.maxResolution;
|
maxResolution = inputs.maxResolution;
|
||||||
} else {
|
} else {
|
||||||
response.infoLog += `No valid resolution selected, defaulting to ${maxResolution}.\n`;
|
response.infoLog += `No valid resolution selected, defaulting to ${maxResolution}.\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getMediaInfo(file);
|
||||||
// -------------------------------- METADATA UPDATES --------------------------------
|
|
||||||
// If there is no _STATISTICS_WRITING_DATE_UTC-eng field, then we need to run mkvpropedit and
|
|
||||||
// rerun mediainfo to load the stats.
|
|
||||||
if (file.ffProbeData.streams[0].tags === undefined || file.ffProbeData.streams[0].tags["_STATISTICS_WRITING_DATE_UTC-eng"] === undefined ) {
|
|
||||||
response.infoLog += "☑Track statistics are missing.\n";
|
|
||||||
updateTrackStats(file);
|
|
||||||
getMediaInfo(file);
|
|
||||||
} else {
|
|
||||||
// mkvpropedit records the time the stats were written. Get it (specify it is in UTC) and add a 10 second buffer.
|
|
||||||
StatsWritingTime = Date.parse(`${file.ffProbeData.streams[0].tags["_STATISTICS_WRITING_DATE_UTC-eng"]} UTC`) + 10000;
|
|
||||||
|
|
||||||
// If the file's mtime is more than 60 seconds later than StatsWritingTime, then we should rerun mkvpropedit!
|
|
||||||
if ( file.statSync.mtimeMs > StatsWritingTime ) {
|
|
||||||
response.infoLog += "☑Track statistics are out of date.\n";
|
|
||||||
updateTrackStats(file);
|
|
||||||
getMediaInfo(file);
|
|
||||||
} else {
|
|
||||||
response.infoLog += "☑Track statistics are up to date.\n";
|
|
||||||
getMediaInfo(file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( isNaN(MediaInfo.videoBR) || isNaN(MediaInfo.videoBitDepth) ) {
|
|
||||||
response.infoLog += "videoBR or videoBitDepth was NaN, something went wrong with mediainfo.\n";
|
|
||||||
updateTrackStats(file);
|
|
||||||
getMediaInfo(file);
|
|
||||||
if ( isNaN(MediaInfo.videoBR) || isNaN(MediaInfo.videoBitDepth) ) {
|
|
||||||
response.infoLog += "videoBR or videoBitDepth still NaN, using default.\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the overall bitrate is less than the videoBR, then something is wacky.
|
|
||||||
if ( MediaInfo.videoBR > MediaInfo.overallBR ) {
|
|
||||||
response.infoLog += `videoBR (${MediaInfo.videoBR} was greater than overallBR (${MediaInfo.overallBR}),
|
|
||||||
which is impossible. Updating stats.\n`;
|
|
||||||
updateTrackStats(file);
|
|
||||||
getMediaInfo(file);
|
|
||||||
if ( MediaInfo.videoBR > MediaInfo.overallBR ) {
|
|
||||||
response.infoLog += `videoBR and overallBR still inconsistent, using default.\n`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if ( file.forceProcessing !== true ) {
|
|
||||||
if ( MediaInfo.JSRProcessed !== undefined && MediaInfo.JSRProcessed == true) {
|
|
||||||
response.infoLog += `JSRPROCESSED metadata tag was true. This file was already transcoded by this plugin. Exiting...\n`;
|
|
||||||
response.processFile = false;
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set decoding options here
|
// Set decoding options here
|
||||||
switch (file.ffProbeData.streams[0].codec_name) {
|
switch (file.ffProbeData.streams[0].codec_name) {
|
||||||
case "hevc":
|
case 'hevc':
|
||||||
response.preset = `-vsync 0 -hwaccel cuda -hwaccel_output_format cuda -c:v hevc_cuvid `;
|
response.preset = '-vsync 0 -hwaccel cuda -hwaccel_output_format cuda -c:v hevc_cuvid ';
|
||||||
break;
|
break;
|
||||||
case "h264":
|
case 'h264':
|
||||||
response.preset = `-vsync 0 -hwaccel cuda -hwaccel_output_format cuda -c:v h264_cuvid `;
|
response.preset = '-vsync 0 -hwaccel cuda -hwaccel_output_format cuda -c:v h264_cuvid ';
|
||||||
break;
|
break;
|
||||||
case "vc1":
|
case 'vc1':
|
||||||
response.preset = `-vsync 0 -hwaccel cuda -hwaccel_output_format cuda -c:v vc1_cuvid `;
|
response.preset = '-vsync 0 -hwaccel cuda -hwaccel_output_format cuda -c:v vc1_cuvid ';
|
||||||
break;
|
break;
|
||||||
case "vp8":
|
case 'vp8':
|
||||||
response.preset = `-vsync 0 -hwaccel cuda -hwaccel_output_format cuda -c:v vp8_cuvid `;
|
response.preset = '-vsync 0 -hwaccel cuda -hwaccel_output_format cuda -c:v vp8_cuvid ';
|
||||||
break;
|
break;
|
||||||
case "vp9":
|
case 'vp9':
|
||||||
response.preset = `-vsync 0 -hwaccel cuda -hwaccel_output_format cuda -c:v vp9_cuvid `;
|
response.preset = '-vsync 0 -hwaccel cuda -hwaccel_output_format cuda -c:v vp9_cuvid ';
|
||||||
break;
|
break;
|
||||||
} //end switch(codec)
|
default:
|
||||||
|
break;
|
||||||
// Resize high resolution videos to 1080p.
|
} // end switch(codec)
|
||||||
if ( resolutionOrder.indexOf(file.video_resolution) > resolutionOrder.indexOf(maxResolution) ) {
|
|
||||||
// File resolution exceeds limit, need to resize.
|
let targetBitrate;
|
||||||
response.preset += ` -resize ${resolutions[maxResolution].dimensions} `;
|
|
||||||
response.infoLog += `Resizing to ${resolutions[maxResolution].dimensions}.\n`;
|
// Resize high resolution videos to 1080p.
|
||||||
response.processFile = true;
|
if (resolutionOrder.indexOf(file.video_resolution) > resolutionOrder.indexOf(maxResolution)) {
|
||||||
var targetBitrate = Math.round((resolutions[maxResolution].pixelCount*MediaInfo.videoFPS)*compressionFactor);
|
// File resolution exceeds limit, need to resize.
|
||||||
} else {
|
response.preset += ` -resize ${resolutions[maxResolution].dimensions} `;
|
||||||
// No resize needed.
|
response.infoLog += `Resizing to ${resolutions[maxResolution].dimensions}.\n`;
|
||||||
var targetBitrate = Math.round((MediaInfo.videoWidth*MediaInfo.videoHeight*MediaInfo.videoFPS)*compressionFactor);
|
response.processFile = true;
|
||||||
}
|
targetBitrate = Math.round((resolutions[maxResolution].pixelCount * MediaInfo.videoFPS) * compressionFactor);
|
||||||
|
} else {
|
||||||
|
// No resize needed.
|
||||||
|
targetBitrate = Math.round((MediaInfo.videoWidth * MediaInfo.videoHeight * MediaInfo.videoFPS) * compressionFactor);
|
||||||
|
}
|
||||||
|
|
||||||
// Calculate bitrates
|
// Calculate bitrates
|
||||||
response.infoLog += `Video details: ${file.ffProbeData.streams[0].codec_name}-${file.video_resolution}
|
response.infoLog += `Video details: ${file.ffProbeData.streams[0].codec_name}-${file.video_resolution}
|
||||||
${MediaInfo.videoWidth}x${MediaInfo.videoHeight}x${MediaInfo.videoFPS}@8 bits.\n`;
|
${MediaInfo.videoWidth}x${MediaInfo.videoHeight}x${MediaInfo.videoFPS}@8 bits.\n`;
|
||||||
|
|
||||||
var maxBitrate = Math.round(targetBitrate*1.3);
|
const maxBitrate = Math.round(targetBitrate * 1.3);
|
||||||
var minBitrate = Math.round(targetBitrate*0.7);
|
const minBitrate = Math.round(targetBitrate * 0.7);
|
||||||
if ( isNaN(MediaInfo.videoBR) ) {
|
|
||||||
var bufsize = targetBitrate;
|
|
||||||
} else {
|
|
||||||
var bufsize = Math.round(MediaInfo.videoBR);
|
|
||||||
}
|
|
||||||
|
|
||||||
response.preset += `,-map 0:v -map 0:a -map 0:s? -map -:d? -c copy -c:v:0 hevc_nvenc -rc:v vbr_hq -preset medium -profile:v main10 -rc-lookahead 32 -spatial_aq:v 1 -aq-strength:v 8 -max_muxing_queue_size 4096 `;
|
let bufsize;
|
||||||
response.infoLog += `Video bitrate is ${Math.round(MediaInfo.videoBR/1000)}Kbps, overall is ${Math.round(MediaInfo.overallBR/1000)}Kbps. `;
|
if (isNaN(MediaInfo.videoBR)) {
|
||||||
response.infoLog += `Calculated target is ${Math.round(targetBitrate/1000)}Kbps.\n`;
|
bufsize = targetBitrate;
|
||||||
|
} else {
|
||||||
|
bufsize = Math.round(MediaInfo.videoBR);
|
||||||
|
}
|
||||||
|
|
||||||
|
response.preset += ',-map 0:v -map 0:a -map 0:s? -map -:d? -c copy -c:v:0 hevc_nvenc'
|
||||||
|
+ ` -rc:v vbr_hq -preset ${inputs.ffmpegPreset} -profile:v main10 -rc-lookahead 32 `
|
||||||
|
+ '-spatial_aq:v 1 -aq-strength:v 8 -max_muxing_queue_size 4096 ';
|
||||||
|
response.infoLog += `Video bitrate is ${Math.round(MediaInfo.videoBR / 1000)}Kbps,`
|
||||||
|
+ ` overall is ${Math.round(MediaInfo.overallBR / 1000)}Kbps. `;
|
||||||
|
response.infoLog += `Calculated target is ${Math.round(targetBitrate / 1000)}Kbps.\n`;
|
||||||
|
|
||||||
// Adjust target bitrates by codec and bitrate
|
// Adjust target bitrates by codec and bitrate
|
||||||
switch (file.ffProbeData.streams[0].codec_name) {
|
switch (file.ffProbeData.streams[0].codec_name) {
|
||||||
case "hevc":
|
case 'hevc':
|
||||||
if ( isNaN(MediaInfo.videoBR) ) {
|
if (isNaN(MediaInfo.videoBR)) {
|
||||||
response.processFile = true;
|
response.processFile = true;
|
||||||
targetBitrate = Math.min(MediaInfo.overallBR, targetBitrate);
|
targetBitrate = Math.min(MediaInfo.overallBR, targetBitrate);
|
||||||
response.preset +=` -b:v ${targetBitrate} -maxrate ${maxBitrate} -minrate ${minBitrate} -bufsize ${bufsize} `;
|
response.preset += ` -b:v ${targetBitrate} -maxrate ${maxBitrate} -minrate ${minBitrate} -bufsize ${bufsize} `;
|
||||||
response.infoLog += `☒HEVC Bitrate for ${file.video_resolution} could not be determined,
|
response.infoLog += `☒HEVC Bitrate for ${file.video_resolution} could not be determined,
|
||||||
using sensible default of ${Math.round(targetBitrate/1000)}Kbps.\n`;
|
using sensible default of ${Math.round(targetBitrate / 1000)}Kbps.\n`;
|
||||||
} else if ( (MediaInfo.videoBR > targetBitrate*1.5) || file.forceProcessing === true ) {
|
} else if ((MediaInfo.videoBR > targetBitrate * 1.5) || file.forceProcessing === true) {
|
||||||
response.processFile = true;
|
response.processFile = true;
|
||||||
response.preset +=` -b:v ${targetBitrate} -maxrate ${maxBitrate} -minrate ${minBitrate} -bufsize ${bufsize} `;
|
response.preset += ` -b:v ${targetBitrate} -maxrate ${maxBitrate} -minrate ${minBitrate} -bufsize ${bufsize} `;
|
||||||
response.infoLog += `☒HEVC Bitrate for ${file.video_resolution} exceeds ${Math.round(targetBitrate*1.5/1000)}Kbps,
|
response.infoLog += `☒HEVC Bitrate for ${file.video_resolution}`
|
||||||
downsampling to ${Math.round(targetBitrate/1000)}Kbps.\n`;
|
+ ` exceeds ${Math.round((targetBitrate * 1.5) / 1000)}Kbps,`
|
||||||
|
+ ` downsampling to ${Math.round(targetBitrate / 1000)}Kbps.\n`;
|
||||||
} else {
|
} else {
|
||||||
response.infoLog += `☑HEVC Bitrate is within limits.\n`
|
response.infoLog += '☑HEVC Bitrate is within limits.\n';
|
||||||
}
|
}
|
||||||
break; // case "hevc"
|
break; // case "hevc"
|
||||||
case "h264":
|
case 'h264':
|
||||||
response.processFile = true;
|
response.processFile = true;
|
||||||
|
let new_bitrate;
|
||||||
// We want the new bitrate to be 70% the h264 bitrate, but not higher than our target.
|
// We want the new bitrate to be 70% the h264 bitrate, but not higher than our target.
|
||||||
if ( isNaN(MediaInfo.videoBR) ) {
|
if (isNaN(MediaInfo.videoBR)) {
|
||||||
new_bitrate = Math.min(MediaInfo.overallBR*0.7,targetBitrate);
|
new_bitrate = Math.min(MediaInfo.overallBR * 0.7, targetBitrate);
|
||||||
} else {
|
} else {
|
||||||
new_bitrate = Math.min(Math.round(MediaInfo.videoBR*0.7),targetBitrate);
|
new_bitrate = Math.min(Math.round(MediaInfo.videoBR * 0.7), targetBitrate);
|
||||||
// New bitrate should not be lower than our 60% of our target.
|
// New bitrate should not be lower than our 60% of our target.
|
||||||
new_bitrate = Math.max( new_bitrate, Math.min(MediaInfo.videoBR, targetBitrate*0.6) );
|
new_bitrate = Math.max(new_bitrate, Math.min(MediaInfo.videoBR, targetBitrate * 0.6));
|
||||||
}
|
}
|
||||||
response.preset +=` -b:v ${new_bitrate} -maxrate ${Math.round(new_bitrate*1.3)} -minrate ${Math.round(new_bitrate*0.7)} -bufsize ${bufsize}`;
|
response.preset += ` -b:v ${new_bitrate} -maxrate ${Math.round(new_bitrate * 1.3)}`
|
||||||
response.infoLog += `☒H264 Resolution is ${file.video_resolution}, bitrate was ${Math.round(MediaInfo.videoBR/1000)}Kbps.
|
+ `-minrate ${Math.round(new_bitrate * 0.7)} -bufsize ${bufsize}`;
|
||||||
HEVC target bitrate will be ${Math.round(new_bitrate/1000)}Kbps.\n`;
|
response.infoLog += `☒H264 Resolution is ${file.video_resolution},`
|
||||||
break; // case "h264"
|
+ ` bitrate was ${Math.round(MediaInfo.videoBR / 1000)}Kbps.`
|
||||||
|
+ ` HEVC target bitrate will be ${Math.round(new_bitrate / 1000)}Kbps.\n`;
|
||||||
|
break; // case "h264"
|
||||||
default:
|
default:
|
||||||
response.processFile = true;
|
response.processFile = true;
|
||||||
response.preset +=` -b:v ${targetBitrate} -maxrate ${maxBitrate} -minrate ${minBitrate} -bufsize ${bufsize} `;
|
response.preset += ` -b:v ${targetBitrate} -maxrate ${maxBitrate} -minrate ${minBitrate} -bufsize ${bufsize} `;
|
||||||
response.infoLog += `☒${file.ffProbeData.streams[0].codec_name} resolution is ${file.video_resolution},
|
response.infoLog += `☒${file.ffProbeData.streams[0].codec_name} resolution is ${file.video_resolution},`
|
||||||
bitrate was ${Math.round(MediaInfo.videoBR/1000)}Kbps. HEVC target bitrate will be ${Math.round(targetBitrate/1000)}Kbps.\n`;
|
+ ` bitrate was ${Math.round(MediaInfo.videoBR / 1000)}Kbps.`
|
||||||
break; // default
|
+ ` HEVC target bitrate will be ${Math.round(targetBitrate / 1000)}Kbps.\n`;
|
||||||
|
break; // default
|
||||||
} // switch (file.ffProbeData.streams[0].codec_name)
|
} // switch (file.ffProbeData.streams[0].codec_name)
|
||||||
|
|
||||||
|
if (response.processFile === true) {
|
||||||
|
response.preset += ' -map_metadata:g -1';
|
||||||
if (response.processFile == true) {
|
|
||||||
response.preset += ` -map_metadata:g -1 -metadata JSRVERSION=1 -metadata JSRPROCESSED=true -metadata JSRPROCESSEDTIME=${Date.now()} `;
|
|
||||||
response.FFmpegMode = true;
|
response.FFmpegMode = true;
|
||||||
response.infoLog += `☒Transcoding to HEVC.`;
|
response.infoLog += '☒Transcoding to HEVC.';
|
||||||
} else {
|
|
||||||
if (file.container != "mkv") {
|
|
||||||
response_preset = ',-c copy -map 0';
|
|
||||||
response.processFile = true;
|
|
||||||
response.infoLog += `☒Remuxing to mkv.`;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
return response;
|
}; // end plugin()
|
||||||
} // end plugin()
|
|
||||||
|
|
||||||
module.exports.details = details;
|
module.exports.details = details;
|
||||||
module.exports.plugin = plugin;
|
module.exports.plugin = plugin;
|
||||||
|
|||||||
Loading…
Reference in new issue