Last active
January 15, 2021 19:20
-
-
Save deoxxa/5467112 to your computer and use it in GitHub Desktop.
lol "streaming video"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ffmpeg -i /path/to/a/video -f rawvideo -vcodec rawvideo -s 60x24 -pix_fmt rgb24 - 2>/dev/null | ./video.js 60 24 | |
check it out: http://d.pr/i/wjEj |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var stream = require("stream"), | |
Jetty = require("jetty"); | |
var Renderer = function Renderer(options, config) { | |
stream.Duplex.call(this, options); | |
this.width = config.width; | |
this.height = config.height; | |
this.buffer = new Buffer(0); | |
this.cache = {}; | |
this.tty = new Jetty(); | |
this.pixel = 0; | |
this.frame = 0; | |
this.bytes = 0; | |
this.tty.clear().hide(); | |
}; | |
Renderer.prototype = Object.create(stream.Duplex.prototype); | |
Renderer.prototype._read = function _read(n) { | |
var chunk = this.tty.read(n); | |
if (chunk === null) { | |
this.tty.once("readable", this._read.bind(this, n)); | |
} else { | |
this.push(chunk); | |
} | |
}; | |
Renderer.prototype._write = function _write(input, encoding, done) { | |
this.buffer = Buffer.concat([this.buffer, input]); | |
var pixels = Math.floor(this.buffer.length / 3); | |
var x, y, r, g, b; | |
for (var i=0;i<pixels;++i) { | |
x = Math.floor((this.pixel + i) / this.width) % this.height; | |
y = (this.pixel + i) % this.width; | |
if (x === 0 && y === 0) { | |
this.tty.moveTo([this.height + 1, 0]).reset().text(this.frame + ", " + this.bytes); | |
this.frame++; | |
} | |
r = Math.round(this.buffer[i * 3 + 0] / 64); | |
g = Math.round(this.buffer[i * 3 + 1] / 64); | |
b = Math.round(this.buffer[i * 3 + 2] / 64); | |
if (this.cache[x + "-" + y + "-r"] !== r || this.cache[x + "-" + y + "-g"] !== g || this.cache[x + "-" + y + "-b"] !== b) { | |
this.tty.moveTo([x, y]).rgb([r, g, b], true).text(" "); | |
this.bytes++; | |
} | |
this.cache[x + "-" + y + "-r"] = r; | |
this.cache[x + "-" + y + "-g"] = g; | |
this.cache[x + "-" + y + "-b"] = b; | |
} | |
this.pixel = (this.pixel + pixels) % (this.width * this.height); | |
this.buffer = this.buffer.slice(pixels * 3); | |
done(); | |
}; | |
process.stdin.pipe(new Renderer(null, {width: parseInt(process.argv[2]), height: parseInt(process.argv[3])})).pipe(process.stdout); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment