Skip to content

Commit

Permalink
Merge pull request #82 from Marnalas/flowPluginSetDefaultAudioStream
Browse files Browse the repository at this point in the history
Implemented non processing when nothing to do
  • Loading branch information
Marnalas authored Jan 10, 2025
2 parents 12d1698 + 3ae3cd2 commit afde558
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ var getFFMPEGDisposition = function (isDefault, dispositions) {
.join('+')
|| '0';
};
var getIsDescriptiveAudioStream = function (stream) {
var _a;
return Boolean(stream.disposition
&& (stream.disposition.comment
|| stream.disposition.descriptions
|| stream.disposition.visual_impaired
|| /\b(commentary|description|descriptive)\b/gi.test(((_a = stream.tags) === null || _a === void 0 ? void 0 : _a.title) || '')));
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
var plugin = function (args) {
var _a, _b, _c, _d;
Expand All @@ -112,6 +120,7 @@ var plugin = function (args) {
(0, flowUtils_1.checkFfmpegCommandInit)(args);
// const streams: IffmpegCommandStream[] = JSON.parse(JSON.stringify(args.variables.ffmpegCommand.streams));
var streams = args.variables.ffmpegCommand.streams;
var shouldProcess = false;
var defaultSet = false;
// Sets the language code used to determine the default audio stream
var languageCode = args.inputs.language;
Expand All @@ -127,22 +136,28 @@ var plugin = function (args) {
args.jobLog("Channels ".concat(channels, " determined has being the highest match"));
}
streams.forEach(function (stream, index) {
var _a, _b, _c;
var _a, _b, _c, _d, _e;
if (stream.codec_type === 'audio') {
var dispositions = stream.disposition;
var isDescriptiveAudioStream = getIsDescriptiveAudioStream(stream);
if (((_b = (_a = stream.tags) === null || _a === void 0 ? void 0 : _a.language) !== null && _b !== void 0 ? _b : '') === languageCode
&& ((_c = stream.channels) !== null && _c !== void 0 ? _c : 0) === channels
&& ((_d = dispositions === null || dispositions === void 0 ? void 0 : dispositions.default) !== null && _d !== void 0 ? _d : 0) === 0
&& !isDescriptiveAudioStream
&& !defaultSet) {
args.jobLog("Setting stream ".concat(index, " (language ").concat(languageCode, ", channels ").concat(channels, ") has default"));
args.jobLog("Stream ".concat(index, " (language ").concat(languageCode, ", channels ").concat(channels, ") set has default"));
stream.outputArgs.push("-c:".concat(index), 'copy', "-disposition:".concat(index), getFFMPEGDisposition(true, dispositions));
defaultSet = true;
shouldProcess = true;
}
else {
else if (((_e = dispositions === null || dispositions === void 0 ? void 0 : dispositions.default) !== null && _e !== void 0 ? _e : 0) === 1) {
args.jobLog("Stream ".concat(index, " (language ").concat(languageCode, ", channels ").concat(channels, ", \n descriptive ").concat(isDescriptiveAudioStream, ") set has not default"));
stream.outputArgs.push("-c:".concat(index), 'copy', "-disposition:".concat(index), getFFMPEGDisposition(false, dispositions));
shouldProcess = true;
}
}
});
if (defaultSet) {
if (shouldProcess) {
// eslint-disable-next-line no-param-reassign
args.variables.ffmpegCommand.shouldProcess = true;
// eslint-disable-next-line no-param-reassign
Expand All @@ -152,7 +167,7 @@ var plugin = function (args) {
args.jobLog('No matching stream was found');
return {
outputFileObj: args.inputFileObj,
outputNumber: defaultSet ? 1 : 2,
outputNumber: shouldProcess ? 1 : 2,
variables: args.variables,
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const details = (): IpluginDetails => ({
});

interface IDisposition {
default: number,
dub: number,
original: number,
comment: number,
Expand All @@ -101,6 +102,7 @@ interface IDisposition {

interface IStreamDisposition {
disposition?: IDisposition
tags?: { title?: string }
}

const getFFMPEGDisposition = (isDefault: boolean, dispositions?: IDisposition): string => {
Expand All @@ -123,6 +125,12 @@ const getFFMPEGDisposition = (isDefault: boolean, dispositions?: IDisposition):
|| '0';
};

const getIsDescriptiveAudioStream = (stream: IStreamDisposition): boolean => Boolean(stream.disposition
&& (stream.disposition.comment
|| stream.disposition.descriptions
|| stream.disposition.visual_impaired
|| /\b(commentary|description|descriptive)\b/gi.test(stream.tags?.title || '')));

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const plugin = (args: IpluginInputArgs): IpluginOutputArgs => {
const lib = require('../../../../../methods/lib')();
Expand All @@ -134,6 +142,7 @@ const plugin = (args: IpluginInputArgs): IpluginOutputArgs => {
// const streams: IffmpegCommandStream[] = JSON.parse(JSON.stringify(args.variables.ffmpegCommand.streams));
const { streams } = args.variables.ffmpegCommand;

let shouldProcess = false;
let defaultSet = false;

// Sets the language code used to determine the default audio stream
Expand All @@ -158,29 +167,36 @@ const plugin = (args: IpluginInputArgs): IpluginOutputArgs => {
streams.forEach((stream, index) => {
if (stream.codec_type === 'audio') {
const dispositions = (stream as IStreamDisposition).disposition;
const isDescriptiveAudioStream = getIsDescriptiveAudioStream(stream as IStreamDisposition);
if ((stream.tags?.language ?? '') === languageCode
&& (stream.channels ?? 0) === channels
&& (dispositions?.default ?? 0) === 0
&& !isDescriptiveAudioStream
&& !defaultSet) {
args.jobLog(`Setting stream ${index} (language ${languageCode}, channels ${channels}) has default`);
args.jobLog(`Stream ${index} (language ${languageCode}, channels ${channels}) set has default`);
stream.outputArgs.push(
`-c:${index}`,
'copy',
`-disposition:${index}`,
getFFMPEGDisposition(true, dispositions),
);
defaultSet = true;
} else {
shouldProcess = true;
} else if ((dispositions?.default ?? 0) === 1) {
args.jobLog(`Stream ${index} (language ${languageCode}, channels ${channels},
descriptive ${isDescriptiveAudioStream}) set has not default`);
stream.outputArgs.push(
`-c:${index}`,
'copy',
`-disposition:${index}`,
getFFMPEGDisposition(false, dispositions),
);
shouldProcess = true;
}
}
});

if (defaultSet) {
if (shouldProcess) {
// eslint-disable-next-line no-param-reassign
args.variables.ffmpegCommand.shouldProcess = true;
// eslint-disable-next-line no-param-reassign
Expand All @@ -189,7 +205,7 @@ const plugin = (args: IpluginInputArgs): IpluginOutputArgs => {

return {
outputFileObj: args.inputFileObj,
outputNumber: defaultSet ? 1 : 2,
outputNumber: shouldProcess ? 1 : 2,
variables: args.variables,
};
};
Expand Down

0 comments on commit afde558

Please sign in to comment.