Skip to content

Instantly share code, notes, and snippets.

@FreekingDean
Last active January 10, 2022 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FreekingDean/56fcdb8f91ba0a87cd0dbe00e570322f to your computer and use it in GitHub Desktop.
Save FreekingDean/56fcdb8f91ba0a87cd0dbe00e570322f to your computer and use it in GitHub Desktop.
const fs = require('fs');
const details = () => {
return {
id: "Tdarr_Plugin_bboy_create_reres",
Name: "Create copy of video downsized and recoded",
Type: "Video",
Operation: "Transcode",
Description: `Creates a copy of a video in preperation to downsize
This will check the resolution of the current file, if it is above
the set resolution(s) it will create proper resolution copies in a
nested directory
ex: <original_path>/tdarrversions/<RESOLUTION>/<FILENAME>.`,
Version: "0.1.0",
Stage: 'Pre-processing',
Type: 'Video',
Tags: 'pre-processing',
Inputs: [
{
name: '1080p',
type: 'boolean',
defaultValue: true,
inputUI: {
type: 'dropdown',
options: [
'false',
'true'
]
},
tooltip: 'Enable to ensure a 1080p version exists',
},
{
name: '720p',
type: 'boolean',
defaultValue: true,
inputUI: {
type: 'dropdown',
options: [
'false',
'true'
]
},
tooltip: 'Enable to ensure a 720p version exists',
}
]
}
}
const SUBDIR = "tdarrversions"
const ffmpegOutput = (file, resolution) => {
return `-c:v libx264 -c:a copy -preset veryslow -filter:v scale=-1:${resolution} "${path.join(resDir(file, resolution), file.meta.FileName)}"`
}
const resDir = (file, resolution) => {
return path.join(file.meta.Directory, SUBDIR, resolution)
}
const ensureDir = (dir) => {
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
}
const checkExists = (file, resolution) => {
ensureDir(path.join(file.meta.Directory, SUBDIR))
ensureDir(resDir(file, resolution))
return fs.readdirSync(resDir(file, resolution)).length > 0
}
const plugin = (file, librarySettings, inputs, otherArguments) => {
const lib = require('../methods/lib');
const proc = require("child_process")
//inputs = lib.loadDefaultValues(inputs, details)
const response = {
preset: '-y ,',
processFile: false,
reQueueAfter: false,
ffmpegMode : true,
container : '.mkv',
infoLog: "",
}
const pathParts = file.meta.Directory.split("/")
if (pathParts[pathParts.length - 2] === SUBDIR) {
response.infoLog += "☑ Video already a tdarr version file, not downsizing! \n";
return response
}
const height = file.ffProbeData.streams[0].height
if (inputs['720p'] && height > 720 && !checkExists(file, '720')) {
response.infoLog += "720p version required\n"
response.reQueueAfter = true
response.processFile = true
response.preset += ` ${ffmpegOutput(file, '720')}`
}
if (inputs['1080p'] && height > 1080 && !checkExists(file, '1080')) {
response.infoLog += "1080p version required\n"
response.reQueueAfter = true
response.processFile = true
response.preset += ` ${ffmpegOutput(file, '1080')}`
}
const currentCodec = file.ffProbeData.streams[0].codec_name
if (height <= 1080 && currentCodec != 'h264') {
response.infoLog += "Setting proper file codec! \n";
response.reQueueAfter = true
response.processFile = true
response.preset += ' -c:a copy -c:v libx264 -preset veryslow'
} else if (response.processFile) {
response.preset += ' -codec copy'
} else {
response.infoLog += "☑ File meets conditions! \n";
}
return response
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment