123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 'use strict';
- import childProcess = require('child_process');
- import fs = require('fs');
- import os = require('os');
- import path = require('path');
- import subtitle from '../subtitle/index';
- /**
- * Merges the subtitle and video files into a Matroska Multimedia Container.
- */
- export default function(config: IConfig, isSubtitled: boolean, rtmpInputPath: string, filePath: string,
- streamMode: string, done: (err: Error) => void)
- {
- const subtitlePath = filePath + '.' + (subtitle.formats[config.format] ? config.format : 'ass');
- let videoPath = filePath;
- if (streamMode === 'RTMP')
- {
- videoPath += path.extname(rtmpInputPath);
- }
- else
- {
- videoPath += '.mp4';
- }
- childProcess.exec(command() + ' ' +
- '-o "' + filePath + '.mkv" ' +
- '"' + videoPath + '" ' +
- (isSubtitled ? '"' + subtitlePath + '"' : ''), {
- maxBuffer: Infinity,
- }, (err) =>
- {
- if (err)
- {
- return done(err);
- }
- unlink(videoPath, subtitlePath, (errin) =>
- {
- if (errin)
- {
- unlinkTimeout(videoPath, subtitlePath, 5000);
- }
- done(null);
- });
- });
- }
- /**
- * Determines the command for the operating system.
- */
- function command(): string
- {
- if (os.platform() !== 'win32')
- {
- return 'mkvmerge';
- }
- return '"' + path.join(__dirname, '../../bin/mkvmerge.exe') + '"';
- }
- /**
- * Unlinks the video and subtitle.
- * @private
- */
- function unlink(videoPath: string, subtitlePath: string, done: (err: Error) => void)
- {
- fs.unlink(videoPath, (err) =>
- {
- if (err)
- {
- return done(err);
- }
- fs.unlink(subtitlePath, done);
- });
- }
- /**
- * Attempts to unlink the video and subtitle with a timeout between each try.
- */
- function unlinkTimeout(videoPath: string, subtitlePath: string, timeout: number)
- {
- setTimeout(() =>
- {
- unlink(videoPath, subtitlePath, (err) =>
- {
- if (err)
- {
- unlinkTimeout(videoPath, subtitlePath, timeout);
- }
- });
- }, timeout);
- }
|