lossless-cut/src/util.js

157 wiersze
4.0 KiB
JavaScript
Czysty Zwykły widok Historia

const _ = require('lodash');
const path = require('path');
const fs = require('fs-extra');
const Swal = require('sweetalert2');
2019-01-28 17:09:54 +00:00
const randomColor = require('./random-color');
2020-02-14 05:48:58 +00:00
function formatDuration({ seconds: _seconds, fileNameFriendly, fps }) {
const seconds = _seconds || 0;
const minutes = seconds / 60;
const hours = minutes / 60;
const hoursPadded = _.padStart(Math.floor(hours), 2, '0');
const minutesPadded = _.padStart(Math.floor(minutes % 60), 2, '0');
const secondsPadded = _.padStart(Math.floor(seconds) % 60, 2, '0');
2020-02-20 11:18:36 +00:00
const ms = seconds - Math.floor(seconds);
2020-02-14 05:48:58 +00:00
const msPadded = fps != null
2020-02-20 11:18:36 +00:00
? _.padStart(Math.floor(ms * fps), 2, '0')
: _.padStart(Math.floor(ms * 1000), 3, '0');
// Be nice to filenames and use .
const delim = fileNameFriendly ? '.' : ':';
return `${hoursPadded}${delim}${minutesPadded}${delim}${secondsPadded}.${msPadded}`;
}
function parseDuration(str) {
if (!str) return undefined;
const match = str.trim().match(/^(\d{2}):(\d{2}):(\d{2})\.(\d{3})$/);
if (!match) return undefined;
const hours = parseInt(match[1], 10);
const minutes = parseInt(match[2], 10);
const seconds = parseInt(match[3], 10);
const ms = parseInt(match[4], 10);
if (hours > 59 || minutes > 59 || seconds > 59) return undefined;
return ((((hours * 60) + minutes) * 60) + seconds) + (ms / 1000);
}
2020-02-15 07:57:40 +00:00
function getOutDir(customOutDir, filePath) {
if (customOutDir) return customOutDir;
if (filePath) return path.dirname(filePath);
return undefined;
2020-02-15 07:57:40 +00:00
}
function getOutPath(customOutDir, filePath, nameSuffix) {
if (!filePath) return undefined;
2020-02-28 10:28:04 +00:00
const parsed = path.parse(filePath);
2020-02-28 10:28:04 +00:00
return path.join(getOutDir(customOutDir, filePath), `${parsed.name}-${nameSuffix}`);
}
async function transferTimestamps(inPath, outPath) {
try {
const stat = await fs.stat(inPath);
await fs.utimes(outPath, stat.atime.getTime() / 1000, stat.mtime.getTime() / 1000);
} catch (err) {
console.error('Failed to set output file modified time', err);
}
}
async function transferTimestampsWithOffset(inPath, outPath, offset) {
try {
const stat = await fs.stat(inPath);
const time = (stat.mtime.getTime() / 1000) + offset;
await fs.utimes(outPath, time, time);
} catch (err) {
console.error('Failed to set output file modified time', err);
}
}
const toast = Swal.mixin({
2019-01-27 23:21:53 +00:00
toast: true,
position: 'top',
showConfirmButton: false,
2019-08-12 20:04:43 +00:00
timer: 5000,
2019-01-27 23:21:53 +00:00
});
2020-02-14 04:06:15 +00:00
const errorToast = (title) => toast.fire({
2020-02-19 04:56:10 +00:00
icon: 'error',
2019-01-27 23:21:53 +00:00
title,
});
async function showFfmpegFail(err) {
console.error(err);
return errorToast(`Failed to run ffmpeg: ${err.stack}`);
}
function setFileNameTitle(filePath) {
const appName = 'LosslessCut';
2020-02-11 14:15:55 +00:00
document.title = filePath ? `${appName} - ${path.basename(filePath)}` : appName;
}
function filenamify(name) {
return name.replace(/[^0-9a-zA-Z_.]/g, '_');
}
async function promptTimeOffset(inputValue) {
const { value } = await Swal.fire({
title: 'Set custom start time offset',
2019-01-28 11:50:21 +00:00
text: 'Instead of video apparently starting at 0, you can offset by a specified value (useful for viewing/cutting videos according to timecodes)',
input: 'text',
inputValue: inputValue || '',
showCancelButton: true,
inputPlaceholder: '00:00:00.000',
});
if (value === undefined) {
return undefined;
}
const duration = parseDuration(value);
// Invalid, try again
if (duration === undefined) return promptTimeOffset(value);
return duration;
}
2019-01-27 23:21:53 +00:00
2019-01-28 17:09:54 +00:00
function generateColor() {
return randomColor(1, 0.95);
}
function withBlur(cb) {
return (e) => {
cb(e);
e.target.blur();
};
}
function getSegColors(seg) {
if (!seg) return {};
const { color } = seg;
return {
segBgColor: color.alpha(0.5).string(),
segActiveBgColor: color.lighten(0.5).alpha(0.5).string(),
segBorderColor: color.lighten(0.5).string(),
};
}
module.exports = {
formatDuration,
parseDuration,
getOutPath,
2020-02-15 07:57:40 +00:00
getOutDir,
transferTimestamps,
transferTimestampsWithOffset,
2019-01-27 23:21:53 +00:00
toast,
errorToast,
showFfmpegFail,
setFileNameTitle,
promptTimeOffset,
2019-01-28 17:09:54 +00:00
generateColor,
2020-02-24 09:05:17 +00:00
filenamify,
withBlur,
getSegColors,
};