Skip to content

Instantly share code, notes, and snippets.

@monyone
Created June 3, 2021 11:44
Show Gist options
  • Save monyone/2d1e9f3c764ea174e8b0714eea0c9dec to your computer and use it in GitHub Desktop.
Save monyone/2d1e9f3c764ea174e8b0714eea0c9dec to your computer and use it in GitHub Desktop.
EPGStation 用字幕入り encode 用スクリプト

EPGStation 用字幕入り encode 用スクリプト

EPGStation で TS ファイルとしてエンコードして、encode 後に字幕を残す設定です。

const spawn = require('child_process').spawn;
const fs = require('fs');
const ID3MetadataTransfrom = require('arib-subtitle-timedmetadater').default
const { FFmpegID3PaddingTransform } = require('arib-subtitle-timedmetadater')
const ffmpeg = process.env.FFMPEG;
const input = process.env.INPUT;
const output = process.env.OUTPUT;
const src = fs.createReadStream(input);
const dst = fs.createWriteStream(output);
const analyzedurationSize = '10M'; // Mirakurun の設定に応じて変更すること
const probesizeSize = '32M'; // Mirakurun の設定に応じて変更すること
const maxMuxingQueueSize = 1024;
const dualMonoMode = 'main';
const videoHeight = parseInt(process.env.VIDEORESOLUTION, 10);
const isDualMono = parseInt(process.env.AUDIOCOMPONENTTYPE, 10) == 2;
const audioBitrate = videoHeight > 720 ? '192k' : '128k';
const preset = 'veryfast';
const codec = 'h264_omx';
const crf = 23;
const args = ['-y', '-analyzeduration', analyzedurationSize, '-probesize', probesizeSize];
// dual mono 設定
if (isDualMono) {
Array.prototype.push.apply(args, ['-dual_mono_mode', dualMonoMode]);
}
// input 設定
Array.prototype.push.apply(args,['-f', 'mpegts', '-i', 'pipe:0']);
// メタ情報を先頭に置く
Array.prototype.push.apply(args,['-movflags', 'faststart']);
// 字幕データを含めたストリームをすべてマップ
Array.prototype.push.apply(args, ['-map', '0', '-ignore_unknown', '-max_muxing_queue_size', maxMuxingQueueSize, '-sn']);
// video filter 設定
let videoFilter = 'yadif';
if (videoHeight > 720) {
videoFilter += ',scale=-2:720'
}
Array.prototype.push.apply(args, ['-vf', videoFilter]);
// その他設定
Array.prototype.push.apply(args,[
'-preset', preset,
'-aspect', '16:9',
'-c:v', codec,
'-crf', crf,
'-f', 'mpegts',
'-c:a', 'aac',
'-ar', '48000',
'-ab', audioBitrate,
'-ac', '2',
'pipe:1'
]);
let str = '';
for (let i of args) {
str += ` ${ i }`
console.error(str);
const child = spawn(ffmpeg, args);
child.stderr.on('data', (data) => { console.error(String(data)); });
src.pipe(new ID3MetadataTransfrom()).pipe(child.stdin);
child.stdout.pipe(new FFmpegID3PaddingTransform()).pipe(dst);
child.on('error', (err) => {
console.error(err);
throw new Error(err);
});
process.on('SIGINT', () => {
child.kill('SIGINT');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment