-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgetDuration.js
103 lines (92 loc) · 2.71 KB
/
getDuration.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
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
const fsPromise = require('fs').promises;
var ffmpeg = require('fluent-ffmpeg');
const path = require('path');
const _getDuration = (directoryPath, clipPath, languageCode) => {
return new Promise((resolve, reject) => {
ffmpeg(path.join(directoryPath, clipPath)).ffprobe(0, function (err, data) {
if (err) {
reject(err);
}
let duration =
data &&
data.format &&
data.format.duration &&
data.format.duration * 1000;
if (!duration) reject();
duration = Number.parseFloat(duration).toFixed(0);
resolve([duration, languageCode, clipPath]);
});
});
};
const makeData = async (directoryPath, clipPath, languageCode, pathf) => {
try {
const data = await _getDuration(
directoryPath,
clipPath,
languageCode,
pathf
);
await fsPromise.writeFile(pathf, data.toString() + ',\n', {
flag: 'a',
});
return data;
} catch (error) {
console.error(error);
}
};
/**
* Experimental Node-only implementation to get clip duration and save to a static file.
* Subsequent getDuration calls would read from file instead of ffmpeg.
* @param {string} languageCode Language Code (e.g. 'en')
* @param {string} directoryPath Path to directory where clips are located
* @param {string} durationLedgerFilePath Path to file where clip data is stored
* @returns Promise<{languageCode, totalClipDuration}>
*/
const getDuration = async (
languageCode,
directoryPath,
durationLedgerFilePath
) => {
let totalClipsDuration = 0;
const clipPaths = await fsPromise.readdir(directoryPath);
const pa = path.join(__dirname, durationLedgerFilePath);
const clipList = [];
await Promise.all(
clipPaths.map((clipPath) => {
return makeData(directoryPath, clipPath, languageCode, pa);
})
);
console.log(
`Language ${languageCode} has a total duration of ${totalClipsDuration}ms`
);
return new Promise((resolve, reject) => {
resolve({ languageCode, totalClipsDuration });
});
};
module.exports = {
getDuration,
};
const getFilesFromDirectory = async (directoryPath, callback) => {
return await fsPromise.readdir(directoryPath);
};
(async () => {
const args = process.argv.slice(2);
try {
const DIRECTORY_PATH =
args[0] || '/home/ubuntu/cv-bundler/cv-corpus-9.0-2022-04-27/';
const dirs = await getFilesFromDirectory(DIRECTORY_PATH);
await Promise.all(
dirs.map((langPath) => {
const lang = path.basename(langPath);
console.log('Reading ', lang, langPath);
return getDuration(
lang,
DIRECTORY_PATH + lang + '/clips',
lang + '-clip-durations.csv'
);
})
);
} catch (error) {
console.error(error);
}
})();