-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (73 loc) · 2.25 KB
/
index.js
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
const { spawn } = require('child-process-promise')
const fs = require('fs')
const path = require('path')
const { promisify } = require('util')
const fsMkdir = promisify(fs.mkdir)
const fsReaddir = promisify(fs.readdir)
const fsRename = promisify(fs.rename)
const fsUnlink = promisify(fs.unlink)
const videoDirName = 'videos'
const dirPath = path.join(__dirname, videoDirName)
fsReaddir(dirPath).then(async videos => {
for (const video of videos) {
// skip hidden files on macOS
if (video.charAt(0) === '.') {
continue
}
console.log(`Converting video: ${video}`)
const videoName = video.substring(0, video.lastIndexOf('.'))
const videoDirRelativePath = `${videoDirName}/${videoName}`
try {
await makeDirIfDoesntExist(videoDirRelativePath)
await spawn('ffmpeg', [
'-i',
`${videoDirName}/${video}`,
`${videoDirRelativePath}/${videoName}_%03d.png`,
])
console.log(`Extracted frames`)
const images = await fsReaddir(videoDirRelativePath)
for (const image of images) {
// skip hidden files on macOS
if (image.charAt(0) === '.') {
continue
}
await processImage(videoDirRelativePath, image)
}
} catch (err) {
console.log(`Something went wrong for file '${video}'. Error: ${err}`)
}
console.log(`Successfully processed video: ${video}`)
}
console.log(`Done!`)
})
const makeDirIfDoesntExist = async relativePath => {
const absolutePath = path.join(__dirname, relativePath)
return fsMkdir(absolutePath).catch(err => {
if (err.code == 'EEXIST') {
// Folder exists, continue normally
return Promise.resolve()
} else {
return Promise.reject(err)
}
})
}
const processImage = async (dirPath, image) => {
console.log(`Processing image: ${image}`)
await spawn('ffmpeg', [
'-i',
`${dirPath}/${image}`,
'-vf',
'chromakey=0x70de77:0.19:0.0',
`${dirPath}/temp_${image}`,
])
await spawn('ffmpeg', [
'-i',
`${dirPath}/temp_${image}`,
'-vf',
'crop=512:900:240:40',
`${dirPath}/cropped_${image}`,
])
await fsRename(`${dirPath}/cropped_${image}`, `${dirPath}/${image}`)
await fsUnlink(`${dirPath}/temp_${image}`)
console.log(`Successfully processed image: ${image}`)
}