-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
117 lines (95 loc) · 3.38 KB
/
Copy pathindex.js
File metadata and controls
117 lines (95 loc) · 3.38 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
#!/usr/bin/env node
// Dependencies
const unzipper = require('unzipper');
const fs = require('fs');
const path = require('path');
const prompt = require('prompts');
const yargs = require('yargs');
const ffmpeg = require("ffmpeg-cli");
const { hideBin } = require('yargs/helpers')
// Declare CLI arguments
const arguments = yargs(hideBin(process.argv))
.option('noprompts', {
alias: 'y',
type: 'boolean',
description: 'Options will automatically be set to defaults if not provided through args'
})
.option('debug', {
alias: 'd',
type: 'boolean',
description: 'Run in debug mode'
})
.command('framerate [fps]', 'Set a defined framerate (fps)', (yargs) => {
return yargs
.positional('fps', {
describe: 'Number of frames per second',
default: null
})
})
.parse()
// Variable declaring
var fps = arguments.framerate
var debug = arguments.d
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
// This code block handles command line arguments and prompts
if(!arguments.noprompts) (async () => {
if (fps == null){
let fpsprompt = await prompt({
type: 'number',
name: 'value',
message: 'What will be the video\'s FPS',
});
fps = fpsprompt.value
}
console.log(`Your video will be exported at ${fps} frames per second`)
unpack()
})(); else unpack()
function unpack(){
if(debug == true) console.log('\x1b[33m%s\x1b[0m', "\nWARNING: Debug mode has been enabled, performance may seem affected on lower end devices.")
console.log("\nCleaning up...")
// this chunk of code cleans up the /tmp directory for future use :P
const directory = 'work/tmp/extractedProject';
fs.readdir(directory, (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlinkSync(path.join(directory, file), err => {
if (err) throw err;
});
}
});
console.log("Unpacking SB3...")
// unzip sb3 file
fs.createReadStream('work/projects/project.sb3')
.pipe(unzipper.Extract({ path: 'work/tmp/extractedProject/' }))
.on('close', function () {
console.log("\nFinished unpacking!\n")
extractframes()
});
}
function extractframes(){
console.log("\nParsing main Project file, this may take long depending on your computer specs and project size...\n")
let object1 = JSON.parse(fs.readFileSync("work/tmp/extractedProject/project.json"));
console.log("Parsing finished!")
console.log("\nExtracting video frames... (will take a long time!)")
fs.rmSync("work/tmp/frames", { recursive: true, force: true });
fs.mkdirSync("work/tmp/frames/")
ffmpeg.runSync("-i \"work/projects/video.mp4\" -r ${fps} \"work/tmp/frames/frame-%04d.png\"")
}
function parseimages(){
console.log("Parsing images for json prep")
const directoryPath = path.join(__dirname, 'work/tmp/frames');
//passsing directoryPath and callback function
fs.readdir(directoryPath, function (err, files) {
//handling error
if (err) {
return console.log('Unable to list image files: ' + err);
}
//listing all files using forEach
let images = (fs.readdirSync(directoryPath))
if(debug == true) console.log(files)
});
}