Repository navigation
Expand file tree
/
Copy pathvideo-reader.js
More file actions
86 lines (77 loc) · 2.98 KB
/
Copy pathvideo-reader.js
File metadata and controls
86 lines (77 loc) · 2.98 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
const fs = require('fs')
const path = require('path')
const os = require('os')
const rmdirRecursive = require('rmdir-recursive')
const child = require('child_process')
const util = require('util')
// VideoReader is a simple interface to ffmpeg (which must have CLI tool installed in host system)
// it uses the CLI tool to convert short videos in to a sequence of png files in a temporary folder
// and provides access to those images to make randomly reading video files easy
class VideoReader {
constructor(videoPath) {
this.srcVideoPath = videoPath
this.videoFilters = { gamma: 1.0, contrast: 1.0 }
this.length = null
}
// decode video in to frame files
// async, returns a promise which resolves when all frames are decoded and available via @frame and @frames
decode() {
return new Promise((resolve, reject)=> {
fs.mkdtemp(path.join(os.tmpdir(), 'VideoReader-'), (err, folder)=> {
if (err) return resolve(err)
this.tempPath = folder
child.execFile('ffmpeg', [
'-i', this.srcVideoPath, '-vf',
'eq=' + Object.entries(this.videoFilters).map(([key,val])=> `${key}=${val}`).join(':'),
path.join(this.tempPath, 'frame-%00d.png')
], {}, (err, stdout, stderr)=> {
if (err) return reject(err)
// find last statement about final frame and store that as length
// var m = stderr.match(/frame=([ 0-9]+)fps=/)
// if (m) {
// this.length = parseInt(m[1].trim())
// resolve(this)
// } else {
// reject(stderr)
// }
// figure out frame count by looking in folder
fs.readdir(folder, {}, (err, list)=> {
if (err) reject(err)
this.length = list.filter((x)=> x.match(/frame/)).length
resolve(this)
})
})
})
})
}
// return path to frame file
frame(num) {
if (!this.tempPath) throw new Error("VideoReader is not open, call decode() first")
return path.join(this.tempPath, `frame-${parseInt(num)+1}.png`)
}
// get an array of frame paths
frames() {
if (!this.tempPath) throw new Error("VideoReader is not open, call decode() first")
var result = []
for (var i = 0; i < this.length; i++) {
result[i] = path.join(this.tempPath, `frame-${i+1}.png`)
}
return result
}
// when done using this video, close it to free hard drive space
// note, if close isn't called, temporary png files may pollute system drive indefinitely, be careful!
async close() {
if (!this.tempPath) return
await util.promisify(rmdirRecursive)(this.tempPath)
this.tempPath = null
this.length = null
}
}
// async, open a video file and decode it
// returns a promise, which resolves a decoded VideoReader
VideoReader.open = function(videoPath, filters) {
var reader = new VideoReader(videoPath)
if (filters) Object.keys(filters).forEach((key)=> reader.videoFilters[key] = filters[key])
return reader.decode()
}
module.exports = VideoReader