-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideoSort.js
53 lines (37 loc) · 1.25 KB
/
videoSort.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
// ORIGINAL FILES
const fs = require('node:fs');
const path = require('node:path');
// Get all videos
const videos = fs.readdirSync(__dirname);
// Get video info
const withInfo = videos.map(name => ({
name,
time: fs.statSync(path.join(__dirname, name)).birthtime.getTime()
}));
// Sort videos and map to names only
const sortedVideos = withInfo.sort((a, b) => a.time - b.time).map(v => v.name);
// update extensions
const uptatedNames = sortedVideos.map(v => v.split('.')[0] + '.mp4');
// get arrays with current and new names
const nameMapping = uptatedNames.map((v, i) => [v, i + 1]);
// log results
console.log(nameMapping);
// ---------------------------------------------------------------------------------------
// CONVERTED FILES
const fs = require('node:fs');
const path = require('node:path');
// resulting array from original files
const namesMapping = [];
for (const [name, newName] of namesMapping) {
const file = path.join(__dirname, name);
try {
// get file stats - if !exists, error is thrown
const stats = fs.statSync(file);
// console.log(stats);
// build new filename
const newFile = path.join(__dirname, newName + '.mp4');
// console.log(newFile);
// rename file
fs.renameSync(file, newFile);
} catch { }
}