Skip to content

Commit f160dec

Browse files
util(duration): add screen duration util.
1 parent 5272e37 commit f160dec

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/util/screen-duration.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @param {Array} trims
3+
* @returns {number}
4+
* @description Get video trims duration.
5+
*/
6+
const _getVideoTrimsDuration = (trims) => {
7+
return trims.reduce((acc, elem, index) => {
8+
if (index % 2 === 0) {
9+
return acc + (trims[index + 1] - elem)
10+
}
11+
return acc
12+
}, 0)
13+
}
14+
15+
/**
16+
* @param {Array} areas
17+
* @returns {number}
18+
* @description Get screen areas duration.
19+
*/
20+
const _getScreenAreasDuration = (areas) => {
21+
return areas.reduce((acc, area) => {
22+
const videoCropParams = area && area.type === 'video' ? area.videoCropParams : null
23+
if (!videoCropParams) return acc
24+
25+
if (videoCropParams.duration) {
26+
return acc + _getVideoTrimsDuration(videoCropParams.trims)
27+
}
28+
29+
return acc
30+
}, 0)
31+
}
32+
33+
/**
34+
* @param {Object} screen
35+
* @param {Array} screen.areas
36+
* @returns {number}
37+
* @description Get screen duration.
38+
*/
39+
const getScreenDuration = (screen) => {
40+
const { characterBasedDuration, selectedDuration, extraVideoSecond, areas, duration } = screen
41+
42+
// If screen duration is adjustable and have selectedDuration, return selectedDuration
43+
if (characterBasedDuration && selectedDuration) {
44+
return selectedDuration
45+
}
46+
47+
const extra = extraVideoSecond
48+
? parseInt(extraVideoSecond, 10) || 0
49+
: 0
50+
51+
const videoArea = areas.find((area) => area.type === 'video')
52+
const conditionScreenDuration = videoArea
53+
? 0
54+
: parseInt(duration, 10) || 0
55+
56+
return (_getScreenAreasDuration(areas) + extra + conditionScreenDuration)
57+
}
58+
59+
module.exports = {
60+
getScreenDuration
61+
}

0 commit comments

Comments
 (0)