Skip to content

Instantly share code, notes, and snippets.

@scr4tchy
Last active March 1, 2023 21:03
Show Gist options
  • Save scr4tchy/c82e7f0c8d5cc96cc03da9c8d2885712 to your computer and use it in GitHub Desktop.
Save scr4tchy/c82e7f0c8d5cc96cc03da9c8d2885712 to your computer and use it in GitHub Desktop.
Tdarr_Plugin_Kedal_ExtractPGS.js
/* eslint-disable */
// tdarrSkipTest
// # pgsrip - Windows:
//
// choco install mkvtoolnix
// choco install tesseract-ocr
// pip install pgsrip
// cd C:\Users\quent\Downloads\tdarr
// git clone https://github.com/tesseract-ocr/tessdata_best.git
// cd tessdata_best
// New-Item -ItemType SymbolicLink -Path "zho.traineddata" -Target "chi_sim.traineddata"
// # pgsrip - Dockerfile
// FROM ratoaq2/pgsrip AS pgsrip
// FROM haveagitgat/tdarr
// ENV PYTHONFAULTHANDLER=1 \
// PYTHONUNBUFFERED=1 \
// PYTHONHASHSEED=random \
// PYTHONDONTWRITEBYTECODE=1 \
// PIP_NO_CACHE_DIR=off \
// PIP_DISABLE_PIP_VERSION_CHECK=on \
// PIP_DEFAULT_TIMEOUT=100 \
// TESSDATA_PREFIX=/usr/src/tessdata
//
// RUN apt-get update \
// && apt-get install -y --no-install-recommends curl gpg \
// && curl -sSL https://notesalexp.org/debian/alexp_key.asc | gpg --dearmor > /etc/apt/trusted.gpg.d/alex-p-ubuntu-tesseract-ocr5.gpg \
// && curl -sSL -o /usr/share/keyrings/gpg-pub-moritzbunkus.gpg https://mkvtoolnix.download/gpg-pub-moritzbunkus.gpg \
// && echo "deb https://notesalexp.org/tesseract-ocr5/focal/ focal main" >> /etc/apt/sources.list \
// && echo "deb [signed-by=/usr/share/keyrings/gpg-pub-moritzbunkus.gpg] https://mkvtoolnix.download/ubuntu/ focal main" >> /etc/apt/sources.list.d/mkvtoolnix.download.list \
// && echo "deb-src [signed-by=/usr/share/keyrings/gpg-pub-moritzbunkus.gpg] https://mkvtoolnix.download/ubuntu/ focal main" >> /etc/apt/sources.list.d/mkvtoolnix.download.list \
// && apt-get update \
// && apt-get install -y python3-pip \
// && apt-get install -y --no-install-recommends ffmpeg libsm6 libxext6 tesseract-ocr mkvtoolnix \
// && apt-get clean \
// && rm -rf /var/lib/apt/lists/*
//
// COPY --from=pgsrip ${TESSDATA_PREFIX} ${TESSDATA_PREFIX}
// COPY --from=pgsrip /usr/src/dist /usr/src/dist
// RUN pip install /usr/src/dist/pgsrip-*.tar.gz
//
// # Symlink chinese simplified to "zho" (as sometimes referred to by track tags)
// RUN ln -s /usr/src/tessdata/chi_sim.traineddata /usr/src/tessdata/zho.traineddata
const details = () => {
return {
id: "Tdarr_Plugin_Kedal_ExtractPGS",
Stage: "Pre-processing",
Name: "Kedal - Extract PGS into SRT files",
Type: "Video",
Operation: "Transcode",
Description: `This plugin extract PGS subs into SRT files with pgsrip. Both pgsrip & tessdata must be installed (see embedded instructions).\n`,
Version: "1.00",
Tags: "ffmpeg",
Inputs:[
{
name: "conda_windows_path",
type: 'string',
defaultValue: 'C:\\ProgramData\\Anaconda3\\Scripts\\conda.exe',
inputUI: {
type: 'text',
},
tooltip: 'Path to conda.exe',
},
{
name: "tessdata_windows_path",
type: 'string',
defaultValue: 'C:\\Users\\quent\\Downloads\\tdarr\\tessdata_best\\',
inputUI: {
type: 'text',
},
tooltip: 'Path to tessdata',
},
{
name: "languages",
type: 'string',
defaultValue: 'eng,en-US,zh,zho,zh-Hans,fr,fra,fr-FR',
inputUI: {
type: 'text',
},
tooltip: 'Languages to extract',
},
],
};
};
// eslint-disable-next-line no-unused-vars
const plugin = (file, librarySettings, inputs, otherArguments) => {
const spawn = require("child_process").spawnSync;
const lib = require('../methods/lib')();
// eslint-disable-next-line no-unused-vars,no-param-reassign
inputs = lib.loadDefaultValues(inputs, details);
// Must return this object at some point in the function else plugin will fail.
let response = {
processFile: false,
preset: "",
container: "",
handBrakeMode: false,
FFmpegMode: false,
reQueueAfter: true,
infoLog: "",
};
let subsArr = file.ffProbeData.streams.filter(row => row.codec_name === 'hdmv_pgs_subtitle')
if (subsArr.length === 0) {
response.infoLog += "No PGS subs in file to extract!";
return response
}
response.infoLog += `Extracting ${subsArr.length} PGS subtitles from "${file.file}"...\n\n`
let binary = "pgsrip"
let options = {}
let args = [].concat.apply([], inputs.languages.split(",").map(language => ["-l", language]).concat(["-a", "-f", "-w4", "--debug", file.file]))
if (otherArguments.ffmpegPath.includes("C:")) {
options = {env: {TESSDATA_PREFIX: inputs.tessdata_windows_path}};
args = ["run", binary].concat(args)
binary = inputs.conda_windows_path
}
let r = spawn(binary, args, options)
response.infoLog += r.stdout
response.infoLog += r.stderr
if (r.status != 0) {
throw new Error(`Failed to execute pgsrip with "${binary} ${args.join(" ")}":\n${r.stdout}\n${r.stderr}`);
}
return response;
};
module.exports.details = details;
module.exports.plugin = plugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment