-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·191 lines (162 loc) · 5.03 KB
/
main.js
File metadata and controls
executable file
·191 lines (162 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env node --harmony
/*=====================================================
IMPORTS / SETUP
======================================================*/
const _ = require('lodash')
const fs = require('fs')
const yaml = require('js-yaml')
const clc = require('cli-color')
const shell = require('shelljs')
const program = require('commander')
const logUpdate = require('log-update')
const imageToAscii = require('image-to-ascii')
const TMP_DIR_PATH = '/tmp/__sprite_cli_output/'
const END_OF_FRAME_ID = '\nzzzzzzzzzzzzzzzzzzzzzzz'
/*=====================================================
MAIN
======================================================*/
program
.version('0.1.2')
.command('create <input-video> <output-filename>')
.description(
'Takes an input video, converts it into ASCII frames, and writes it to an output file.'
)
.action((video, outputTo) => {
if (!_.endsWith(outputTo, '.yaml')) {
return console.log(errMsg('The outputfile must be a yaml file.'))
}
// remove the output file if it exists already
if (fs.existsSync(outputTo)) {
fs.unlinkSync(outputTo)
}
const dir = process.cwd()
const finishLoadingId = showLoading()
// make temp directory to write image files to
shell.exec(`cd /tmp && mkdir __sprite_cli_output && cd ${dir}`)
if (
shell.exec(`ffmpeg -i ${program.args[0]} ${TMP_DIR_PATH}image%d.jpg`)
.code !== 0
) {
// stop loading animation
clearInterval(finishLoadingId)
return console.log(errMsg('@todo: error message for shit went wrong.'))
}
// stop loading animation
clearInterval(finishLoadingId)
// ensure frames are in correct order
const files = []
fs.readdirSync(TMP_DIR_PATH)
.forEach(f => {
try {
const fId = parseInt(f.match(/\d/g).join(''))
files[fId - 1] = f // convert 1-indexed id to 0-indexed
} catch (e) {
return 0
}
})
createSprites(files, outputTo, 0, [])
})
program
.command('play <file>')
.description('Plays back a generated sprite file')
.option(
'-f, --frame_rate <rate>',
'A number which specifies the rate at which to iterate through the sprites'
)
.action((pathToFile, opts) => {
console.log(pathToFile)
const lineReader = require('readline').createInterface({
input: require('fs').createReadStream(pathToFile),
})
let frame = ''
const re = RegExp(END_OF_FRAME_ID, 'g')
const frameRate = opts && opts.frame_rate ? opts.frame_rate : 155
lineReader.on('line', async line => {
lineReader.pause()
const fragment = yaml.safeLoad(line)[0]
frame += fragment
if (re.test(frame)) {
const frames = frame.split(END_OF_FRAME_ID)
for (let frameItem of frames) {
await delay(frameRate)
if (frameItem.length) logUpdate(frameItem)
}
frame = ''
}
lineReader.resume()
})
})
program.parse(process.argv)
if (!program.args.length) program.help()
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time))
}
/*=====================================================
HELPERS
======================================================*/
function createSprites(files, outputTo, idx, sprites) {
if (idx === files.length) {
appendToFile(outputTo, sprites, true)
// clean up temp directory after the last chunk of sprites is written
shell.exec(`rm -rf ${TMP_DIR_PATH}`)
console.log(infoMsg(`File written to ${outputTo}`))
} else {
imageToAscii(
TMP_DIR_PATH + files[idx],
{
image_type: 'jpg',
},
(err, converted) => {
if (err) {
console.log(warningMsg(err))
} else {
sprites.push(converted + '\nzzzzzzzzzzzzzzzzzzzzzzz')
// write to disk before sprites array gets too large
if (sprites.length > 500) {
appendToFile(outputTo, sprites)
sprites = []
}
logUpdate(
`Creating sprites: ${Math.round(idx / files.length * 100)}%`
)
}
createSprites(files, outputTo, idx + 1, sprites)
}
)
}
}
function appendToFile(outputTo, sprites) {
const outFile = yaml.safeDump(sprites)
fs.appendFile(outputTo, outFile, err => {
if (err) return console.log(warningMsg(err))
})
}
function showLoading() {
const frames = ['-', '\\', '|', '/']
let i = 0
return setInterval(() => {
const frame = frames[(i = ++i % frames.length)]
logUpdate(`${frame} Converting video to frames ${frame}`)
}, 80)
}
function readFile(pathTo) {
return new Promise((resolve, reject) => {
fs.readFile(pathTo, (err, data) => {
if (!err) return resolve(data)
console.log(err)
reject(err)
})
})
}
function infoMsg(msg) {
const infoColor = clc.xterm(33)
return infoColor(msg)
}
function errMsg(msg) {
const errColor = clc.xterm(9)
return errColor(msg)
}
function warningMsg(msg) {
const warningColor = clc.xterm(214)
return warningColor(msg)
}