Skip to main content

Function Examples

Here are some examples of functions you can use.

Different outputs based on file size

   if(Variables.file.Size > 10_000_000_000) // 10GB
return 1; // output 1
if(Variables.file.Size > 2_000_000_000) // 2GB
return 2; // output 2

let reducedSize = (Variables.file.Size / Variables.file.Orig.Size) * 100;
if(reducedSize < 10)
return 0; // its too small, something went wrong

return 3; // output 3

Copying files in a Function

// file here is a FileInfo object which makes it easy to get the short filename (file.Name)
for(let file in new System.IO.DirectoryInfo('/mnt/source').GetFiles('*.*'))
{
System.IO.File.Copy(file.FullName, '/mnt/destination/' + file.Name);
}

// alternative, where file is a string of the full filename
for(let file in System.IO.Directory.GetFiles('/mnt/source', '*.*')
{
System.IO.File.Copy(file, '/mnt/destination/' + file.substring(file.lastIndexOf('/') + 1));
}

Note: This uses C# to do the copy and look for files


Disable all but first Audio / Subtitle

This example function will disable all subtitle and all but the very first Audio track

This must be used between a FFmpeg Builder: Start and FFmpeg Builder: Executor

for(let i=1;i<Variables?.FfmpegBuilderModel?.AudioStreams?.length;i++)
{
let as = Variables.FfmpegBuilderModel.AudioStreams[i];
as.Deleted = true;
}
for(let i=0;i<Variables?.FfmpegBuilderModel?.SubtitleStreams?.length;i++)
{
let sub = Variables.FfmpegBuilderModel.SubtitleStreams[i];
sub.Deleted = true;
}

return 1;

Disable all but first English/Japanese Audio

This example function disables all but the first English and Japanese Audio track found

This must be used between a FFmpeg Builder: Start and FFmpeg Builder: Executor

let foundEnglish = false;
let foundJapanese = false;
let first = null;

for(let i=0;i<Variables?.FfmpegBuilderModel?.AudioStreams?.length;i++)
{
let as = Variables.FfmpegBuilderModel.AudioStreams[i];
as.Deleted = true;
if(i === 0)
first = as;
let isEnglish = /^en/i.test('' + as.Language);
let isJapanese = /^(jp|jap)/i.test('' + as.Language);

if(foundEnglish && foundJapanese)
{
// already found japanese and english remove the rest of the audio
continue;
}

if(foundEnglish && isEnglish)
{
// already found english
continue;
}
if(foundJapanese && isJapanese)
{
// already found japanese
continue;
}

// else we want to keep this track, so make sure its not deleted
as.Deleted = false;
if(isEnglish)
foundEnglish = true;
if(isJapanese)
foundJapanese = true;
}

if(foundEnglish === false && foundJapanese === false && first)
{
// enable the first audio for safety
first.Deleted = false;
}

return 1;

Clear Video Title

This example will clear all titles from video streams

This must be used between a FFmpeg Builder: Start and FFmpeg Builder: Executor

for(let i=0;i<Variables?.FfmpegBuilderModel?.VideoStreams?.length;i++)
{
let video = Variables.FfmpegBuilderModel.VideoStreams[i];
video.Title = '';
}
return 1;

Set Audio Stream 0 as default

This example will iterate all audio streams in the FFmpeg Builder and set the first audio stream as the default stream.

This must be used between a FFmpeg Builder: Start and FFmpeg Builder: Executor

let length = Variables?.FfmpegBuilderModel?.AudioStreams?.length;
if(!length)
{
Logger.ILog('No audio stream found"');
return 1;
}

Logger.ILog('Found audio streams: ' + length);

for(let i=0;i<length;i++)
{
let as = Variables.FfmpegBuilderModel.AudioStreams[i];
if(as)
as.IsDefault = i === 0;
}

return 1;