├── .gitignore ├── LICENSE ├── README.md ├── bin.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # web-transcode 2 | 3 | CLI program to transcode video + captions for playback in a Browser 4 | 5 | ```sh 6 | npm i -g web-transcode 7 | 8 | # will create web/video.mp4, web/video.vtt, web/video.html 9 | # 10 | # video.mp4 is the transcoded video with video codec h264 11 | # video.vtt is the vtt captions file 12 | # 13 | # video.html will contain a simple fullscreen player 14 | # that adds the subtitles to the video 15 | 16 | web-transcode -i video.something -o web -s subtitle.srt 17 | ``` 18 | 19 | Requires ffmpeg to be installed in your PATH. 20 | -------------------------------------------------------------------------------- /bin.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const vtt = require('srt-to-vtt') 4 | const { spawn, spawnSync } = require('child_process') 5 | const { mkdirSync, createReadStream, createWriteStream, writeFileSync, existsSync } = require('fs') 6 | const minimist = require('minimist') 7 | const path = require('path') 8 | const pump = require('pump') 9 | const os = require('os') 10 | 11 | const argv = minimist(process.argv.slice(2), { 12 | alias: { 13 | input: 'i', 14 | subtitles: 's', 15 | subs: 's', 16 | sub: 's', 17 | out: 'o', 18 | name: 'n' 19 | }, 20 | '--': true 21 | }) 22 | 23 | const onlyMp4 = !!argv['only-mp4'] 24 | 25 | if (!argv.out) { 26 | argv.out = 'web' 27 | } 28 | 29 | if (!argv.input || argv.help) { 30 | console.log('Usage: web-transcode -i ( -s subtitles -n name -o ./web --only-mp4? )') 31 | process.exit(1) 32 | } 33 | 34 | if (!existsSync(argv.input)) { 35 | console.log('Input file does not exist') 36 | process.exit(2) 37 | } 38 | 39 | if (argv.s === true || argv.s === 'auto') { 40 | const tmp = tmpFile('srt') 41 | spawnSync('ffmpeg', [ '-i', argv.input, '-map', '0:s:0', tmp ], { stdio: 'inherit' }) 42 | argv.s = tmp 43 | } 44 | 45 | if (argv.s && !existsSync(argv.s)) { 46 | console.log('Subtitle file does not exist') 47 | process.exit(2) 48 | } 49 | 50 | const name = (argv.name || path.basename(argv.input, path.extname(argv.input))) 51 | 52 | try { 53 | mkdirSync(argv.out) 54 | } catch (_) {} 55 | 56 | const mediaOut = path.join(argv.out, name + '.mp4') 57 | const subsOut = onlyMp4 ? tmpFile('.vtt') : path.join(argv.out, name + '.vtt') 58 | const htmlOut = onlyMp4 ? tmpFile('.html') : path.join(argv.out, name + '.html') 59 | 60 | const { stderr } = spawnSync('ffmpeg', [ '-i', argv.input, '-hide_banner' ]) 61 | 62 | let v = (stderr.toString().match(/Stream #0.+ Video: (\w+) /) || [ null, null ])[1] 63 | let a = (stderr.toString().match(/Stream #0.+ Audio: (\w+) /) || [ null, null ])[1] 64 | 65 | if (v === 'h264') v = 'copy' 66 | else v = 'h264' 67 | 68 | if (a === 'aac') a = 'copy' 69 | else a = 'aac' 70 | 71 | if (argv.s) { 72 | const stream = /\.vtt$/.test(argv.s) 73 | ? createReadStream(argv.s) 74 | : pump(createReadStream(argv.s), vtt()) 75 | pump(stream, createWriteStream(subsOut), function (err) { 76 | if (err) throw err 77 | ready() 78 | }) 79 | } else { 80 | ready() 81 | } 82 | 83 | function ready () { 84 | const extraArgs = [].concat(argv['--'] || []) 85 | const subArgs = [] 86 | 87 | if (argv.s) { 88 | subArgs.push( 89 | '-i', 90 | subsOut, 91 | '-c:s', 92 | 'mov_text' 93 | ) 94 | } 95 | 96 | spawn('ffmpeg', [ 97 | '-i', argv.input, 98 | ...subArgs, 99 | '-y', 100 | '-hide_banner', 101 | '-vcodec', v, 102 | '-acodec', a, 103 | ...extraArgs, 104 | mediaOut 105 | ], { stdio: 'inherit' }) 106 | 107 | if (argv.html !== false) { 108 | writeFileSync(htmlOut, ` 109 | 110 | 124 | 125 | 126 | 130 | 131 | 132 | `) 133 | } 134 | } 135 | 136 | function tmpFile (ext) { 137 | return path.join(os.tmpdir(), 'web-transcode.' + Math.random().toString(16).slice(2) + '.' + ext) 138 | } 139 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "web-transcode", 3 | "version": "1.4.0", 4 | "description": "Transcodes a video file plus captions so it can be played in a browser", 5 | "main": "bin.js", 6 | "bin": { 7 | "web-transcode": "./bin.js" 8 | }, 9 | "dependencies": { 10 | "minimist": "^1.2.0", 11 | "pump": "^3.0.0", 12 | "srt-to-vtt": "^1.1.3" 13 | }, 14 | "devDependencies": { 15 | "standard": "^12.0.1" 16 | }, 17 | "scripts": { 18 | "test": "standard" 19 | }, 20 | "author": "Mathias Buus (@mafintosh)", 21 | "license": "MIT" 22 | } 23 | --------------------------------------------------------------------------------