EPGStation のエンコード

EPGStationのenc.jsを参考にエンコード処理を書いてみました。 fluent-ffmpeg と mathjs はインストールします。スクリプト「enc2.js」はconfig内に保存します。今回のスクリプトの特徴は次の通りです。

  • log4js を使ってエンコードのログを残す
  • aribts を使ってdrop情報をログに残す
  • ファイルサイズをログに残す
  • h264_qsvで低負荷&高速エンコード
  • fluent-ffmpeg を使ってエンコード
$ cd EPGStation
$ npm i fluent-ffmpeg
$ npm i mathjs
//
//   enc2.js
//

const ffmpeg   = require("fluent-ffmpeg");
const log4js   = require("log4js");
const util     = require("util");
const fs       = require("fs");
const math     = require("mathjs");
const aribts   = require("aribts");

const TsStream = aribts.TsStream;
const inputFile   = process.env.INPUT;
const outputFile  = process.env.OUTPUT;
const readStream = fs.createReadStream(inputFile);
const tsStream = new TsStream();
const videoHeight = parseInt(process.env.VIDEORESOLUTION, 10);
const space7 = "       ";

function byteFormat( byte ) {
  let ut = "MiB";
  if (byte >= math.pow(1024, 3)) ut = "GiB";
  const x = math.unit(byte, 'B');
  return math.format(x.to(ut), 3).toString();
}

let videoSize = videoHeight > 720 ? "1280x720" : "720x480";
let voiceMode = "main";

ffmpeg.setFfmpegPath(process.env.FFMPEG);

if (process.argv.length == 3) {
  if (process.argv[2].match(/^main$|^sub$/)) {
    voiceMode = process.argv[2];
  }
}

log4js.configure({
  appenders: {
    everything: { type: "dateFile", filename: "./logs/encjs2.log", "daysToKeep": 10 },
    "console":  { type: "console" }
  },
  categories: {
default: { appenders: ["everything","console"], level: "debug" }
  }
});
let logger = log4js.getLogger(process.pid.toString());

logger.debug(util.inspect(process.env, false, null));
logger.debug(util.inspect(process.argv, false, null));

readStream.pipe(tsStream);
tsStream
  .on("data", () => {})
  .on("info", data => {
    Object.keys(data).forEach(key => {
        logger.debug(`0x${("000" + parseInt(key, 10).toString(16)).slice(-4)}: packet: ${(space7 + data[key].packet).slice(-8)}, drop: ${(space7 + data[key].drop).slice(-8)}, scrambling: ${(space7 + data[key].scrambling).slice(-8)}`);
    });
  });

ffmpeg()
  .input(inputFile)
  .inputOptions("-dual_mono_mode " + voiceMode)
  .videoFilters("yadif")
  .videoCodec("h264_qsv")
  .addOption('-look_ahead', 0)
  .addOption("-q", 20)
  .format("mp4")
  .size(videoSize)
  .audioCodec("aac")
  .audioFrequency(48000)
  .audioBitrate("128k")
  .audioChannels(2)
  .save(outputFile)
  .on("start", function(commandLine) {
      logger.info("cmd: " + commandLine);
  })
  .on("error", function(err, stdout, stderr) {
      logger.error("err: " + err.message);
      process.exit(1);
  })
  .on("end", function(stdout, stderr) {
      logger.info(inputFile + "     = " + byteFormat(fs.lstatSync(inputFile).size));
      logger.info(outputFile + " = " + byteFormat(fs.lstatSync(outputFile).size));
      logger.info("fin");
  });
    "encode": [
        {
            "name": "H264",
            "cmd": "/usr/bin/node /home/<Your Account>/EPGStation/config/enc2.js main",
            "suffix": ".mp4",
            "default": true
        },
        {
            "name": "H264-sub",
            "cmd": "/usr/bin/node /home/<Your Account>/EPGStation/config/enc2.js sub",
            "suffix": "-sub.mp4"
        }
    ],

取得されるログはこんな感じです。

[2017-12-18T08:15:47.352] [DEBUG] 4840 - { AUDIOCOMPONENTTYPE: '3',
  AUDIOSAMPLINGRATE: '48000',
  CHANNELID: '3239123608',
  FFMPEG: 'C:\\bin\\ffmpeg.exe',
  INPUT: 'C:\\EPGStation\\recorded\\171218-0135-GR3-323912360864304.ts',
  OUTPUT: 'C:\\EPGStation\\recorded\\171218-0135-GR3-323912360864304.ts.mp4',
  PATH: 'C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\EmEditor;C:\\Program Files\\Git\\cmd;C:\\nodejs\\;C:\\Program Files\\MariaDB 10.2\\bin;C:\\Program Files (x86)\\Windows Kits\\8.1\\Windows Performance Toolkit\\;C:\\Program Files (x86)\\AOMEI Backupper;C:\\WINDOWS\\system32\\config\\systemprofile\\AppData\\Local\\Microsoft\\WindowsApps',
  SYSTEMDRIVE: 'C:',
  SYSTEMROOT: 'C:\\WINDOWS',
  TEMP: 'C:\\WINDOWS\\TEMP',
  USERDOMAIN: 'AIRWHITE.NET',
  USERNAME: 'WIN10$',
  USERPROFILE: 'C:\\WINDOWS\\system32\\config\\systemprofile',
  VIDEOCOMPONENTTYPE: '179',
  VIDEORESOLUTION: '1080i',
  VIDEOSTREAMCONTENT: '1',
  VIDEOTYPE: 'mpeg2',
  WINDIR: 'C:\\WINDOWS' }
[2017-12-18T08:15:47.352] [DEBUG] 4840 - [ 'C:\\nodejs\\node.exe',
  'C:\\EPGStation\\config\\enc2.js',
  'main' ]
[2017-12-18T08:15:47.712] [INFO] 4840 - cmd: ffmpeg -dual_mono_mode main -i C:\EPGStation\recorded\171218-0135-GR3-323912360864304.ts -y -acodec aac -ar 48000 -b:a 128k -ac 2 -vcodec h264_qsv -filter:v yadif,scale=w=1280:h=720 -look_ahead 0 -q 20 -f mp4 C:\EPGStation\recorded\171218-0135-GR3-323912360864304.ts.mp4
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0000: packet:    18055, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0001: packet:      180, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0010: packet:     1806, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0011: packet:     1804, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0012: packet:   104146, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0014: packet:      360, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0023: packet:     1252, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0024: packet:     1803, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0028: packet:      100, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0029: packet:       84, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0031: packet:    18033, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0100: packet:    31182, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0101: packet:    36110, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0111: packet:  9585894, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0112: packet:   320288, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0114: packet:      401, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0740: packet:   259623, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0750: packet:   344129, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0751: packet:    59952, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0752: packet:     3452, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x075e: packet:      406, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0960: packet:   575406, drop:        0, scrambling:        0
[2017-12-18T08:15:58.587] [DEBUG] 4840 - 0x0961: packet:   443651, drop:        0, scrambling:        0
[2017-12-18T08:23:27.624] [INFO] 4840 - C:\EPGStation\recorded\171218-0135-GR3-323912360864304.ts     = 2.07 GiB
[2017-12-18T08:23:27.624] [INFO] 4840 - C:\EPGStation\recorded\171218-0135-GR3-323912360864304.ts.mp4 = 568 MiB
[2017-12-18T08:23:27.624] [INFO] 4840 - fin

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です